0

I have a directory with files such as

aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png

Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.

0

You can do this with a one liner:

grep '^aaa.*\.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/

grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'

If you are in the directory of the files you can just cp them with:

cp aaa*.png destination_dir
  • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job? – user2842390 Jan 22 at 0:18
  • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp – user1794469 Jan 22 at 0:21
  • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them. – Nasir Riley Jan 22 at 0:25
  • @NasirRiley he edited the question. – user1794469 Jan 22 at 0:27
  • 1
    @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available. – Nasir Riley Jan 22 at 0:30
6
cp aaa*.png /some/destdir

This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.

This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.

In that case, use something like the following loop:

for name in aaa*.png; do
    cp "$name" /some/destdir
done

This would copy the files one by one.

A more efficient method for many thousands of files would be (using GNU cp with its -t option):

find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +

Or (without GNU cp):

find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +

This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.

More on find using -exec: Understanding the -exec option of `find`

0

With find:

find . -maxdepth 1 -type f -name aaa\*.png -exec cp {} destination \;

. indicates the current directory.

-maxdepth 1 tells it to only look in the current directory

type -f tells it to look for files

-name aaa\*.png indicates files beginning with aaa and ending in .png

-exec cp {} destination \; copies the files into the directory called destination.

My environment requires me to escape the * with a \. Yours may not require this so you may be able to just use aaa*.png

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.