cp is the command of the Unix operating system that is used to copy files.
Questions tagged [cp]
217 questions
638
votes
18 answers
How to copy with cp to include hidden files and hidden directories and their contents?
How can I make cp -r copy absolutely all of the files and directories in a directory
Requirements:
Include hidden files and hidden directories.
Be one single command with an flag to include the above.
Not need to rely on pattern matching at…
![](../../users/profiles/8544.webp)
eleven81
- 15,376
- 15
- 55
- 83
233
votes
10 answers
How to copy symbolic links?
I have directory that contains some symbolic links:
user@host:include$ find .. -type l -ls
4737414 0 lrwxrwxrwx 1 user group 13 Dec 9 13:47 ../k0607-lsi6/camac -> ../../include
4737415 0 lrwxrwxrwx 1 user group 14 Dec 9 13:49…
![](../../users/profiles/28322.webp)
Basilevs
- 2,766
- 3
- 20
- 20
106
votes
4 answers
How to copy symlinks to target as normal folders
i have a folder with symlinks:
marek@marek$ ls -al /usr/share/solr/
razem 36
drwxr-xr-x 5 root root 4096 2010-11-30 08:25 .
drwxr-xr-x 358 root root 12288 2010-11-26 12:25 ..
drwxr-xr-x 3 root root 4096 2010-11-24 14:29 admin
lrwxrwxrwx 1…
Marek
97
votes
8 answers
How can I recursively copy files by file extension, preserving directory structure?
At the Linux command line, I'd like to copy a (very large) set of .txt files from one directory (and its subdirectories) to another.
I need the directory structure to stay intact, and I need to ignore files except those ending in .txt.
![](../../users/profiles/86617.webp)
unclaimedbaggage
- 1,085
- 1
- 9
- 7
87
votes
9 answers
Copy files in Linux, avoid the copy if files do exist in destination
I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.
I use
cp -r /origin /destination
sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need…
![](../../users/profiles/10826.webp)
Open the way
- 8,283
- 13
- 49
- 69
61
votes
4 answers
Copying a file only when it is newer than the destination
How do I copy a file in Linux only when the file being copied is newer than the version at the destination?
If the file at the destination is newer, I want the file copy to not go ahead.
![](../../users/profiles/99591.webp)
Eli
- 785
- 2
- 6
- 7
52
votes
4 answers
Copy list of files
I have a list of files separated by spaces in a file list.txt. I'd like to copy them to a new folder.
I tried to do:
cp `cat list.txt` new_folder
but it did not work.
How would you do this ?
Update:
Thank you for all your very interesting answers.…
![](../../users/profiles/31069.webp)
Klaus
- 653
- 1
- 8
- 13
50
votes
8 answers
Why is -r recursive necessary when copying a directory in Linux?
My question is why is it required to use the -r (recursive) flag when making a copy of a directory? I.e., why do this:
$ cp -r dir1 copyDir1
When would I not want this behavior when copying a directory?
Isn’t a recursive copy of a directory really…
![](../../users/profiles/320542.webp)
Madeleine P. Vincent
- 844
- 1
- 8
- 16
41
votes
2 answers
Use rsync to copy all files except for certain filenames with a certain extension
I have two same-size flash cards, and I want to copy contents of one to the other based on the following rules:
I want all directories and subdirectories in place
I want to exclude files of type .FOO, .BAR, and .ZIM (all other files are…
![](../../users/profiles/26787.webp)
macek
- 6,035
- 17
- 46
- 57
37
votes
4 answers
Recursive, Non-Overwriting File Copy?
I've got a directory that contains a bunch of other folders containing CoffeeScript/ JavaScript files. I'm able to compile the CoffeeScript files into a new folder with the same folder structure fine.
What I want to do is copy all the *.js files in…
![](../../users/profiles/197740.webp)
AlbertEngelB
- 1,096
- 2
- 11
- 20
27
votes
2 answers
Why do we use cp to copy files and not dd? (in unix-derivatives)
For normal file copying in *nix, I've only ever seen people use cp (eg. cp /mnt/mydevice/myfile ~/myfile), but I eventually ran into dd, which appears to do the exact same thing (dd if=/mnt/mydevice/myfile of=~/myfile). I do see that they have some…
![](../../users/profiles/232105.webp)
user232105
- 273
- 1
- 3
- 5
25
votes
4 answers
How do you use regular expressions with the cp command in Linux?
I am attempting to only copy a subset of files from one directory to another using cp however am greeted with the message cp: cannot stat [^\.php]': No such file or directory. Is there a way to use regular expressions only using the cp command?
![](../../users/profiles/79947.webp)
PeanutsMonkey
- 8,900
- 36
- 90
- 133
24
votes
3 answers
How is install -c different from cp
What is the difference between install -c and cp? Most installations tend to use install -c, but from the man page it doesn't sound like it does anything different than cp (except maybe the ability to also set permissions). When should I use install…
![](../../users/profiles/45153.webp)
sligocki
- 849
- 1
- 9
- 11
24
votes
2 answers
Can I hard link files with rsync instead of copying them?
cp -l hard links files instead of copying them, saving filesystem space. I need to use rsync instead of cp because of its --exclude capabilities.
So my question is, how do I get rsync to hard link files instead of copying them? Obviously, this is a…
![](../../users/profiles/11203.webp)
nnyby
- 1,359
- 3
- 13
- 22
23
votes
4 answers
Copying many files without stopping on errors on OSX
I need to copy several Gb from an external HD to my main HD and some files will cause errors. If I do this with the finder, it will stop on the first error.
Is there a way to copy everything no matter the errors? Something like copy of Teracopy for…
![](../../users/profiles/12461.webp)
cfischer
- 8,973
- 11
- 37
- 42