0

How do I copy all the files with "t_1" in the name to a different directory using one command?

  • 2
    cp "*t_1*" /target/dir/... – jasonwryan Sep 24 '16 at 0:58
  • @jasonwryan it is saying "cp: cannot stat ‘t_1’: No such file or directory" – Jarhead Sep 24 '16 at 1:05
  • Are you in a directory that includes at least one file with t_1 in the name? – jasonwryan Sep 24 '16 at 1:15
  • Yes because I can do ls | grep "t_1" and files show up. There are 4. – Jarhead Sep 24 '16 at 1:22
  • 2
    Remove the quotes: *t_1*... – jasonwryan Sep 24 '16 at 1:26
1

Use

cp /path/to/files/*t_1* /path/to/destination

or just

cp ./*t_1* /path/to/destination

if the files are in the current directory.

Or,

for f in ./*t_1*; do
    cp "$f" /path/to/destination
done

That should work for sure, even if there are many thousands of files matching the pattern in the current directory.

-1

Here is a quick fix..

find ./ -name '*t_1*' -exec mv '{}' ./ \;

This code will move all the files (containing the search pattern in the file name) one level up the hierarchy.

For example, Let's say that all your 't_1' files are stored in /Home/Desktop/ directory. Then just go to /Home/ from terminal and execute this command. All the 't_1' files will be moved from /home/Desktop/ to /home/ directory.

All the files irrespective of the position of t_1 in the filename will be copied.

  • just copy the command as it is....presence or absence of a whitespace will affect the results. I have tried and tested this command before posting this answer – Upendra Pratap Singh Sep 24 '16 at 6:03
  • once you have all your files in the parent folder, you can move the entire parent folder wherever you wish – Upendra Pratap Singh Sep 24 '16 at 6:09
  • This would copy all the files matching the pattern in the current directory or below, to the current directory, not just from one level below the current directory. – Kusalananda Jun 22 at 7:11

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.