Activities

May 2013
M T W T F S S
« Apr   Jun »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Linux : How do I overwrite the files using cp command

Most of the time, you may need to overwrite the existing files in the huge folder whenever a new release or updates are published. And this may occur certain situation and I got the prompt to delele each files inside that folder which contains thousands of files. I’m lazy to press “y” to delete all the file till it’s end. When I find an answer in google, it says you need to use “f” force option to over write the files. I applied it but still I’m getting that prompt for confirmation. ahhhhhh !! Strange .. Isn’t it. What I normally do is,

a. Rename that folder and create an empty folder to copy. This is not a straight forward action but works fine without any delay.

 [root@rc-090 html]# cp -r public_html_new/*  public_html/
cp: overwrite `public_html/build.dev.properties'? y
cp: overwrite `public_html/build.xml'? y
cp: overwrite `public_html/css/video.css'? y
cp: overwrite `public_html/css/event.css'? y
cp: overwrite `public_html/css/400.css'? y
cp: overwrite `public_html/css/page.css'?

So I want to know the reason of this prompt even though I uses force [-f] option in cp command.

Reason

Most of the Linux distribution has set certain alias towards a command which execute certain harm/delete actions or it may cause a process initiation which clear all the sensitive information from the disk. So it has to be validate twice before executing it. This was done for additional file safety and be warn users what they are going to perform.

You may see the existing command alias set for your platform by executing the command “alias” without any argument

 [root@rc-127 installation]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@rc-127 installation]#

Pls note that the command “cp” has the alias name cp -i which cause you to get a promt up on each file deletion. This is the root cause of getting prompt even though you applied “-f” option 🙂 🙂

Solution/hack

You will not use the EXACT cp command to replace the files. Here is the great hack, you have to use the exact path where the cp command resides.

[root@rc-090 html]# whereis cp
cp: /bin/cp /usr/share/man/man1/cp.1.gz /usr/share/man/man1p/cp.1p.gz
[root@rc-090 html]# /bin/cp -rf /var/www/html/public_html_new/* /var/www/html/public_html/
[root@rc-090 html]#

Cheers !!

How do I set new command alias

If you want to set an alias to watch the top CPU consuming process using the command top alias top_process=”top | head -n 15″. You need to set alias as shown below

 [root@rc-025 Liju]#alias top_process="top | head -n 15"
[root@rc-025 Liju]# top_process
top - 15:10:05 up 14 days,  2:56,  2 users,  load average: 10.24, 10.36, 10.34
Tasks: 268 total,   1 running, 264 sleeping,   0 stopped,   3 zombie
Cpu(s): 22.8%us,  0.4%sy,  0.0%ni, 76.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4138544k total,  3983676k used,   154868k free,    78028k buffers
Swap:  4096532k total,   143116k used,  3953416k free,  3011296k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3524 root      18   0  285m  82m  11m S 777.8  2.0 320367:55 webAccess
 5008 root      16   0  198m  74m  20m S  3.9  1.9 588:19.60 firefox
 5208 root       5 -10  538m 282m 272m S  3.9  7.0 217:09.85 vmware-vmx
 5242 root       0 -20     0    0    0 S  2.0  0.0  48:07.39 vmware-rtc
    1 root      15   0  2064  620  536 S  0.0  0.0   0:01.14 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.05 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>