Skip to main content
76 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
  • 1,102
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's user avatar
  • 339k
71 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
  • 141k
61 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
  • 903
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

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
  • 67.4k
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
  • 101k
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

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
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,965
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.7k
33 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
  • 10.2k
32 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
  • 429
29 votes
Accepted

cp: cannot create regular file: Invalid argument

The syntax of your command is correct. “Invalid argument” from cp usually means that the file name is not valid on the target filesystem. It may be too long, contain a forbidden character, or be a ...
Gilles 'SO- stop being evil''s user avatar
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's user avatar
  • 339k
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's user avatar
  • 244k
27 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,573
27 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's user avatar
  • 339k
25 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
  • 416
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

Making alias of rm command

An alias can not take arguments and use $@ to access them like that. Alias expansion in bash is a simple text replacement. If you have alias rm ='something something', then using rm file1 file2 would ...
Kusalananda's user avatar
  • 339k
24 votes

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

cp comes from the beginning of Unix. It was there well before the Posix standard was written. Indeed: Posix just formalized the existing behavior of cp in this regard. We're talking around Epoch (...
Ljm Dullaart's user avatar
  • 4,863
23 votes
Accepted

Grep word within a file then copy the file

Try: grep -rl --null --include '*.txt' LINUX/UNIX . | xargs -0r cp -t /path/to/dest Because this command uses NUL-separation, it is safe for all file names including those with difficult names that ...
John1024's user avatar
  • 75.2k
21 votes
Accepted

How to move content of a folder to current folder?

mv will overwrite files, but it will refuse to overwrite directories. There's no single command that will merge directories and remove the source directories (which is probably what you want with mv). ...
muru's user avatar
  • 73.8k
20 votes

How to copy or move files without being asked to overwrite?

For force overwrite without asking you should use the command mv and the option "-f", use man to see the options. man mv: -f, --force do not prompt before overwriting Example: mv -f ...
sinkmanu's user avatar
  • 341
20 votes
Accepted

cp some/path/**/*.h is not including .h files at all depths

**/ to match any level of subdirectories is from the zsh shell from 1992. ksh93 did copy it in 2003, but made it disabled by default for backward compatibility (where before ** was the same as *), ...
Stéphane Chazelas's user avatar
19 votes

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

Because these commands are also meant to be used in scripts, possibly running without any kind of human supervision, and also because there are plenty of cases where you indeed want to overwrite the ...
xenoid's user avatar
  • 9,018
19 votes
Accepted

cp command follows symbolic link at destination and clobbers link target instead of overwriting link

This depends on what Unix you are using. On some BSD systems (OpenBSD, FreeBSD), you will find that cp -f will unlink (remove) the symbolic link and replace it with the source. Using GNU cp, this ...
Kusalananda's user avatar
  • 339k

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