중첩 된 하위 디렉터리에서 상대 디렉터리로 많은 수의 파일 (.jpg)을 일괄 이동 / 정리하려고합니다. 구조는 미리 계획되어 있습니다. 가장 효율적이고 안전한 방법은 100 % 확신 할 수 없습니다.
샘플 구조 :
/directory/subdir/jpg/
/directory/subdir/source/something.jpg
/directory/subdir/source/something.tif
/directory/subdir/source/something-else.jpg
/directory/subdir/source/something-else.tif
/directory/subdir/source/another-file.jpg
/directory/subdir/source/another-file.tif
/directory/another-subdir/jpg/
/directory/another-subdir/source/yet-another-file.jpg
/directory/another-subdir/source/yet-another-file.tif
목표는 그것을 얻는 것입니다 …
/directory/subdir/jpg/something.jpg
/directory/subdir/jpg/something-else.jpg
/directory/subdir/jpg/another-file.jpg
/directory/subdir/source/something.tif
/directory/subdir/source/something-else.tif
/directory/subdir/source/another-file.tif
/directory/another-subdir/jpg/yet-another-file.jpg
/directory/another-subdir/source/yet-another-file.tif
이런 식으로 생각했습니다. 내 구조를 토스트하는지 확실하지 않습니다. 우리는 수십 개의 데이터 및 고객 조직에 중요한 파일 수천 개를 말하고 있습니다.
find /directory -name \*.jpg -exec mv {} ../jpg/ \;
실제로 실행하기 전에 시각적으로 테스트하는 데 사용할 수있는 “마른 실행”에 대해 알 수있는 몇 가지 형태가 있다면 그것은 놀랄 것입니다. 고맙습니다!
최신 정보:
사실, 지금은 Mac에서 같은 일을 로컬에서 수행하려고하는데,이 오류가 발생합니다. 현명한 해결 방법은 무엇입니까?
$ find -name "*.jpg" -execdir pwd \; -execdir echo mv -v '{}' ../jpg \;
find: illegal option -- n
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
답변
간단히 말하면 다음과 같이 드라이 런을 할 수 있습니다 echo
.
find /directory -name \*.jpg -exec echo mv {} ../jpg/ \;
그러나 그것은 ../jpg
현재 디렉토리에서 항상 평가되는 것처럼 당신이 원하는 것을하지 않을 것이고, 따라서 모든 jpg 이미지를 이동시킬 것 $PWD/../jpg
입니다.
예상대로 작동합니다.
find /directory -name "*.jpg" -execdir pwd \; -execdir echo mv -v '{}' ../jpg \;
때문에 -execdir
입니다
like
-exec
이지만 지정된 명령은 일치하는 파일이 들어있는 서브 디렉토리에서 실행됩니다.이 파일은 일반적으로 사용자가 찾은 디렉토리가 아닙니다. 이는 명령을 호출하는 훨씬 더 안전한 방법입니다. 일치 된 파일에 대한 경로를 확인하는 동안 경쟁 조건을 피할 수 있습니다.
그러나 물론 “우리는 수십 개의 데이터와 수천 개의 파일을 우리 고객 조직에 중요한 부분으로 이야기하고 있습니다.” 항상 최신 백업을 보유하고 있습니다 …