Basic Linux File Management

In addition to knowing how to navigate the Linux operating system, file management is another thing you’ll do a lot when using Linux locally or while managing cloud servers.

Getting Started

For this lesson we’ll do everything in the home directory of the logged-in user. To get started make sure you are in the home directory by typing cd ~

When you have the terminal filed with the output of previous commands, you can type clear to remove these from the terminal display.

Creating Directories

Type mkdir first to create a directory named first. Then type ls to view the contents of the current directory. You should be able to see the new directory you created name first. You can also use ls -l to view extra information about files and directories.

One of the things that make Linux cool and fun is how much control it gives you as a user. For example, instead of typing mkdir first and then typing ls, Linux allows you execute these two commands and more at the same time. Let’s create another directory named second inside the first directory, move into the newly created directory and view its contents all in one command. While still in the home directory, type mkdir first/second && cd first/second && ls

Typing mkdir first/second && cd first/second && ls as once command creates a new directory named second inside a directory named first then switches into the newly created directory and displays its contents. Since second is a new folder it has no contents. The && is responsible for concatenating all the three individual commands into one. Powerful and cool!

Creating Files

While still in the second directory, type touch one to create a new file named one. You can also use ls to view the contents of the second directory.

Creating a file using touch command.

Editing Files

Just as with everything else in Linux, there are several ways to achieve the same thing. nano is one of the numerous file editors and we’ll be using it to edit the content of the newly create file named one.

Type nano one to open the file for editing in nano . You should have a screen similar to the one below.

Using nano to edit a file.

With the nano editor open type “Hello world!”. You should have a screen similar to the one below.

Contents of the file named one.

Press ctrl + o to write the file (you may have to press enter to confirm), then type ctrl + x to close the file. On Mac use control in place of ctrl .

Copying Files

Let’s copy one from the second directory (which we are currently in) to the first directory which is a level above.

cp is used to copy files in Linux. It requires 2 arguments, the source file path and the destination file path. To copy one from the second directory to the first directory type cp one ..

.. in the command above specifies the directory that is a level above as the destination path. In this case the directory a level above is named first.

Using cp command to copy a file named one into the directory that’s a level up.

Move into the directory we just copied the file into by typing cd .. . Once you’ve done that you should be in the directory named first. View the contents of the first directory by typing ls -l

Viewing the contents of the first directory.

Do you notice that since second is a directory it has a d in the extra details section while one which is a regular file doesn’t.

Moving Files

While in the first directory, create a new file named two using the following command touch two then view the contents of first with ls

Contents of the first directory showing the newly created file named two

Let’s move the two file into the second directory using the following command mv two second . The mv command takes 2 arguments, the source and the destination paths. Since both the source and the destination are within the same directory we are using a relative path by simply specifying their names.

Let’s view the contents of the second directory by typing ls second . You’ll see that second now contains both files one and two. Type ls (while still in the first directory) and you’ll see that the first directory no longer contains the file named two.

Moving a file to another location.

Renaming Files

To rename a file we use the mv command but instead of specifying the destination path as the second argument, we simply provide the name we want to rename the file to. While in the first directory type mv one three . After that type ls to view the contents of first. You’ll see that file named one is now named three.

Renaming a file using mv .

Deleting Files

To delete a file the rm command is used. Please note that you will note be asked to confirm deletion of the file so be careful not to delete the wrong files. While in the first directory type rm three to delete the file named three. View the contents of the directory to confirm the file has been deleted.

Copying a Directory

Copying directories is similar to how regular files are copied using the cp command. The only difference when copying directories is the inclusion of the -r option which stands for recursive and that simply means include all directory content while copying to a new location. In the first directory type cp -r second third . This command copies the second directory including all of its content to a new directory named third (if the directory doesn’t exist it creates it). To confirm the copy command type ls third and you’ll notice that the new directory named third has the same content as second.

Copying a directory using the cp command.

Moving a Directory

Type mv second third to move the second directory into the third directory. When you check the contents of the first directory you’ll see that it only contains third. Viewing the contents of third using ls third shows that third now contains the directory second.

Moving a directory to another location.

Renaming a Directory

Let’s move into the third directory using cd third . Once in there, let’s rename the second directory to fourth using mv second fourth then check the contents of third using ls . You’ll notice that the directory named second has been renamed to fourth.

Renaming a directory using mv .

Deleting Directories

To delete a directory the rm command is used. While in the third directory type rm -r fourth to delete the fourth directory. Please note that Linux does not confirm deletion of files and directories.

Deleting a directory using rm -r .

Conclusion

File management is a critical part of using any operating system and this is no different for Linux. Whether you are makings of your local system or a connecting to and managing a remote server, file management commands will make up a lot of the commands you’ll be using.

The next lesson in this course is Summary: Working With Linux.

Back to: Basic Linux for Cloud Computing > Linux Fundamentals