1

I have directory named "Documents". In this directory I have 5 files:

User1.txt

User2.txt

User3.txt

User4.txt

User5.txt

Users-info.zip

index.html

I want to copy only those files in whose names there is a word "user" to another directory. How I can do this with cp command?

3
  • Basic globbing cp User* dirname Or to copy only those with a number, cp User[0-9]* dirname Commented Dec 27, 2017 at 7:53
  • You should also spend time with the Advanced Bash-Scripting Guide or Bash Guide for Beginners Commented Dec 27, 2017 at 7:58
  • This title was a bit misleading to me. By "the same name," I assumed the files were exactly the same name and in different subfolders. Perhaps the title could be clarified with "How to copy all files with the same name prefix into another directory"? Commented Dec 8, 2022 at 20:58

2 Answers 2

2

For your case it is:

cp User[1-9].txt /dst_dir 

We copy only files with User in the beginning, than some digit and finally .txt.

Sign up to request clarification or add additional context in comments.

6 Comments

I think he wants Users-info.zip as well. Either way, he can choose what meets his needs. (you can use a bit more explanation as well :)
When I am using your command it says: usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
What OS do you use?
I am using OS X 10.9.5
But you have tag linux in your question. My answer is correct for linux OSs.
|
2

cp User* /path/to/dir try this, will be enough.

If you wish unusual way:

find . -type f -name 'User*' -print0 | xargs -0 cp -t /path/to/dir/for/copies/ 

9 Comments

Your answer is correct, but a little more explanation than "try this, will be enough." is called for in any Answer, otherwise, leave a Comment.
When I use your command it says: cp: user*: No such file or directory
@Alex first go to the "Documents" directory cd Documents. Or try cp ~/Documents/User* /path/to/dir
@ViktorKhilin Yes I am doing this in "Documents" directory but it still makes an error
mm, in your question files named User, nout user. You write command correctly, 'U' in 'User' is capital?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.