0

I am on a remote machine attempting to move all loader.gif files into a /Pictures folder

find . -iname *.gif | grep loader shows four file I would like to move, naturally I tried: find . -iname *.gif | grep loader -exec cp *.gif /home/me/

How could I copy all files matching my criteria of loader.gif through a find and a grep command? (I am using grep because the original find command results show ALL .gif files, but I only desire loader.gif filenames)

2

GNUly:

find . -iname '*.gif' -print0 | grep -z loader | xargs -r0 cp -t /home/me

POSIXly:

find . -name '*.[gG][iI][fF]' -path '*loader*' -exec sh -c '
  exec cp "$@" /home/me' sh {} +

(I used -path so it be equivalent with the grep solution, but it sounds like you want loader to be found in the name of the file as opposed to its directory components, in which case you should use -name instead).

Note that -iname '*.gif' may fail to match on a .GIF in a locale where uppercase i is İ instead of I (like Turkish locales on GNU systems).

  • What is the significance of loader after grep, I do not see loader in man grep – phillipsk Aug 10 '15 at 0:10
1

I think xargs is the more simple and direct solution:

find . -iname *.gif | grep loader | xargs cp -t /home/me
  • That assumes none of the file names contain blanks (the list of which depends on the locale and the xargs implementation), newline, single quote, double quote or backslash characters. – Stéphane Chazelas Aug 3 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.