Linux Mint – Managing files

How to configure nginx for wordpress

Now that you have an understanding of how to navigate the filesystem, it’s time to take a look at some basic file management. After all, what good is accessing a terminal if you don’t know how to create, delete, move, and rename files and directories? Once you learn the basics of navigating the filesystem, the rest will be easy. File management in the Linux Shell is very logical but also very important to learn. Take some time to go through the following examples to manage some files on your system.

First, let’s take a look at creating a file. There is more than one way to do this, but the following command line accomplishes the goal very easily:


touch myfile

With the touch command, we created a new file named myfile. The touch command will create a file in your working directory. Thus, if your working directory was your home directory, you’ll now have a file called myfile in that directory.

However, the file that it created isn’t very useful, is it? In fact, the file is completely empty. In the next section, we’ll go over how to modify this file and add content to it. Being able to create files in Linux is an important first step, and you have successfully done so with the touch command.

Note

If you execute the touch command against a file or directory that already exists, the modified time of the file or directory will be updated, though its contents would not have changed. This is useful if you are using a backup program that looks for files with a certain modification time. Thus, you can execute the touch command against a file or directory that already exists to update the modification time and trigger a backup of that item.

So, what if you wanted to remove the file you created in the previous step? This is also very easy. The following rm command will allow you to easily remove the file:


rm myfile

So, what about directories? It’s just as easy to create a directory instead of a file using the following command:


mkdir myfolder

If we want to remove the directory later, we can do so with the rm command but with the -r flag added to it. Keep in mind though; if you delete a directory, you delete everything inside it as well. The command line to remove the directory is as follows:


rm -r myfolder

Now you know how to create files and directories as well as how to remove them. What about renaming files or moving a file or directory from one place to another? To set up a walkthrough of further manipulating files and directories, let’s create some files and directories to work with. They are as follows:


touch myfile
touch myfile2
mkdir myfolder
mkdir myfolder2

Now, we have some files and directories to play around with. First, let’s rename myfile to myfile1 to make it look better. The mv command will allow you to move from its old name to a new one. This can be done using the following command line:


mv myfile myfile1

If you execute ls to list the storage in your working directory, you’ll see that there is no file named myfile anymore; the preceding command moved it to myfile1. We can also rename directories in the exact same way using the following command line:


mv myfolder myfolder1

Renaming files and directories isn’t the only thing that can be done with the mv command. In addition, you can move a file from one place to another. Let’s move myfile1 into myfolder1:


mv myfile1 myfolder1

Now, myfile1 is no longer in the working directory. It now resides inside myfolder1. You can confirm this by typing the following command:


ls myfolder1

You can also move a folder into another directory using the following command:


mv myfolder1 myfolder2

As you can see, the mv command takes care of the logic for you. You don’t have to clarify whether the file you are moving or the destination is a file or directory. It’s able to figure it out by itself. Type the following command in the terminal:


mv myfolder2 myfile2

You would get an error. As myfile2 is a file and not a directory, you cannot move a directory into it.

In the preceding examples, we used the rm and mv commands with files and directories that existed in our working directory. It’s important to note that you are not required to be within the directory where the files are located in order for you to be able to modify them. Both the mv and rm commands accept path arguments as well. For example, if you wish to delete a file called mydocument contained under /home/users/Documents, but your working directory was some other path, you could type the following command line:


rm /home/user/Documents/mydocument

As ~ is shorthand for /home/user, you can simplify the command line even further, as follows:


rm ~/Documents/mydocument

Many commands accept path arguments in much the same way. Once you master relative paths, absolute paths, and how to navigate around, you’re well on your way to becoming a shell guru!

Comments are closed.