Полезная информация

Next Up Previous Contents Index
Copying, Moving and Renaming Files and Directories

3.5 Copying, Moving and Renaming Files and Directories

By now, you've learned a little about the structure of the filesystem; and you've learned how to create files and directories.

But just because you know how to create files and directories doesn't mean that you're stuck with the changes you've made. What if you want to rename and/or move files and directories?

Let's start with the copy command.

3.5.1 Copying Files

Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you're copying, moving, or deleting files and directories.

Basically, the copy command is not much more complex than typing:

cp <source> <destination>

so to copy the file sneakers.txt to the directory tigger in your login directory, just type:

cp sneakers.txt tigger

Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with cp. Our login directory is the parent of the directory tigger; meaning that tigger is one directory down from ours.

Read the cp man page (man cp) for a full list of the options available with cp. But among the options you can use with cp are:

Just by using cp alone, you won't see much when the command is executed. Using an option, such as -i, can make the process a little more useful, because if you want to copy a file to a location that already has a file with the same name, you'll be asked first if you really want to overwrite -- meaning replace -- the file that's already there.

  • Tip: Remember that among your options is -f (force), which can overwrite files without asking you if you're certain. Make sure, when you use the force option, that you really want to overwrite a file.

  • Now that we have the file sneakers.txt in the tigger directory, let's use cp -i to copy the file again to the same location.

    [billy@localhost billy] cp -i sneakers.txt tigger
    cp: overwrite 'tigger/sneakers.txt'?
    

    If we want to overwrite the file that's already there, we can press [Y] and then [Enter]. If we think we don't want to overwrite the file, now's the time to press [N] and [Enter].

    3.5.2 Moving Files

    To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp.

    Common options available with mv include:

    If you want to move a file out of your home directory and into another directory, you would type:

    mv sneakers.txt tigger
    

    or, mv sneakers.txt /home/billy /home/billy/tigger using absolute pathnames.

    3.5.3 Renaming Files

    Actually, we've already covered half of renaming, because when you copy or move files, you can also rename.

    To copy the file sneakers.txt from our login directory to our tigger subdirectory, just type:

    cp  sneakers.txt tigger
    

    To copy and rename that file from sneakers.txt to piglet.txt, type:

    cp sneakers.txt tigger/piglet.txt
    

    To move and rename the file, just substitute mv for cp in the above example.

    If you cd to tigger and use ls, you'll see the file piglet.txt.

    If you just want to rename the file and keep its location, just mv in your current directory:

    mv  sneakers.txt piglet.txt
    

    3.5.4 Deleting Files and Directories

    We talked about creating files with the touch command and by using redirection in Chapter 2. And we created the directory tigger using mkdir.

    But we haven't discussed how to delete files and directories.

    Deleting files and directories with the rm command (man rm) is a straightforward process.

    Let's take our new file piglet.txt, and delete it from the tigger directory with the rm command:

    rm piglet.txt
    

    What happens if we didn't really want to get rid of it? Too late! Again, that's where the -i (interactive) option comes in handy, because with it, we have the chance to think about whether we really want to toss the file.

    [billy@localhost billy] rm -i piglet.txt
    rm: remove 'piglet.txt'?
    

    You can also delete files using the wildcard *, but be careful, because you can easily delete files you didn't intend to throw away.

    To remove a file using a wildcard, you would type:

    rm pig*
    

    You can also remove more than one file in one command, as in:

    rm piglet.txt sneakers.txt
    

    Options for removing files -- and directories -- include:

    To remove directories with rm, you must specify the -r option.

    For example, if you want to recursively remove the directory tigger you would type:

    rm -r tigger
    

    And if you want to combine options, such as forcing a recursive deletion, you can type:

    rm -rf tigger
    

  • Tip: Be careful! rm is a powerful command, and can delete your entire system! If you're root and you type the simple command rm -rf / you're sunk -- like a snake eating its tail, the command will recursively remove everything on your system.

  • The safer alternative to using rm for removing directories is the rmdir command. With this command, you won't be allowed to use recursive deletions, so a directory which has files in it won't be deleted.

    Read the rmdir man page by typing man rmdir to find out more about the command.

    3.5.5 Time to Learn More

    So far, you've learned to become familiar with how to create accounts, using passwords, your filesystem and more.

    But what we've covered is just the tip of the iceberg. There are books, websites, and newsgroups (Linux users are second to none when it comes to helping new users) to help you find out more about your new system.

    You can begin with the next chapter, in which you'll find pointers to other documentation, as well as additional post-installation configuration.


    Next Up Previous Contents Index