• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Saturday, March 02, 2013

    Forcefully overwriting while copying files

    Many a times it happens you are trying to copy or move a large number of directories or files from oney" every time but it can be adjusted by the number of file is 2-3 but what if the number of files you are trying to copy is more than 100 then you cannot press "y" every time to overwrite location to another location and every time you get a prompt of "overwrite" as already a file with the new file name exists. It becomes really very hectic to press "

    Well in the post I will show you a simple step to suppress the overwrite prompt.

    For example i am trying to copy some files in a folder but these files already exists and I want to override them
    # cp -r /etc/ Folder/
    cp: overwrite `Folder/etc/rc5.d'? y
    cp: overwrite `Folder/etc/ggz.modules.d/kdegames'? y
    cp: overwrite `Folder/etc/idmapd.conf'? y
    cp: overwrite `Folder/etc/bonobo-activation/bonobo-activation-config.xml'? y
    cp: overwrite `Folder/etc/csh.cshrc'? y
    cp: overwrite `Folder/etc/group-'?
    So you see every time I have to press y to accept.

    Firstly you can try to use -f switch along with the copy command but this won't work in all the cases
    # cp -rvf /etc/ Folder/
    cp: overwrite `Folder/etc/rc5.d'? y
    removed `Folder/etc/rc5.d'
    `/etc/rc5.d' -> `Folder/etc/rc5.d'
    cp: overwrite `Folder/etc/ggz.modules.d/kdegames'? y
    `/etc/ggz.modules.d/kdegames' -> `Folder/etc/ggz.modules.d/kdegames'
    cp: overwrite `Folder/etc/idmapd.conf'? y
    `/etc/idmapd.conf' -> `Folder/etc/idmapd.conf'
    `/etc/bonobo-activation-config.xml' -> `Folder/etc/bonobo-activation- config.xml'
    cp: overwrite `Folder/etc/csh.cshrc'? y
    Now as you see above it is still prompting me for overwrite

    Solution:

    In Unix machines generally an alias entry is created by default for some of the commands including "cp" which you can verify the same using this command
    # alias
    alias cp='cp -i'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

    Now as you can see there is an alias entry for cp command as 'cp -i' and if you look out for
    # cp --help
    -i, --interactive            prompt before overwrite (overrides a previous -n option)

    So as you can see and understand why it was asking for overwrite each time. So the only thing you need to do is unalias the entry
    # unalias cp

    Now let us try to copy
    # cp -rvf /etc /Folder
    `/etc/sfcb/file.pem' -> `Folder/etc/sfcb/file.pem'
    `/etc/sfcb/sfcb.cfg' -> `Folder/etc/sfcb/sfcb.cfg'
    `/etc/sfcb/server.pem' -> `Folder/etc/sfcb/server.pem'
    `/etc/sfcb/client.pem' -> `Folder/etc/sfcb/client.pem'
    `/etc/mke2fs.conf' -> `Folder/etc/mke2fs.conf'
    `/etc/sysctl.conf' -> `Folder/etc/sysctl.conf'
    So the files are getting copied without any prompt.

    But the unalias command we used is just temporary as next time you change the terminal again the same problem will come up so to permanently unalias any command
    # vi .bashrc
    # .bashrc
    # User specific aliases and functions
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    . /etc/bashrc
    fi
    Remove the entry for cp command and save the file. This will permanently remove the alias entry for cp.

    Follow the below links for more tutorials:


    4 comments:

    1. I was searching from a long time looking for a solution to this. It really becomes head ache at times when copying no. of files.

      You tips worked like a charm.

      ReplyDelete
    2. You can also run \cp, and that will unalias the existing cp alias and won't prompt.

      ReplyDelete
      Replies
      1. Thanks for the info..that was new to me as well.

        Delete