Hot answers tagged

522

sudo cp -rp /home/my_home /media/backup/my_home From cp manpage: -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all


490

The syntax for scp is: If you are on the computer from which you want to send file to a remote computer: scp /file/to/send username@remote:/where/to/put Here the remote can be a FQDN or an IP address. On the other hand if you are on the computer wanting to receive file from a remote computer: scp username@remote:/file/to/send /where/to/put scp can also ...


233

Recursive means that cp copies the contents of directories, and if a directory has subdirectories they are copied (recursively) too. Without -R, the cp command skips directories. -r is identical with -R on Linux, it differs in some edge cases on some other unix variants. By default, cp creates a new file which has the same content as the old file, and the ...


225

rsync has a flag called progress2 which shows the overall percentage: rsync --info=progress2 source dest


158

Is there any reason why people resist using find's -exec? It's very handy. find . -name '*.csv' -exec cp --parents \{\} /target \; Know your tools. ;-)


113

This isn't working because the command date returns a string with spaces in it. $ date Wed Oct 16 19:20:51 EDT 2013 If you truly want filenames like that you'll need to wrap that string in quotes. $ touch "foo.backup.$(date)" $ ll foo* -rw-rw-r-- 1 saml saml 0 Oct 16 19:22 foo.backup.Wed Oct 16 19:22:29 EDT 2013 You're probably thinking of a different ...


109

If you allow other tools than cp it's surely possible. For a single file you can use pv. It's a small tool providing nice statistics. pv inputfile > outputfile If you have multiple files or directories you can use tar: tar c sourceDirectory | pv | tar x -C destinationDirectory You can wrap it in a shell function. It's less to type and you get ...


102

You can also use rsync. sudo rsync -a /home/my_home/ /media/backup/my_home/ From the rsync manpage: -a, --archive This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the ...


96

Are you using a 64-bit version of Linux with a lot of memory? In that case the problem could be that Linux can lock for minutes on big writes on slow devices like for example SD cards or USB sticks. It's a known bug that should be fixed in newer kernels. See http://lwn.net/Articles/572911/ Workaround: as root issue: echo $((16*1024*1024)) > /proc/...


88

advanced cp cp -r /home/username/A/. /usr/lib/B/ This is especially great because it works no matter whether the target directory already exists. shell globbing If there are not too many objects in the directory then you can use shell globbing: mkdir -p /usr/lib/B/ shopt -s dotglob cp -r /home/username/A/* /usr/lib/B/ rsync rsync -a /home/username/A/ /...


82

You can do this with GNU find and GNU mv: find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' + Basically, the way that works if that find goes through the entire directory tree and for each file (-type f) that is not in the top-level directory (-mindepth 2), it runs a mv to move it to the directory you want (-exec mv … +). The -t argument to mv lets ...


77

You can pass --remove-source-files to rsync to move files instead of copying them. But in your case, there's no point in using rsync, since the destination is empty. A plain mv will do the job as fast as possible. In your case, what could make a difference to performance is the choice of network protocol, if you have a choice among NFS, Samba, sshfs, sftp, ...


75

Copying folders into another folder (folder in folder): cp -r css images js backups ar/ Note: this is different from copying just the contents themselves(contents of folders in folder): cp -r css/ images/ js/ backups/ ar/


70

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, --archive same as -dR --preserve=all


69

With symlinks, tools have two things they can do: Treat the symlink as a symlink ("preserving its nature"), or Treat the symlink as the type of file that it points to. Saying that -H "preserves its nature" is not a contradiction. Consider the alternative. If you use -L, any symlinks cp finds will be opened, and their contents copied to the target file ...


68

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 itself has to be altered. When copying directories, the data for each and every file needs to be duplicated. This involves reading all the source data and ...


67

Use cp -P (capital P) to never traverse any symbolic link and copy the symbolic link instead. This can be combined with other options such as -R to copy a directory hierarchy — cp -RL traverses all symbolic links to directories, cp -RP copies all symbolic links as such. cp -R might do one or the other depending on the unix variants; GNU cp (as found on ...


67

rsync works the best for showing the progress during the copying progress. ex: rsync -avh --progress sourceDirectory destinationDirectory


67

Brace expansion will get the job done. man bash and search for Brace Expansion. cp *.{txt,jpg,png} destination/ EDIT: In keeping with the OP's request, the command above was missing the verbose option: cp -v *.{txt,jpg,png} destination/


60

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 system, not any of the other file systems mounted. See the cp -x documentation (although its distinction is subtle).


58

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 portable solution, see answer using pax and cpio by Stéphane Chazelas


57

My version of unzip has a -j option to not create any directory. So unzip -j /path/to/file.zip Will extract all the files into the current directory without restoring the directory structure stored in the zip file. If you want to only remove one level of directories from the archive, (extract myarchive/dir/file as dir/file, not file), you could use ...


56

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 remembers that it’s supposed to copy to a new file. When the time comes to open the destination file, it opens it with flags which enforce its creation, O_CREAT ...


55

You could also use rsync for this. $ rsync -a --prune-empty-dirs --include '*/' --include '*.csv' --exclude '*' source/ target/ If you want to keep empty directories from the source tree, skip the --prune-empty-dirs option: $ rsync -a --include '*/' --include '*.csv' --exclude '*' source/ target/ If you do not want symlinks, modification dates, file ...


55

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; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents. For almost any instance of bbb this gives the undesirable behaviour that ...


54

This is not a bug in the cp command. When you enter cp *.pdf, cp never sees the actual wildcards because the wildcards are expanded by bash, not by cp. How will cp know that you have entered only one argument? This is a side effect of bash wildcards and cannot be called a bug.


53

%CPU should be low during a copy. The CPU tells the disk controller "grab data from sectors X–Y into memory buffer at Z". Then it goes and does something else (or sleep, if there is nothing else). The hardware triggers an interrupt when the data is in memory. Then the CPU has to copy it a few times, and tells the network card "transmit packets at memory ...


53

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 and was written by a previous step. Otherwise, if dest_file exists, the following steps shall be taken: 3.a.i. If the -i option is in effect, the cp ...


52

install not only copies files but also changes its ownership and permissions and optionally removes debugging symbols from executables. It combines cp with chown, chmod and strip. It's a convenient higher-level tool to that accomplishes a common sequence of elementary tasks. An advantage of install over cp for installing executables is that if the target ...


48

“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, possibly earlier. Csh had set noclobber to configure > to refuse to overwrite an existing file (later set -o noclobber in ksh93 and other sh-style shells). When GNU ...


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