Sunday, February 28, 2016

Moving all the files that DO NOT match extensions

If you want to move from the folder all the files that do not match given extensions use find with ! option to reverse search:

mpjanic@valkyr:~/tmp$ ls -ltrh
total 48K
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 1.gif
mpjanic@valkyr:~/tmp$ find -type f ! \( -iname '*.gif' -o -iname '*.jpg' \) -exec  mv {} ~/tmp2 \;
mpjanic@valkyr:~/tmp$ ls -ltrh
total 24K
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 1.gif
mpjanic@valkyr:~/tmp$ ls -ltrh ~/tmp2
total 24K
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tif
Similarly, remove ! if you want to move files that match the extensions:

mpjanic@valkyr:~/tmp$ find -type f \( -iname '*.gif' -o -iname '*.jpg' \) -exec  mv {} ~/tmp2 \;
mpjanic@valkyr:~/tmp$ ls -ltrh
total 24K
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tiff
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 2.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 3.tif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.tif
mpjanic@valkyr:~/tmp$ ls -ltrh ~/tmp2
total 24K
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:26 1.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.jpg
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 3.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 2.gif
-rwxrwxr-x 1 mpjanic mpjanic 3 Feb 28 21:27 1.gif

No comments:

Post a Comment