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 ...
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 ...
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, --...
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; ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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:
-...
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 ...
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'...
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 ...
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 &...
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 ...
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.
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 ...
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 ...
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 ...
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
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 ...
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 ...
24
votes
Accepted
Copy nested directory structure ignoring all directories of a certain name
rsync -rv --exclude=node_modules --exclude=env /projects /destination
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cp × 756linux × 98
bash × 95
files × 82
file-copy × 72
shell × 68
find × 68
rsync × 66
command-line × 63
shell-script × 55
directory × 54
mv × 42
permissions × 32
rename × 28
symlink × 28
recursive × 24
filenames × 23
wildcards × 23
xargs × 21
macos × 19
backup × 18
coreutils × 17
hard-link × 16
ubuntu × 15
filesystems × 15