As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

12 votes

cp -p will preserve times for some files but not all?

On Unix the timestamps cannot be preserved on the files the user does not own. This is why the following command fails if file1 and file2 are owned by different users in case file2 already exists: ...
krisFR's user avatar
  • 13.2k
11 votes

Continue aborted cp

In case the aborted cp was a recursive copy, you might want to resume with rsync including the option --recursive. Example Aborted copy command: cp -r source-directory destination-directory Let ...
Jan-Philip Gehrcke's user avatar
5 votes

Linux (mv or cp) specific files from a text list of files?

rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/ Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
Paul Wenzel's user avatar
4 votes

cp without overwriting destination permissions

By default, GNU cp (version 8.32) will NOT overwrite destination permissions, so the question is moot: % ls -li total 8,192 19392015 -rwxrwxrwx 1 ravi ravi 4 Jan 3 16:54 bar* 19392014 -rw-r--r-- 1 ...
Tom Hale's user avatar
  • 1,085
3 votes

UNIX Copy/Move files in same directory preserving timestamp

Only root can change ownership of files, so user3 will need to be able to use sudo to change ownership of files belonging to user1 and user2. mv will always preserve all metadata, including ...
wurtel's user avatar
  • 3,864
3 votes

How to move and partially rename files on linux

This worked for me - could also be done with find instead of ls: #touch foo-1.3.{1..10}.war #ls -1 foo-1.3.10.war foo-1.3.1.war foo-1.3.2.war foo-1.3.3.war foo-1.3.4.war foo-1.3.5.war ...
Guz's user avatar
  • 31
3 votes

Copying between lzo compressed BtrFS's: de/re-compressing?

Not really, and it comes down to syscalls. Have an example: open ("tuppence", O_RDONLY) = 3 fstat (3, {st_mode=S_IFREG|0644, st_size=15, ...}) = 0 open ("/tmp/tuppence"...
sysadmin1138's user avatar
  • 133k
3 votes

mv fails but cp succeeds on nfs mount

Are your password / group databases in sync between client and server? Do you see any files on nfs mounted directory on nfs client as owned by nobody nobody Can you post output of nfsidmap -d from ...
Dmitry Zayats's user avatar
3 votes

cp -p will preserve times for some files but not all?

I know this is old, but I was having the same issue of trying to copy over cifs shares. I have several commands set up in sudoers with the NOPASSWD option and one is cp. If I do sudo cp -r from.file ...
user999684's user avatar
3 votes

copy files, get file names from a list

Xargs is nice/fast/etc but isn't easy to understand for novices in shell scripting. I think that's will be much more readable (and more clean than for with subshell). #!/bin/bash while read ...
strizhechenko's user avatar
2 votes

copy files, get file names from a list

You can do this IFS=$'\n' ; for file in `cat file_list.txt` ; do cp $file /path/ ; done Explanation Set internal field separator to newline Read file_list.txt and loop through each line assigning ...
Ryan Babchishin's user avatar
2 votes
Accepted

Is it OK to copy files installed with rpm to another location?

Doing it that way negates most the the advantages of using packages. Fix the package to put its files where you want them. That way if you use rpm to verify files it will checksum the files in use. ...
John Mahowald's user avatar
2 votes
Accepted

using find, exec and cp to copy specific files while preserving directory path

You have a race condition: since temp/s is in find's search path, find is listing files, while the spawned cps are adding new files. You have to exclude the sub-directory while running find: find . -...
Piotr P. Karwasz's user avatar
2 votes
Accepted

How to move and partially rename files on linux

Using just bash and commands in most common distributions by default: find . -name "foo-*.war" -exec sh -c 'mv "$1" "other_folder/foo-dev-${1#foo-}"' _ {} \;
LTPCGO's user avatar
  • 508
2 votes
Accepted

Safest way to move a mount point that is a parent of other mount points on Linux to a new physical device

If you bind mount a directory, then the submounts will not be bound. So create a dummy directory somewhere, and do mount --bind /var/lib/kubelet/ /mnt/tmptmp_dummy/ Then the contents of the /var/lib/...
Lacek's user avatar
  • 7,153
1 vote
Accepted

Making a copy of all .acignore->.gitignore in place, recursively

This should do the trick: find ./ -name '.acignore' | sed 's/.acignore//g' | xargs -I {} cp {}.acignore {}.gitignore It's "./" if you are inside the "c" directory. If you are ...
Lorem ipsum's user avatar
1 vote

How to move and partially rename files on linux

You could use Perl implementation of rename program (there is another program called rename which is a part of util-linux package) like that: $ touch foo-1.3.{1..10}.war $ ls -l total 0 -rw-r--r-- 1 ...
Arkadiusz Drabczyk's user avatar
1 vote

Copying files in Linux keeps full path

To copy folder world to ~/backups/backup_2019-05-04: backupdir=~/backups/backup_$(date +%F) mkdir -p "$backupdir" cp -av ~/world "$backupdir" I used flag -a for archive mode which includes -R. You ...
Freddy's user avatar
  • 2,019
1 vote

what is the best approach to copy /var content

Instead of cp I suggest rsync -aqxP /var/* /var_copy ...but your plan will break many things (rm -rf /var "under" a running system). Better to copy things modify /etc/fstab reboot ... as ...
V-Mark's user avatar
  • 111
1 vote
Accepted

Create copy of file in the same location

cp /very/long/file/path{,.bak} vim !$ or cpvi() { name=$1 cp "${name}" "${name}.bak" $EDITOR "${name}.bak" } cpvi /very/long/file/path
ptman's user avatar
  • 28.1k
1 vote
Accepted

cp is too slow, same with rsync (I assume) - is there a way to share files in two locations?

You could copy using reflinks `cp --reflink=always', which will create a differential file that references a static original. If you do this, you'll probably want to make the original immutable so the ...
Spooler's user avatar
  • 7,036
1 vote

ACL mask not updated when cp a directory with g=r-x to directory with default:mask::rwx and default:group::rwx

To copy ACLs from a directory to another, you had to instruct cp to do that, using the -a switch. In other words, use cp -a src dst EDIT The inherited ACLs will not grant permissions which the file ...
shodanshok's user avatar
  • 46.3k
1 vote
Accepted

copy files, get file names from a list

cat file_list.txt | tr '\n' '\0' | xargs -r0i cp -t /path/to/dest/ "{}"
Anubioz's user avatar
  • 3,667
1 vote
Accepted

Copying between lzo compressed BtrFS's: de/re-compressing?

As @sysadmin1138 so well illustrated, this problem is unavoidable if using cp/rsync/send-receive across file-systems; but there is a way to avoid it under certain circumstances. If you use a seed ...
Diagon's user avatar
  • 246
1 vote
Accepted

Most side effect-free way to archive a large file while it is being modified

You can do this with significantly reduced I/O load by not doing the copy+truncate. Instead, rename the file, then, if the process holds the log file descriptor open, do whatever is required to cause ...
womble's user avatar
  • 96.1k
1 vote

cpio VS tar and cp

I understand from the comments and other background that cpio is less ubiquitous now and inconsistent between versions. But cpio has one advantage I recently found invaluable when dealing with a large ...
mdisibio's user avatar
  • 111
1 vote

Turning off cp (copy) command's interactive mode (cp : overwrite ?)

Yea the reason for cp command to be interactive because in many common shells by default copy comes with alias as cp -i this can be found by running command as follows : alias cp output will be ...
SAGAR Nair's user avatar
1 vote

Recursively copying hidden files - Linux

Much better answers here; https://superuser.com/questions/61611/how-to-copy-with-cp-to-include-hidden-files-and-hidden-directories-and-their-con It describes for example using shopt for a native bash ...
Roel Van de Paar's user avatar

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