Copy all files in a directory with a particular string in the filename to different directory in Bash [closed] Copy all files in a directory with a particular string in the filename to different directory in Bash [closed] bash bash

Copy all files in a directory with a particular string in the filename to different directory in Bash [closed]


$ mkdir NEWDIR$ touch foo_file file_foo file_foo_file$ lsNEWDIR      file_foo    file_foo_file   foo_file$ cp -v *foo NEWDIR/file_foo -> NEWDIR/file_foo$ cp -v foo* NEWDIR/foo_file -> NEWDIR/foo_file$ cp -v *foo* NEWDIR/file_foo -> NEWDIR/file_foofile_foo_file -> NEWDIR/file_foo_filefoo_file -> NEWDIR/foo_file$ ls NEWDIR/file_foo    file_foo_file   foo_file


cp *foo* /path/to/separate_directory

If you want to validate the files that will be included first, the use:

ls *foo*

This will confirm the files to be matched, then you can re-use the same pattern with the cp command to execute the copy.