cp - Command-line tool to copy a file

learn more… | top users | synonyms

58
votes
6answers
104k views

How do I copy a folder keeping owners and permissions intact?

So I was going to back up my home folder by copying it to an external drive as follows: sudo cp -r /home/my_home /media/backup/my_home With the result that all folders on the external drives are ...
38
votes
6answers
29k views

Is it possible to see cp speed and % copied?

I'm having problems when copy large files using nautilus (it gets stuck) so I need to copy using cp. I would like to know if there is any parameter that shows the % copied and also the transfer ...
29
votes
3answers
35k views
28
votes
4answers
14k views

Difference Between cp -r and cp -R (copy command)

cp- r is meant to copy files recursively, and cp -R for copying directories recursively. But I've checked, and both appear to copy both files and directories, the same thing. So, what's the difference ...
28
votes
2answers
8k views

Why doesn't cp have a progress bar like wget?

Please note that I don't ask how. I already know options like pv and rsync -P. I want to ask why doesn't cp implement a progress bar, at least as a flag ?
27
votes
3answers
12k views

Why unix mv program doesn't need -R (recursive) option for directories but cp does need it?

This is a kind of "wtf?" question, so I'm sorry if I will disturb someone's vision of how things should be or if I will just annoy you with the "stupid" question. I always get messed up when need to ...
22
votes
10answers
14k views

Is there a faster alternative to cp for copying large files (~20 GB)?

I am a graduate student, and the group in which I work maintains a Linux cluster. Each node of the cluster has its own local disk, but these local disks are relatively small and are not equipped with ...
22
votes
8answers
24k views

How to check progress of running cp?

Is it possible to check the progress of running cp process? Some processes respond to various KILL signals so that you can check what is their status. I know that I can run cp with parameter -v but ...
15
votes
2answers
2k views

What happens if a file is modified while you're copying it?

What is the effect of copying a file say fileA.big (900mb) from location B to locationC. If during that cp operation, say 35% through the process, fileA.big is appended with new information and grows ...
14
votes
4answers
994 views

Why not always use rsync?

I wanted to ask is there any reason not to use rsync for everything and abandon cp? I wasn't aware of rsync and now I don't know why cp is ever needed.
14
votes
3answers
4k views

Flattening a nested directory

This is probably very simple, but I can't figure it out. I have a directory structure like this (dir2 is inside dir1): /dir1 /dir2 | --- file1 | --- file2 What is ...
11
votes
4answers
5k views

Copying a single file to multiple directories using cp

This question is a sequel of sorts to my earlier question, Creating numerous directories using mkdir. I am using the bash shell. I have created fifty directories, each starting with the prefix "s", ...
10
votes
3answers
7k views

copy recursively except hidden directory

How do I copy recursively like cp -rf *, but excluding hidden directories (directories starting with .) and their contents?
10
votes
3answers
3k views

How does “cp” handle open files?

I'm having two separate directories. The user loads a file into the first. Theres a cronjob running in the background which copies the files every 5 minutes over to the second directory. What happens ...
9
votes
5answers
9k views

Copy specific file type keeping the folder structure

I have a folder structure with a bunch of *.csv files scattered across the folders. Now I want to copy all *.csv files to another destination keeping the folder structure. It works by doing: cp ...
9
votes
4answers
3k views

pv for directory copy

I need to copy a very large directory (talking in terabytes here) and want to monitor the progress. I found that pv is a nice utility, but how can I use it for copying directories recursively? (pv ...
9
votes
6answers
2k views

How to copy every 4th file in a folder

I have a lot of files in a folder, named like 00802_Bla_Aquarium_XXXXX.jpg. Now I need to copy every 4th file to a subfolder, saying in selected/. 00802_Bla_Aquarium_00020.jpg <= this one ...
8
votes
5answers
4k views

cp with a single argument containing wildcards [duplicate]

If I have the following 2 files and 1 folder: someuser@computer:~/Desktop/test$ ls -l total 340 -rw-r--r-- 1 someuser someuser 45082 ago 5 09:56 file1.pdf -rw-r--r-- 1 someuser someuser 291836 ago ...
8
votes
2answers
335 views

Why do `cp` and `rm` treat directories separately?

Why do tools like cp and rm treat directories separately from regular files? They both require the user to explicitly specify she wants a recursive behavior, or else they won't deal with directories ...
8
votes
3answers
615 views

rsync most recent x GB

I'm looking for a command/script to allow the most recently modified files (up to) 10GB be copied to another computer. So if there are 4 files 4 GB each, only 2 of them should be transferred by the ...
7
votes
3answers
739 views

cp or mv files to same directory they are already in when I am in another?

If I am in ~/blob and I have a file in ~/blob/a_long_directory_name/c/x.x I can type mv blob/a_long_directory_name/c/x.x blob/a_long_directory_name/even_more/y.y Is there any shortcut whereby I ...
7
votes
5answers
8k views

How to copy directories with preserving hardlinks?

How to move directories that have files in common from one to another partition ? Let's assume we have partition mounted on /mnt/X with directories sharing files with hardlinks. How to move such ...
7
votes
3answers
14k views

Using rsync to move (not copy) files between directories?

I have been using rsync to copy files for some time. My understanding is that rsync is faster than cp when some of the files to transfer are already in the destination directory, transferring only the ...
7
votes
4answers
5k views

cp or rsync, is cp really worth it?

I hope this does not count as a question without a real answer, as I can't seem to find a good reason to use cp(1) over rsync(1) in virtually all circumstances. Should one typically favour rsync over ...
7
votes
5answers
577 views

copy smallest files first?

I have a large directory containing subdirectories and files that I wish to copy recursively. Is there any way to tell cp that it should perform the copy operation in order of file size, so that the ...
7
votes
4answers
4k views

What is the purpose of the 'install' command?

I've seen the install command used in a lot of Makefiles, and its existence and usage are kind of confusing. From the manpages, it seems like a knockoff of cp with less features, but I assume it ...
7
votes
1answer
3k views

cannot create regular file 'filename': File exists

I've been getting this strange error message in one of my build scripts - cp fails, returning the error "File exists". I've even tried using cp -f, which should overwrite the file if it exists, but ...
7
votes
1answer
192 views

Strange behaviour in recursive copy

Could someone explain why this happens? Most specifically: Why is one 1's content copied to f? And why is f copied to g? $ tree . 0 directories, 0 files $ mkdir 1 $ mkdir 2 $ touch 1/a $ touch ...
7
votes
4answers
15k views

Copy multiple files from filename X to filename Y?

I'm pretty noob when it comes to unix commands. What I want to do is to copy files within a lexicographic range. Example of files: -rw-r--r-- 1 root root 15276 Oct 5 2004 a2ps.cfg -rw-r--r-- ...
7
votes
1answer
99 views

Is there a way to express: `--link` or fall back to ordinary copy in cp (from GNU coreutils)?

Is there a way to tell cp to --link (i.e. create hard links), but fall back in the case where I am attempting inter-device hardlinks? Inter-device links aren't possible and would cause cp to fail. ...
6
votes
5answers
4k views

How to copy all html files from a directory tree to a single directory?

I want to copy all the .html files in myDir and its subdirectories to ~/otherDir. Here's what I tried (it doesn't work): $ find myDir -name *.html -print | xargs -0 cp ~/otherDir usage: cp [-R [-H | ...
6
votes
2answers
919 views

Copying one file to multiple directories

I have a file that I want to copy to another 60 directories. The best way I can think of doing this is by making a bash script that has instructions to every folder like this: cp script.sh ...
6
votes
2answers
11k views

How do I copy a symbolic link?

I have a symbolic link to a file in one directory. I would like to have that same link in another directory. How do I copy a symbolic link? I tried to cp the symbolic link but this copies the file ...
6
votes
2answers
2k views

How to replace a symbolic link with a copy of a file it links to?

Having a (single, no batch filesystem processing needed) symlink, what a command line to use to replace it with a copy of the file it links to?
6
votes
2answers
6k views

tar files from another directory without copying them

This should be very simple for experts in here. I have a Linux account with user disk quota of only 6GB. There is some data-folder in /opt with about 3GB of data with when compressed comes up 1GB. ...
5
votes
7answers
529 views

Can I avoid enter key in cp -i?

I often need to selectively archive a long list of files using cp -iar, or similar, and to speed up the process I would like to press just one key instead of y or n followed by ENTER on every file. In ...
5
votes
3answers
461 views

How to alias cp with cp -i by default

Is there a good way to alias the command cp file1 file2 to cp -i file1 file2? Apologies if this is a dumb question, I'm a Unix novice.
5
votes
2answers
1k views

Copy from linux to windows every minute

I currently quickly setup a way to copy files from my linux machine to a windows share that multiple people can access, but I'm looking for a better way. In Windows 7, I made a folder accessible to ...
5
votes
3answers
12k views

Copy first n files in a different directory [duplicate]

Possible Duplicate: How to move 100 files from a folder containing thousands? Is it possible to copy only the first 1000 files from a directory to another? Thanks in advance
5
votes
3answers
2k views

recursively copy only images and preserve path

I'm working on a website migration. I have a scrape of the site, with all the files and directory structure as you would see them in the URL. I want to pull all images, maintaining the directory ...
5
votes
2answers
163 views

copying a directory+renaming it

I want to copy a directory from: path1/dir1 to path2/dir2 the first time I invoke cp -r path1/dir1 path2/dir2 there's no problem, dir2 is created under path2 ls path2/dir2 bu the 2nd time, ...
5
votes
1answer
519 views

What is causing this Copy command to enter sleep state?

I am trying to recursively copy a huge directory of installables over 25G and want to preserve the owner and group id, permission modes and so on. So I ran the below commands: bash-3.2$ date; cp ...
5
votes
1answer
522 views

Symbolic links with ls, mv: forcing the functions to utilize “logical” addresses (remembering the original path)

It seems that for many basic functions operating on symbolic links, the physical path is used by default. However, cd works fine. When moving into a symbolic link, cd remembers where I came from so it ...
5
votes
1answer
1k views

preserving extended attributes with cp/rsync

When copying with cp, the extended attributes are not preserved, even with explicit cp -a --preserve=all /source /dest or cp -a --preserve=xattr /source /dest The same is with rsync, i.e. ...
5
votes
3answers
678 views

Batch copy to multiple directories

I have about 9000 files in a directory and I want to mv them into 90 files in 100 directories by filename order, ignoring any remainder. In Copy multiple files from filename X to filename Y?, Caleb ...
5
votes
1answer
397 views

ACL mask does not work as expected

If I copy a file with a base ACL of: u::rw-,g::r--,o::r-- into a directory with a default ACL of: u::rwx,g::r-x,g:users:rwx,m::rwx,o::r-x I obtain a file with mask of m::r--. I would have ...
5
votes
4answers
2k views

Copying specific subfolders with directory structure to a new folder

I am having a the following directory structure: Main_Dir | ----------------------------------- Subdir1 Subdir2 Subdir3 | | ...
5
votes
2answers
118 views

Is there a better way than cp to copy millions of files while preserving hard links?

So in this story on the GNU coreutils ML, someone used cp to copy 430 million files and needed to preserve hard links, and just barely got it to finish after 10 days. The big problem was that, in ...
4
votes
3answers
640 views

Multithreaded cp on linux? [duplicate]

Is there a multi-threaded cp command on Linux? I know how to do this on Windows, but I don't know how this is approached in a Linux environment.
4
votes
3answers
3k views

How do I move some but not all files from one directory to another?

I need to move my files from one directory to other. But there is some issues. My file name pattern is like: apple.0, apple.<n>, n -> {0,1,2,3 ...~ } so mv apple.* will not work, because ...