Recipe 9.15. Copying, Moving, and Renaming Files and Directories

 < Day Day Up > 

9.15.1 Problem

You've got directories, you've got files. How do you get the files into the directories? How do you change a file's name? And how do you make a copy?

9.15.2 Solution

Use the cp and mv commands.

This command copies two files from the current working directory into the ~/images2 directory:

$ cp -v navbar.gif redheart.gif  ~/images2 `navbar.gif ' -> `/home/terri/images2/navbar.gif' `redheart.gif ' -> `/home/terri/images2/redheart.gif'

If you're overwriting files, you might want to use the -b flag to create backups of the old files in the destination directory:

$ cp -bv icon-zip.gif  main.gif ~/data2 `icon-zip.gif' -> `/home/terri/data2/icon-zip.gif' (backup: `/home/terri/data2/icon-zip.gif~') `main.gif' -> `/home/terri/data2/main.gif' (backup: `/home/terri/data2/main.gif~')

What if you need to preserve the full filepath? Use the parents flag:

$ cp -v  parents  ~/homes/images/kitchen.jpg ~/data2 '/home/terri/homes/images/kitchen.jpg' ->  '/home/terri/data2/homes/images/kitchen.jpg'

Use the -s flag to create soft links to files, instead of copying the files:

$ cp -s navbar.gif  redheart.gif  ~/images2

Copy a directory and all of its contents with the -r flag:

$ cp -rv  ~/homes/images/  /shared/archives

Moving and renaming files are done with the mv command. To move two files to another directory, use:

$ mv -v about.gif  arrow.gif  ~/data2 `about.gif' -> `/home/terri/data2/about.gif' `arrow.gif' -> `/home/terri/data2/arrow.gif'

To rename a file, use:

$ mv -v downloads.gif  email.gif `downloads.gif' -> `email.gif'

9.15.3 Discussion

A graphical file manager, such as Nautilus, Konqueror, Midnight Commander, Gentoo file manager, gmc, or Rox Filer, often makes chores like these go faster and easier.

9.15.4 See Also

  • mv(1), cp(1)

  • Linux in a Nutshell

     < Day Day Up > 


    Linux Cookbook
    Linux Cookbook
    ISBN: 0596006403
    EAN: 2147483647
    Year: 2004
    Pages: 434

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