Hot answers tagged

203

Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created mkdir /home/<new_user> cp -r /etc/skel/. /home/<new_user> This will copy all files/folder recursively from /etc/skel in to the already existing folder created on the first line.


129

Don't specify the files: cp -r /etc/skel /home/user (Note that /home/user must not exist already, or else it will create /home/user/skel.)


105

cp --preserve=links From the man page: --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,owner- ship,timestamps), if possible additional attributes: context, links, xattr, all Personally, I use cp -av for most of my heavy copying. That way, I can preserve everything - even recursively - ...


64

You can use find and cpio to do this cd /top/level/to/copy find . -name '*.txt' | cpio -pdm /path/to/destdir (-updm for overwrite destination content.)


56

The way filesystems work, a directory is not actually a folder containing files but rather a directory is a file that contains inode pointers to “child” files connected to it. Meaning, from a file system perspective, a file is a file, but a directory is just a file containing list of connected files. So from the command line perspective, doing this: $ cp ...


40

cp -Lr /usr/share/solr/ ~/solrTest Check the man page for unix commands with man cp -L, --dereference always follow symbolic links in SOURCE


31

bash itself has a good solution, it has a shell option, You can cp, mv and so on.: shopt -s dotglob # for considering dot files (turn on dot files) and shopt -u dotglob # for don't considering dot files (turn off dot files) above solution standards of bash NOTE: shopt # without argument show status of all shell options -u # abbrivation of unset -s # ...


27

This isn't really programming related, but you can use scp to do this. scp file.zip remote-box-name:/path/to/destination/file.zip If your username is different on the remote box, you will need to prefix it: scp file.zip yourusername@remotebox:/path/to/destination/file.zip And to retrieve a file you could do this: scp remotebox:/path/to/destination/...


25

No need for cat at all: xargs -a list.txt cp -t new_folder Or using long options: xargs --arg-file=list.txt cp --target-directory=new_folder Here are some shell versions: Bash: for file in $(<list.txt); do cp "$file" new_folder; done or cp $(<list.txt) new_folder sh (or Bash): # still no cat! while read -r line; do for file in $line; do ...


24

Try using xargs: cat list.txt | xargs -J % cp % new_folder Update: I did this on OS X which has an different version than GNU/Linux versions. The xargs which comes from GNU findutils doesn't have -J but it has -I which is similar (as Dennis Williamson pointed out in a comment). The OS X version of xargs has both -I and -J which have slightly different ...


20

"." refers to the current directory, so cp /path/to/file . will do what you want.


18

If the links contain relative paths, then, copying the link will not adjust the relative path. Use readlink, with the switch -f to follow recursively, in order to get the absolute path of the link. For example: ln -s $(readlink -f old/dir/oldlink) new/dir/newlink If preserving the relative paths is what you want, than the option -P of cp, as said by ...


18

It's very true that this is the behavior we want nearly all the time. This doesn't necessarily mean, though, that copying recursively should be the default behaviour. I think the reasons cp acts as it does have roots in Unix philosophy. Unix favors programs that do one thing and do it well, as well as programs that are simple in both interface and ...


16

Just as the man page says, use -P.


16

One significant difference is that cp truncates the destination file and starts copying data from the source into the destination file. install, on the other hand, removes the destination file first. This is significant because if the destination file is already in use, bad things could happen to whomever is using that file in case you cp a new file on top ...


16

No. The cp command does not possess the capability to process any of its arguments as regular expressions. Even wildcards are not handled by it (or most executables); rather they are handled by the shell. cp test/* test2/ is actually expanded by bash, and all that cp really sees for its arguments are cp test/file1 test/file2 test/file3 test2/ (or whatever ...


15

This would be significantly easier using rsync with its --exclude switch. rsync -av --exclude='*.FOO' --exclude='*.BAR' --exclude='*.ZIM' /source /dest The -v switch will provide verbose output on which files are being synchronised.


14

Use rsync: rsync -rtv source_folder/ destination_folder/


14

Use brace expansion cp /home/myuser/really/big/file/here/and/there.png{,.bkp}


14

Using the update option (-u) with cp should do it for you. http://beginnerlinuxtutorial.com/help-tutorial/basic-linux-commands/cp-linux-copy-command/


13

You can use rsync to do this, the command I use is rsync -tr "folder to copy from" "folder to copy to" e.g. rsync -tr /home/me/stuff/* /home/me/otherstuff/


12

It is easy enough to install cp from MacPorts, however, if you don't want to, or want to create a portable script, then you have three options: rsync rsync --archive --link-dest=../yesterdays_backup backups/yesterdays_backup\ backups/todays_backup cpio mkdir backups/todays_backup cd backups/yesterdays_backup find . -print | cpio -p -al ../...


12

In terminal, type in cp -Rp /Volumes/<source-volume>/<source-user>/<source-folder>/ /Volumes/<destination-volume>/<destination-folder>/ Destination folder should be a new folder you are creating. If you get info on the new folder after running this you can see the folder size increase. Example cp -Rp /Volumes/Macintosh ...


10

mkdir ~/MyAssignments/MyWorks


10

It is also possible to do this with good old cp: Thanks to srcspider for reminding me to use -T! cp -ruT old-dir new-dir


9

I used the following to duplicate a really large directory. All symbolic links were preserved, the copy was done recursively and I was able to have some visual feedback of the process: cp -Prv /sourcer_dir/* /target_dir


9

Written the script to use find, cpio and pv to accomplish the task. The rate can be limited. http://vi-server.org/vi/bin/cppv Mirroring it here: #!/bin/bash set -e if [ $# -lt 2 ]; then echo "cppv - copy files with progress bar and rate limiting ability" echo "Usage: cppv source_file[s] destination_file_or_directory" echo "No other non-...


9

Based on post by Gilles I tested the following list: #!/bin/sh touch questionmark? touch less< touch less\< touch more\> touch backslash\\ touch colon: touch asterisk\* touch pipe\| touch inch\" touch carret\^ touch comma, touch semicolon\; touch plus+ touch equals= touch lbracket[ touch rbracket] touch quote\' I tried to copy that onto Android ...


9

I don't get a prompt to overwrite. Why not? Because you're supposed to know what you're doing. Especially as root, you can overwrite pretty much anything, so pay attention to that. Use the -i option for cp to get a prompt before overwriting existing files. If you always want to be reminded of this, consider creating an alias for cp to cp -i. The ...


9

In addition to calling /bin/cp, you could do one of: \cp -f ... command cp -f ... However, I agree that you should not get accustomed to using an alias like cp -i or rm -i -- if you sit down at a different shell, you won't have the safety net you've become dependent on.



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