![mv만 사용하여 폴더 이동](https://linux55.com/image/132065/mv%EB%A7%8C%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%ED%8F%B4%EB%8D%94%20%EC%9D%B4%EB%8F%99.png)
파일과 폴더가 포함된 폴더가 있습니다.
folder/file1.jpg
folder/file2.jpg
folder/file3.jpg
folder/subfolder1/file1.txt
folder/subfolder1/file2.txt
folder/subfolder2/file1.txt
folder/subfolder3/
destination/
모든 폴더(및 해당 내용)를 새 폴더로 이동하고 싶지만 파일은 이동하고 싶지 않습니다.
예를 들어.
folder/file1.jpg
folder/file2.jpg
folder/file3.png
destination/subfolder1/file1.txt
destination/subfolder1/file2.txt
destination/subfolder2/file1.txt
destination/subfolder3/
예를 들어 모든 jpeg 파일을 선택하고 싶다면 이렇게 할 것이라는 것을 알고 있습니다 mv folder/*.jpg destination
. 그런데 모든 폴더를 선택하는 명령은 무엇입니까?
답변1
이렇게 하려면 다음과 같이 * 끝에 /를 추가하면 됩니다.
mv folder/*.jpg destination (match only jpg files)
mv folder/* destination (match anything found)
mv folder/*/ destination (match only the folders)
이렇게 하면 "폴더" 내의 파일이 아닌 "폴더" 내의 폴더만 대상으로 이동됩니다(하위 폴더의 파일은 이동됩니다).
답변2
하위 폴더에 동일한 이름이 있으면 다음을 사용할 수 있습니다.엠마 루오의 답변. 그렇지 않은 경우 간단한 쉘 루프를 사용할 수 있습니다.
mkdir -p destination
for name in folder/*; do
[ ! -d "$name" ] && continue
mv "$name" destination
done
이것은 (파일 및 디렉토리 등)의 모든 디렉토리 항목을 반복하고 folder
각 디렉토리 항목이 디렉토리인지 테스트한 후 이동합니다.
또 다른 가능성은 다음을 사용하는 것입니다 find
.
mkdir -p destination
find folder -mindepth 1 -maxdepth 1 -type d -exec mv {} destination ';'
folder
그러면 모든 디렉터리 (아래 디렉터리나 디렉터리 자체는 아님 ) 에 대한 모든 경로 이름을 찾고 folder
발견된 각 디렉터리를 destination
.
답변3
실제 디렉토리 이름에 따라 다음을 사용할 수 있습니다.
mv folder/subfolder* destination/
subfolder*
폴더 이름과 일치하는 패턴( )이 없으면 이렇게 할 수 있습니다.
find folder -mindepth 1 -maxdepth 1 -type d -exec mv {} destination/ \;
이것조차
find folder -mindepth 1 -maxdepth 1 -type d -exec mv -t destination/ {} +