3

I'm copying files from a git repository into a virtualhost folder, although none of the following commands do overwrite existing files:

sudo cp -R /home/git/repo /var/www/website
sudo cp -R -f /home/git/repo /var/www/website

How can I make sure the cp command overwrites existing files?


Edit: Quick test, copying files from the webdir to the repo does overwrite files, so apparently something (www-data?) is locking the files in the webdir. The mount output:

~$ mount | column -t
sysfs       on  /sys                             type  sysfs       (rw,nosuid,nodev,noexec,relatime)
proc        on  /proc                            type  proc        (rw,nosuid,nodev,noexec,relatime)
udev        on  /dev                             type  devtmpfs    (rw,nosuid,relatime,size=1005964k,nr_inodes=251491,mode=755)
devpts      on  /dev/pts                         type  devpts      (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs       on  /run                             type  tmpfs       (rw,nosuid,noexec,relatime,size=204840k,mode=755)
/dev/vda1   on  /                                type  ext4        (rw,relatime,errors=remount-ro,data=ordered)
securityfs  on  /sys/kernel/security             type  securityfs  (rw,nosuid,nodev,noexec,relatime)
tmpfs       on  /dev/shm                         type  tmpfs       (rw,nosuid,nodev)
tmpfs       on  /run/lock                        type  tmpfs       (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs       on  /sys/fs/cgroup                   type  tmpfs       (ro,nosuid,nodev,noexec,mode=755)
cgroup      on  /sys/fs/cgroup/systemd           type  cgroup      (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore      on  /sys/fs/pstore                   type  pstore      (rw,nosuid,nodev,noexec,relatime)
cgroup      on  /sys/fs/cgroup/hugetlb           type  cgroup      (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup      on  /sys/fs/cgroup/freezer           type  cgroup      (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup      on  /sys/fs/cgroup/cpu,cpuacct       type  cgroup      (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup      on  /sys/fs/cgroup/memory            type  cgroup      (rw,nosuid,nodev,noexec,relatime,memory)
cgroup      on  /sys/fs/cgroup/devices           type  cgroup      (rw,nosuid,nodev,noexec,relatime,devices)
cgroup      on  /sys/fs/cgroup/net_cls,net_prio  type  cgroup      (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup      on  /sys/fs/cgroup/pids              type  cgroup      (rw,nosuid,nodev,noexec,relatime,pids)
cgroup      on  /sys/fs/cgroup/blkio             type  cgroup      (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup      on  /sys/fs/cgroup/cpuset            type  cgroup      (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup      on  /sys/fs/cgroup/perf_event        type  cgroup      (rw,nosuid,nodev,noexec,relatime,perf_event)
systemd-1   on  /proc/sys/fs/binfmt_misc         type  autofs      (rw,relatime,fd=27,pgrp=1,timeout=0,minproto=5,maxproto=5,direct)
debugfs     on  /sys/kernel/debug                type  debugfs     (rw,relatime)
mqueue      on  /dev/mqueue                      type  mqueue      (rw,relatime)
hugetlbfs   on  /dev/hugepages                   type  hugetlbfs   (rw,relatime)
fusectl     on  /sys/fs/fuse/connections         type  fusectl     (rw,relatime)
tmpfs       on  /run/user/1000                   type  tmpfs       (rw,nosuid,nodev,relatime,size=204840k,mode=700,uid=1000,gid=1000)

And:

df -h /var/www
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        30G   20G  8.9G  69% /
  • 5
    Did the above command give you an error or a message? Try adding the -v option to understand better the problem. – Giacomo Catenazzi Oct 19 '16 at 7:29
  • 1
    No errors are given. -v option shows all files being copied, but again, not overwritten. – Kurt Van den Branden Oct 19 '16 at 22:06
  • 1
    The output of mount may help a lot. I suspect a network mount that may be configured in strange ways. – grochmal Oct 20 '16 at 22:35
  • 2
    Are you using the genuine cp command (i.e. /usr/bin/cp), or is cp an alias, such as cp -n? – countermode Oct 21 '16 at 6:37
  • 3
    Reduce the problem to a single or a few files (so they fit sanely in the question), the do an ls -l on the source files, and on the destination files before and after the copy. And post the commands you entered exactly. Something like 'ls -l /home/git/repo/foo ; ls -l /var/www/website/repo/foo ; cp -a /home/git/repo /var/www/website/ ; ls -l /var/www/website/repo/foo` – ilkkachu Oct 23 '16 at 14:04
3
+25

Instead of cp I prefer use tar for big tree..

Yes, I use near never cp -r syntaxe (except for hardlinking backups, with cp -al, but it's very specific).

Syntax of tar do permit a lot of finest configuration for copying big tree, about what could be or not copied (which files, permissions, owner...) see man tar.

I prefer to be familiar with tar command:

On localhost:

 tar -cpC /path/to/source . | tar -xpC /path/to/target

(note the dot . which specify what to save).

Be familiar with tar could be usefull for remote copy:

ssh user@remotesource tar -zcpC /path/to/source . | tar -zxpC /path/to/target

or

tar -zcpC /path/to/source . | ssh user@targethost tar -zxpC /path/to/target

or even

ssh user@remotesource tar -zcpC /path/to/source . |
    ssh user@targethost tar -zxpC /path/to/target

On remote and/or different hosts, while copying users account, you may like --numeric-owner options...

  • --one-file-system stay in local file system
  • --overwrite overwrite existing files
  • -T, --files-from FILE get names to extract or create from FILE
  • -z use gzip compression

Instead of tar, why not using cpio

As imz -- Ivan Zakharyaschev commented out, cpio could do same, but not with same syntax:

cpio require list of inode (file, dir, dev, socket, fifo) that must be stored in archive to be submitted by STDIN.

But cpio offer a mode --pass-through wich let you copy file to another directory.

This make syntaxe stronger, but as we use find to build this list, we could use power of find to copy exactly what we need:

cd /path/to/source
find . \( -type f -o -type d \) ! -name '*.foo' -print0 | 
    cpio --null -pvd /path/to/destination

Used remotely, you could:

ssh user@remotesource /bin/sh <<<'cd /path/to/source;
    find . \( -type f -o -type d \) ! -name '*.foo' -print0 |
    cpio --null -o |
    gzip' |
  ssh user@targethost 'cd /path/to/destination && gunzip | cpio -id'
  • cpio can be similarly useful – imz -- Ivan Zakharyaschev Oct 23 '16 at 9:15
  • 1
    @imz--IvanZakharyaschev Hmm No: cpio require list of node to by backed up by standard input. Don't have a recursive mode. But yes, you could do same by using cpio... with some more work: ssh source /bin/sh <<<'cd /source/path && find . | cpio -o | gzip' | gunzip | cpio -i – F. Hauri Oct 23 '16 at 12:38
  • 2
    rsync can be similarly useful – user1133275 Oct 23 '16 at 13:41
  • 2
    @user1133275 yes, but tar and cpio is installed with linux standard base. rsync is a strong tool not installed by default everywhere. But yes, this could be done with rsync in very insteresting ways. (This is no more only copying, but syncing!) – F. Hauri Oct 23 '16 at 13:54
1

Since you are running this as root (using sudo), have you checked if there is an alias for cp in root's .bashrc or .profile? Something like:

alias cp="cp -i"

Try to run the commands with '\', like this:

sudo \cp -R /home/git/repo /var/www/website

The \ in \cp will make cp run without any alias.

  • 2
    I doubt it's about that, cp -i should ask about overwriting, instead of silently skipping, and -f should override -i. Also, most shells don't expand aliases when running non-interactively, in bash it's controlled by shopt expand_aliases which is set by default (only) on interactive shells. Also, .bashrc isn't read by non-interactive shells either. You can test that with doing something like bash -c 'alias ls="ls -l"; ls'. Actually I don't think sudo even runs a shell to execute a command line like that, but runs the command by itself. (try sudo alias) – ilkkachu Oct 19 '16 at 8:35
  • Yeah, you're right. cp -i should prompt before overwriting. – maulinglawns Oct 19 '16 at 9:37
  • \cp does not do the trick. The files are not overwritten. – Kurt Van den Branden Oct 19 '16 at 22:07
  • @KurtVandenBranden Sorry buddy, at this point I am out of ideas! – maulinglawns Oct 20 '16 at 6:54
  • cp -u but this only works, when the Files that you are copy are newer, than the existing. – user192526 Oct 25 '16 at 12:22
1

It sounds like you want the files to end up in /var/www/website/index.html, but with the above syntax, they'll end up in /var/www/website/repo/index.html.

You can verify if this is indeed happening by checking whether you have a /var/www/website/repo/ directory with new files.

The solution would be to use something like cp -R source/* dest/, or maybe a rsync -a source/ dest/ to be more efficient.

  • Yes. And additionally to cp -R source/* dest/ or rsync -a source/ dest/, one other could help here: cp -R source -T dest – imz -- Ivan Zakharyaschev Oct 23 '16 at 9:17
  • As stated before in one of the comments: For testing purpose, we copy everything to a subfolder of /var/www/website. – Kurt Van den Branden Oct 23 '16 at 11:02
  • @KurtVandenBranden you stated in a comment earlier you want to operate on /var/www/website/index.html. Is that not true? Please clarify your question, it's apparent that several people now don't understand what you're actually asking. – Josip Rodin Oct 23 '16 at 11:07
  • @KurtVandenBranden a good place to start would be pasting the exact output of a command that shows the previous state of the directory you want to update, and the exact output of the command that shows you the new state after the update. – Josip Rodin Oct 23 '16 at 11:13
1

Permissions and ownership may matter on your files, but if you don't need anything but 644 and 755 all by the same owner, you could just check out directly from Git:

git --git-dir=/home/git/repo/.git --work-tree=/var/www/website checkout -f HEAD

The -f flag forces the checkout, causing Git to overwrite any changes in /var/www/website with the version saved in Git.

HEAD is of course the pointer to Git's idea of the "current" commit.


You could also explore the git worktree command, but since you won't be doing development in /var/www/website, the above is likely more appropriate.

0

I recommend trying sudo cp -Rf /home/git/repo/* /var/www/website, I've always had better luck myself when copying contents of directories instead of directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.