Move and Rename Files


mv

So cp copies files. That seems simple enough, but what about moving files? In the similar vein of removing unnecessary vowels in commands, we have the mv command, short for move.

You're going to notice quickly that most of the options you learned for cp are quite similar to those used by mv. This shouldn't surprise you; after all, mv in reality performs a cp -a, and then removes the file after it's been successfully copied.

At its simplest, mv moves a file from one location to another on your file system.

$ pwd /home/scott/libby $ ls libby_arrowrock.jpg  libby_bak.jpg  libby.jpg  libby_on_couch.jpg  on_floor $ ls ~/pictures/dogs libby_on_floor_01.jpg libby_on_floor_03.jpg libby_on_floor_02.jpg libby_on_floor_04.jpg $ mv ~/pictures/dogs/libby_on_floor_04.jpg     libby_on_floor_04.jpg $ ls libby_arrowrock.jpg libby.jpg  libby_on_floor_04.jpg libby_bak.jpg       libby_on_couch.jpg on_floor $ ls ~/pictures/dogs libby_on_floor_01.jpg libby_on_floor_02.jpg    libby_on_floor_03.jpg 


Just as you did with cp, you can use a dot to represent the current directory if you don't feel like typing out the filename again.

$ pwd /home/scott/libby $ ls arrowrock.jpg libby.jpg on_couch.jpg on_floor $ ls ~/pictures/dogs on_floor_01.jpg on_floor_03.jpg on_floor_02.jpg on_floor_04.jpg $ mv ~/pictures/dogs/on_floor_04.jpg . $ ls arrowrock.jpg  on_couch.jpg on_floor_04.jpg libby.jpg      on_floor $ ls ~/pictures/dogs on_floor_01.jpg on_floor_02.jpg on_floor_03.jpg 


If you're moving a file into a directory and you want to keep the same filename, you just need to specify the directory. The filename stays the same.

$ pwd /home/scott/libby $ ls arrowrock.jpg on_couch.jpg on_floor_04.jpg libby.jpg     on_floor $ ls on_floor on_floor_01.jpg on_floor_02.jpg on_floor_03.jpg $ mv on_floor_04.jpg on_floor $ ls arrowrock.jpg on_couch.jpg on_floor_04.jpg libby.jpg     on_floor $ ls on_floor on_floor_01.jpg on_floor_03.jpg on_floor_02.jpg on_floor_04.jpg 


In order to visually communicate that on_floor is in fact a directory, it's a good idea to use a / at the end of the directory into which you're moving files, like this: mv libby_on_floor_04.jpg on_floor/. If on_floor is not a directory, your mv won't work, thus preventing you from accidentally writing over a file.

Note

The cp and mv commands use many of the same options, which work the same way for either command. For instance, -v copies and moves verbosely, and -i copies and moves interactively.




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