-->
Previous Table of Contents Next


When you use cp, you are actually making a second physical copy of your file and placing it on your disk. This can be slower (although for small files, you won’t notice any difference), and it causes a bit more wear and tear on your computer. Don’t make copies of files when all you really want to do is move them!

Moving and Copying with Wildcards

If you have 20 files in a directory, and you want to copy them to another directory, it would be very tedious to use the cp command on each one. Fortunately, you can use the wildcards * and ? to copy more than one file at a time.

If you want to move or copy all files in a directory, use the wildcard *:


darkstar:~$ cp * /tmp

This command copies every file in your current directory to the directory /tmp.

You can use * along with other characters to match only certain files. For instance, suppose you have a directory that contains the files book1, book_idea, book-chapter-1, and poem.book. To copy just the first three files, you can type cp book* /tmp. When you type book*, you are asking Linux to match all files whose names start with book. In this case, poem.book does not start with book, so there is no way book* can match it. (Note that if your filename were book.poem, book* would match it.)

As you saw at the outset, mv and cp are very simple commands. It’s specifying the files that is the complicated part! If things still seem confusing, don’t worry. Even experts sometimes mess up “simple” moves and copies. Follow the examples and try any different ways you can think of. There is a definite logic as to how the files to be moved and copied should be specified. It takes a while to become familiar with this logic, and you will probably have to practice a while before these things become intuitive.

Moving Directories

To move a directory, use the mv command. The syntax is mv <directory> <destination>. In the following example, let’s move the newdir subdirectory found in your current directory to the /tmp directory:


darkstar:~$ mvdir newdir /tmp

darkstar:~$ cd /tmp

darkstar:/tmp$ ls

/newdir

The directory newdir is now a subdirectory of /tmp.

When you move a directory, all its files and subdirectories go with it.

You can use the mv command to rename a directory without moving it. For example, if you want to rename the directory newdir to olddir without copying it, the command


mv newdir olddir

does the task. All the files in newdir now are under olddir.

Removing Files and Directories

Now that you know how to create files and directories, it’s time to learn how to undo your handiwork.

To remove (or delete) a file, use the rm command (rm is a very terse spelling of remove). The syntax is rm <filename>. For example, the command:


darkstar:~$ rm dead_duck

removes the file dead_duck from your current directory.


darkstar:~$ rm /tmp/dead_duck

removes the file dead_duck from the /tmp directory.

You can use wildcards with the rm command, just as with all other Linux commands. This can cause you a lot of problems when you type the command at the wrong location. For example, the command


darkstar:~$ rm *

removes all files from your current directory. There’s no way to undelete the files, so if you issued this command by accident, you’re out of luck. The moral of the story is to be very careful when using wildcards with the rm command!

You can combine wildcards and directory paths. For example, the command


darkstar:~$ rm /tmp/*duck

removes all files ending in duck from the /tmp directory.

As soon as a file is removed, it is gone! Always think about what you’re doing before you remove a file. You can use one of the following options to keep out of trouble when using wildcards:

  Run ls using the same file specification you use with the rm command. For instance:

darkstar:~$ ls *duck

dead_duck  guiduck  lame-duck

:~$ rm *duck


In this case, you thought you wanted to remove all files that matched *duck. To verify that this indeed was the case, all the *duck files were listed (wildcards work the same way with all commands). The listing looked okay, so you went ahead and removed the files.
  Use the i (interactive) option with rm:

darkstar:~$ rm -i *duck

rm: remove ‘dead_duck’? y

rm: remove ‘guiduck’? n

rm: remove ‘lame-duck’? y

darkstar:~$


When you use rm -i, the command goes through the list of files to be deleted one by one, prompting you for the okay to remove the file. If you type y or Y, rm removes the file. If you type any other character, rm does not remove it. The only disadvantage of using this interactive mode is that it can be very tedious when the list of files to be removed is long.


Previous Table of Contents Next