Copying, Moving and Deleting Files In Linux

If you have ever owned a computer before then, you know how important it is to be able to copy and move files around. That’s why I dedicated an entire blog to talk just about that: copying, moving, and deleting files.

Copying One File

Sometimes you need to copy a single file. This is a simple operation on the command-line. I have a file named cat.txt in my home directory:

We can make the use of cp command to make a copy of cats.txt named copycats.txt as follows:

As you can see, the copied file copycats.txt has the same content as the original file cats.txt.

We can also copy the file cats.txt to another directory. Example, I can copy the file cats.txt to /tmp by running the cp cats.txt /tmp command:

Notice that the copied file has the same name as the original file. We can also make another copy in /tmp with a different name:

Copying Multiple Files

We may also want to copy multiple files at once. To demonstrate, let’s begin by creating three files apple.txt, banana.txt, and carrot.txt in elliot’s home
directory:

In order to copy the three newly created files to /tmp, we can run the cp apple.txt banana.txt carrot.txt /tmp command in the terminal:

The cp command follows the

syntax: cp source_file(s) destination

Copying One Directory

You also want to copy an entire directory that’s also easily accomplished. In order to demonstrate, create a directory named citie in your home directory, and inside citie, create three files paris, tokyo, and london as follows:

Now if you want to copy the cities directory to /tmp, you have to pass the recursive -r option to the cp command as follows:

You will get an error message if you omitted the -r option:

You can verify that the cities directory is copied to /tmp by listing the files in /tmp:

Copying Multiple Directories

We can also copy multiple directories the same way you copy multiple files the only difference is that we have to pass the recursive -r option to the cp command.

Now you can copy all three directories to /tmp by running the cp -r d1 d2 d3 /tmp command:

Moving One File

If you want to move a file or a directory to a different location instead of copying and wasting disk space. You can use the mv command. Example, you can move file copycats.txt from elliot’s home directory to /tmp by running the mv copycats.txt /tmp command in the terminal:

Notice that copycats.txt is now gone from elliot’s home directory as it relocated to /tmp.

Moving Multiple Files

You can also move multiple files the same way you can copy multiple files. Example, you can move the 3 files apple.txt, banana.txt, and carrot.txt
from /tmp to /home/elliot/d1 as follows:

As we can see, the three files apple.txt, banana.txt, and carrot.txt is no longer located in /tmp as they all moved to /home/elliot/d1.

mv source_file(s) destination

Moving One Directory

We can also use the mv command to move directories. For example, if you want to move the directory d3 and put it inside d2, then you can run the mv d3 d2 command:

Notice that we don’t need to use the recursive -r option to move a directory.

Moving Multiple Directories

You can also move multiple directories at once. Create a directory named big in elliot’s home directory:

Now we can move the three directories d1, d2, and cities to the big directory as follows:

Renaming Files

We can also use the mv command to rename files. For example, if you want to rename the file cats.txt to dogs.txt, you can run the mv cats.txt dogs.txt command:

If we want to rename the directory big to small, we can run the mv big small command:

In summary, here is how the mv command works:

  1. If the destination directory exists, the mv command will move the source file(s) to the destination directory.

Keep in mind that you can only rename one file (or one directory) at a time.

Hiding Files

You can hide any file by renaming it to a name that starts with a dot.

Let’s try it; you can hide the file dogs.txt by renaming it to .dogs.txt as follows:

As we can see, the file dogs.txt is now hidden as it got renamed to .dogs.txt. We can unhide .dogs.txt by renaming it and removing the leading dots from the filename:

We can also hide and unhide directories in the same manner, we will leave that for you to do as an exercise.

Removing Files

You can use the rm command to remove (delete) files. For example, if you want to remove the file dogs.txt, you can run the rm dogs.txt command:

You can also remove multiple files at once. For example, you can remove the three files apple.txt, banana.txt, and carrot.txt by running the rm apple.txt banana.txt carrot.txt command:

Removing Directories

We can pass the recursive -r option to the rm command to remove directories. To demonstrate, let’s first create a directory named garbage in elliot’s home directory:

Now let’s try to remove the garbage directory:

I got an error because I didn’t pass the recursive -r option. I will pass the recursive option this time:

Cool! We got rid of the garbage directory.

You can also use the rmdir command to remove ONLY empty directories. To demonstrate, let’s create a new directory named garbage2 and inside it; create a file named old:

Now let’s go back to elliot’s home directory and attempt to remove garbage2 with the rmdir command:

As you can see, it wouldn’t allow you to remove a nonempty directory. There fore, let’s delete the file old that’s inside garbage2 and then reattempt to remove garbage2:

The garbage2 directory is gone forever. One thing to remember here is that the rm -r command will remove any directory (both empty and nonempty).
On the other hand, the rmdir command will only delete empty directories.

For the final example in this chapter, let’s create a directory named garbage3; then create two files a1.txt and a2.txt inside it:

Now let’s get back to elliot’s home directory and attempt to remove garbage3:

As you can see, the rmdir command has failed to remove the nonempty directory garbage3, while the rm -r command has successfully removed it. Nothing makes information stick in your head like a good knowledge check exercise.

Scroll to Top