Stop Yourself from Copying over Important Files


cp -i

The previous example demonstrates something important about cp that you need to know. In the "Copy Files Using Wildcards" example, you copied three libby_on_floor pictures into the libby directory; in the previous example, you copied the same three libby_on_floor images into the libby directory again. You copied over files that already existed, but cp didn't warn you, which is how Linux works: It assumes you know what you're doing, so it doesn't warn you about things like overwriting your files ... unless you ask it to do so. If you want to be forewarned before overwriting a file using the cp command, use the -i option (or --interactive). If you tried once again to copy the same files but used the -i option this time, you'd get a different result.

$ pwd /home/scott/libby $ ls ~/pix arrowrock.jpg   by_pool_03.jpg   on_floor_03.jpg by_pool_01.jpg  on_floor_01.jpg  on_floor_04.jpg by_pool_02.jpg  on_floor_02.jpg$ ls arrowrock.jpg   libby_on_couch.jpg on_floor_02.jpg libby.jpg       on_floor_01.jpg    on_floor_03.jpg $ cp -i ~/pix/on_floor_0[1-3].jpg . cp: overwrite './on_floor_01.jpg'? 


Bam! The cp command stops in its tracks to ask you if you want to overwrite the first file it's trying to copy, libby_on_floor_01.jpg. If you want to go ahead and copy the file, enter y; otherwise, enter n. If you do choose n, that doesn't mean the cp stops completely; instead, you're next asked about the next file, and the next, and so on. The only way to give an n to whole process is to cancel the whole process by pressing Ctrl+c. Similarly, there's no way to say yes to every question ahead of time, so if you want to copy 1,000 files over 1,000 other files with the same name and also intend to use the -i option, make sure you have plenty of time to sit and interact with your shell, because you're going to get asked 1,000 times if you really want to overwrite your files.

Caution

For normal users, -i usually isn't necessary. For root users, however, it's darn near essential, as a root user can errantly copy over a key system file, causing a disaster. For that reason, it's a good idea to create an alias in the root user's .bashrc file, making sure that cp is really cp -i instead.

alias cp= 'cp -i' 





Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net