Skip to main content
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
  • 356k
49 votes

How to copy files from one machine to another using ssh

If they are running SCP / SSH on a different port, make sure you specify the uppercase -P port option. Example: scp -P 7121 /users/admin/downloads/* [email protected]:/home/
Carter's user avatar
  • 591
46 votes

How can I copy a /tmp/ directory that is created & deleted by a process?

You could always run the application under: gdb --args /path/to/your/your-program and its args Then add breakpoints on unlink(), unlinkat(), rmdir() functions or syscalls: catch syscall unlink catch ...
Stéphane Chazelas's user avatar
40 votes
Accepted

How do programs that can resume failed file transfers know where to start appending data?

For clarity's sake - the real mechanics is more complicated to give even better security - you can imagine the write-to-disk operation like this: application writes bytes (1) the kernel (and/or the ...
LSerni's user avatar
  • 4,695
33 votes

Why use install rather than cp and mkdir?

Apart from the previous descriptions here about the usage, there is a low level difference between cp and install, at least on Linux. If copying over an existing file, cp overwrites the existing inode ...
Tomas Skäre's user avatar
27 votes
Accepted

Approximating atomic move across file systems?

rsync copies to temporary filenames (e.g. see Rsync temporary file extension and rsync - does it create a temp file during transfer?) unless you use the --inplace option. It renames them only after ...
cas's user avatar
  • 83.9k
27 votes

What does !cp:$ do?

This is history expansion, introduced by !; the $ must be intepreted in that context. !cp:$ has two parts, separated by :. !cp is the event designator, it means “find the most recent command starting ...
Stephen Kitt's user avatar
26 votes
Accepted

What happens when I kill 'cp'? Is it safe and does it have any consequences?

This is safe to do, but naturally you may not have finished the copy. When the cp command is run, it makes syscalls that instruct the kernel to make copies of the file. A syscall, or system call, is a ...
forest's user avatar
  • 3,175
25 votes

How to copy files from one machine to another using ssh

A simpler method that works with via SSH controlled NVIDIA Jetson computers is to connect with SFTP (SSH File Transfer Protocol). Suppose I wish to move the document UFO_blueprint.odt from NASA's ...
Pe Dro's user avatar
  • 1,519
23 votes

What happens when I kill 'cp'? Is it safe and does it have any consequences?

Since cp is a userspace command, this does not affect filesystem integrity. You of course need to be prepared that at least one file will not have been copied completely if you kill a runnning cp ...
schily's user avatar
  • 19.7k
21 votes

Problem with install command to copy a whole directory

To copy all the files into the exact same directory (here target/directory): find directory/to/copy -type f -exec install -Dm 755 "{}" "target/directory" \; -D is not mandatory ...
Vrakfall's user avatar
  • 403
20 votes

How to sync two folders with command line tools?

The answer from TuxForLife is pretty good, but I strongly suggest you use -c when syncing locally. You can argue that it's not worth the time/network penalty to do it for remote syncs, but it is ...
Bruno Bronosky's user avatar
19 votes

Copy file along with creating directory

With the install command: install -D -m644 /a/b/c/xxxxx.sql /a/b/d/c/xxxxx.sql From man install: install - copy files and set attributes You can chose the Unix rights by modifying 644 by what you ...
Gilles Quénot's user avatar
17 votes
Accepted

Timestamps of files copied to USB drive

The problem with the timestamp seconds changing comes from the fact that a VFAT (yes, even FAT32) filesystem stores the modification time with only 2-second resolution. Apparently, as long as the ...
telcoM's user avatar
  • 114k
17 votes
Accepted

When moving files between two external drives, are they temporarily written to the internal hdd of the computer?

If you have a copy such as this, or its GUI equivalent, cp -a /media/external/disk1/. /media/external/disk2/ the data is read from the first disk's filesystem and written directly to the second. ...
Chris Davies's user avatar
16 votes
Accepted

cp losing file's metadata

If you use man cp to read the manual page for the copy command you'll find the -p and --preserve flags. -p same as --preserve=mode,ownership,timestamps and --preserve[=ATTR_LIST] preserve the ...
Chris Davies's user avatar
16 votes
Accepted

Moving millions of files to a different directory with specfic name patterns

You should use: find . -maxdepth 1 -type f -name '??????????_a1ac*.jpg' \ -exec mv -t destination "{}" + So maxdepth 1 means that you want to search in current directory no subdirectories. type f ...
Prvt_Yadav's user avatar
  • 6,032
15 votes

How to copy a file with unique name without navigating to directory?

Using find: find . -type f -name Screenshot20180509143013.png -exec cp {} /dev/media/SD256/DestinationFolder ';' This would find all regular files in or below the current directory, whose names are ...
Kusalananda's user avatar
  • 356k
14 votes

How to copy files from one machine to another using ssh

Unix/Linux recursive copy of files and folders over a secure shell using the code following: scp -r <host@machine_name:from_remote_directory_path> <to_local_directory_path> A full example ...
Sizwe's user avatar
  • 141
14 votes

How to copy a file with unique name without navigating to directory?

With zsh or fish or ksh -o globstar or bash -O globstar (or after shopt -s globstar in bash) or tcsh after set globstar or yash -o extended-glob: cp -- **/Screenshot20180509143*3.png /dev/media/SD256/...
Jeff Schaller's user avatar
  • 68.8k
14 votes

What happens when you rsync without a destination path?

You now have a file in your working directory called "user@host". If you had copied it to a remote location without a path, user@host: (note colon) would have called it file.txt in whatever the ...
Michael Homer's user avatar
13 votes

How to copy-merge two directories?

There are faster and much more space-efficient ways of merging two directories using the --link option to cp if the directories are on the same file system, described in the multiple varied answers in ...
Ian D. Allen's user avatar
12 votes

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

The existing answer is great, but I'd like to expand on it a bit by showing exactly what is happening when you move versus when you copy a file. When you look at the syscalls during a copy, you see: ...
forest's user avatar
  • 3,175
12 votes

Copy file along with creating directory

Using GNU cp, do the copy from the starting point of the path you want to see reflected in the new directory and use the --parents option: cd /a/b && cp --parents c/foo d Example: % cp --...
muru's user avatar
  • 77.9k
11 votes

Can I transfer files using SSH?

you can use Windows PowerShell (actually, it's OpenSSH command, it can be used on any UNIX OS too): scp -P <non-default target ssh port> "<source file>" <username>@<hostname/...
Trofogol's user avatar
  • 111
11 votes
Accepted

systemd and copy (/bin/cp): no such file or directory

When using filename globbing patterns on the command line, it's the shell that expands the globbing patterns to filenames that match them, creating a list of pathnames that is then passed on to the ...
Kusalananda's user avatar
  • 356k
11 votes

Moving millions of files to a different directory with specfic name patterns

Your command, find . -maxdepth 1 -type f | ??????????_a1ac*.jpg |xargs mv -t "/home/ubuntu/ntest" Pipes the list of all the files TO all the files! find . -maxdepth 1 -type f -name `*_a1ac*.jpg` -...
waltinator's user avatar
  • 6,876
11 votes
Accepted

How can I copy all files while excluding files with a certain pattern?

A couple of options spring to mind. rsync --dry-run -av --prune-empty-dirs --exclude '*.meta.json' --include '*.json' photos/ photos-copy/ Or if you don't have rsync (and why not!?), this will copy ...
Chris Davies's user avatar
10 votes

How to copy-merge two directories?

The answer to this question is dead simple. I have used it many times. All you need to do is ... cp -rf source_folder parent_of_dest_folder Using the specific file structure in the original question, ...
asiby's user avatar
  • 239

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