Hot answers tagged

93 votes

How to copy a directory recursively using hardlinks for each file

On Linux (more precisely with the GNU and busybox implementations of cp as typically found on systems that have Linux as a kernel) and recent FreeBSD, this is how: cp -al dirA dirB For a more ...
Gudmundur Orn's user avatar
90 votes
Accepted

How do I suppress error messages from cp?

To suppress error output in bash, append 2>/dev/null to the end of your command. This redirects filehandle 2 (STDERR) to /dev/null. There are similar constructs in other shells, though the specific ...
John's user avatar
  • 16.5k
89 votes

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

cp -a Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d). From man cp: -a, --...
Zaz's user avatar
  • 2,419
79 votes
Accepted

How to copy a folder recursively in an idempotent way using cp?

Use this instead: cp -R inputFolder/. outputFolder This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; ...
roaima's user avatar
  • 104k
73 votes
Accepted

Why is mv so much faster than cp? How do I recover from an incorrect mv command?

If a directory is moved within the same filesystem (the same partition), then all that is needed is to rename the file path of the directory. No data apart from the directory entry for the directory ...
Kusalananda on strike's user avatar
65 votes
Accepted

How can I copy a directory and rename it in the same command?

You should be able to do just cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21 However, if the target directory already exists, this would append the final part of the source path ...
ilkkachu's user avatar
  • 131k
64 votes
Accepted

How to copy directories via ssh

If you want to copy a directory from machine a to b while logged into a: scp -r /path/to/directory user@machine_b_ipaddress:/path/to/destination If you want to copy a directory from machine a to b ...
Erik's user avatar
  • 952
61 votes
Accepted

Purpose of cp -x (stay on file system)?

It limits where files are copied from, not where they’re copied to. It’s useful with recursive copies, to control how cp descends into subdirectories. Thus cp -xr / blah will only copy the root file ...
Stephen Kitt's user avatar
59 votes

cp after xargs not working

While you already know how you should solve your current problem, I'll still answer about xargs. xargs puts the string it got in the end of command, while in your case you need that string before the ...
aragaer's user avatar
  • 883
58 votes

Is it possible to see cp speed and percent copied?

Is it possible to see cp speed and percent copied? Yes, it is possible to display the progress of the command cp on the terminal. Method #1: (use progress) Open a new terminal and type progress it ...
GAD3R's user avatar
  • 62.5k
57 votes
Accepted

Is `cp --no-clobber` vulnerable to race condition?

cp isn’t vulnerable to this race condition. When --no-clobber is set, it checks whether the destination already exists; if it determines it doesn’t, and it should therefore proceed with the copy, it ...
Stephen Kitt's user avatar
56 votes
Accepted

Why was `cp` designed to silently overwrite existing files?

The default overwrite behavior of cp is specified in POSIX. If source_file is of type regular file, the following steps shall be taken: 3.a. The behavior is unspecified if dest_file exists ...
telcoM's user avatar
  • 83.5k
47 votes

Why is cp's option not to overwrite files called --no-clobber?

“Clobber” in the context of data manipulation means destroying data by overwriting it. In the context of files in a Unix environment, the word was used at least as far back as the early 1980s, ...
Gilles 'SO- stop being evil''s user avatar
40 votes
Accepted

Why can't I copy all files in a directory to a USB storage device?

My eyes jump straight to the fact that your file name has a pipe | in it. According to your output the file system type is exfat. FAT and its derivatives do not support inclusion of pipe, along with a ...
bxm's user avatar
  • 4,466
39 votes

Randomly copy certain amount of certain file type from one directory into another

You could use shuf: shuf -zn8 -e *.jpg | xargs -0 cp -vt target/ shuf shuffles the list of *.jpg files in the current directory. -z is to zero-terminate each line, so that files with special ...
chaos's user avatar
  • 47.2k
39 votes
Accepted

cp behaves weirdly when . (dot) or .. (dot dot) are the source directory

The behaviour is a logical result of the documented algorithm for cp -R. See POSIX, step 2f: The files in the directory source_file shall be copied to the directory dest_file, taking the four steps ...
Stephen Kitt's user avatar
38 votes
Accepted

How to cp remaining files after failed attempt

I would try, rsync -a /from/file /dest/file you can use other options like --append, -P (--partial --progress). See man rsync for more info. Or if you are using cp then use cp -u. from man cp: -...
Rahul's user avatar
  • 13.2k
33 votes
Accepted

Why use diff/patch when it is easier to just use cp

Diffs can be more complicated than just comparing one file versus another. The can compare entire directory hierarchies. Consider the example that I want fix a bug in GCC. My change adds a line or ...
casey's user avatar
  • 14.5k
32 votes

How to copy a directory recursively using hardlinks for each file

POSIXly, you'd use pax in read+write mode with the -l option: pax -rwlpe -s /A/B/ dirA . (-pe preserves all possible attributes of files (in this case only directories) that are copied, like GNU cp'...
Stéphane Chazelas's user avatar
32 votes
Accepted

What is the added value of the -T option in GNU cp and mv?

Your . trick can only be used when you're copying a directory, not a file. The -T option works with both directories and files. If you do: cp srcfile destfile and there's already a directory named ...
Barmar's user avatar
  • 9,564
30 votes

Copy specific file type keeping the folder structure

This one worked for me: find -name "*.csv" | xargs cp --parents -t /target If you have file names with spaces, add options -print0 and -0 like suggested in one of the comments: find -name &...
marko's user avatar
  • 409
29 votes

Purpose of cp -x (stay on file system)?

The -x flag to cp is a GNU extension. When copying a single file, this option will have no effect, but when copying a whole file hierarchy, the -x option prevents the copying of files and directories ...
Kusalananda on strike's user avatar
28 votes
Accepted

How to copy and add prefix to file names in one step?

a for loop: for f in *.c; do cp -- "$f" "$OTHERDIR/old#$f"; done I often add the -v option to cp to allow me to watch the progress.
glenn jackman's user avatar
28 votes
Accepted

Why does "cp -R --reflink=always" perform a standard copy on a btrfs filesystem?

cp --reflink=always is almost certainly working correctly. If it weren't, you would be getting an error. By design, that's the difference between --reflink=always and --reflink=auto. The error would ...
Celada's user avatar
  • 42.7k
28 votes

Why is cp's option not to overwrite files called --no-clobber?

Because this is actually a standard term. As explained in Wikipedia: In software engineering, clobbering a file or computer memory is overwriting its contents. The Jargon File defines clobbering ...
terdon on strike's user avatar
28 votes
Accepted

I've copied a directory with `cp -as` and now I'm terrified to `rm -rf` the created directory as it might delete the original

You may remove the directory containing the symbolic links without fear that this would also remove the original files. The POSIX specification for the rm utility says (about what happens when ...
Kusalananda on strike's user avatar
25 votes
Accepted

What is the difference between * and *.* while copying?

*.* only matches filenames with a dot in the middle or at the end. For example: abc.jpg def. * matches the filenames above, plus the names which don't have a dot at all. for example: data
SparedWhisle's user avatar
  • 3,568
24 votes

How to copy a file from a remote server to a local machine?

You can use rsync as an alternative. It is mainly for syncing files.. but you can use it for this purpose as well. rsync -avzh --stats --progress remoteuser@remoteip:/path/ localpath to add ssh ...
fugitive's user avatar
  • 1,533
24 votes

What is the added value of the -T option in GNU cp and mv?

The problem with cp/mv/ln as they were originally designed is that they're two commands in one (copy to and copy into). cp A B is either copy A to B or copy A into B (copy A to B/A) depending on ...
Stéphane Chazelas's user avatar
24 votes
Accepted

Copy nested directory structure ignoring all directories of a certain name

rsync -rv --exclude=node_modules --exclude=env /projects /destination
jayson's user avatar
  • 406

Only top scored, non community-wiki answers of a minimum length are eligible