By the end of this chapter, you will be able to handle on your own most of the day-to-day operations. You will learn how to create, move, copy, and remove files and directories in Linux. Also, you will learn how to edit text files using Nano.
Basic Files and Directories Commands
Create a New Directory
The command mkdir creates a new directory.
Run mkdir followed by the name of the directory you wish to create. If everything goes well, nothing will happen. You will simply get the command prompt on the following line. You can use ls to check if the directory was indeed created.
You can also use the -p option with this command followed by a path-name. This will create all the folders that are present in the path-name.
Note that using “ls” with the option “-R” lists the contents of sub-directories recursively.
Create a File
The command touch creates an empty file on the current directory. If the file already exists, then running the command will update the last modification date of the given file.
Move Files and Directories
To move a file or a folder from one directory to another, you can use the mv command. For this, type mv followed by the original file and then the target destination.
Note that you can use mv to rename files and directories as well. To do so, simply type in the file name followed by the new name you wish to give it.
Copy Files and Directories
The command mv moves the file to its new destination without keeping the original file. If you want to keep the original file, and create a copy of it, use the cp command instead.
In the example above, I have created a copy of the file “Renamed” in the parent directory.
Remove an Empty Directory
The command rmdir removes an empty directory.
If you provide a non-empty directory, then the command returns an error.
If you use rmdir with the -p option followed by a path-name, the command will remove all sub-directories present in the path-name, provided they are empty.
Remove a file
The command rm is primarily for deleting files. However, you can use it with the -R or -r options to delete directories and all their content recursively.
Read a File
If you want to read the content of a file, you can use the cat command. By default, this command displays everything that the file contains on the standard output.
To demonstrate how to do that, let’s try and read the file “/proc/cpuinfo“, which contains some information about the CPU.
The command head allows you to read only the first 10 lines of a file. This can be practical when trying to get some text from the top of a file.
You can also use the ‘-n’ flag to customize the number of lines that you want to see.
For example, the following command will output the 5 first lines of file.txt:
$ head -n 5 file.txt
While head displays the first lines of a file, the tail command does the complete opposite. It outputs the last lines of a file.
Similarly to head, running tail will by default show the last 10 lines. If you want to customize this number, you can use the ‘-n’ flag.
As an example, the following command will output the last 5 lines of file.txt:
$ tail -n 5 file.txt
The command cat can be used when trying to read a file that isn’t too long. But with longer files, the screen boundaries wouldn’t suffice to contain the content of the file, and navigating through the contained text would become a tedious task.
You can try this by yourself by reading the content of the much longer file “/var/log/syslog“
Fortunately, the less command simplifies this by providing a terminal pager that facilitates navigation within the file.
By running the above command, only the first page of the given file is shown. You can use the space key to move down to the following page.
To read files using the less command, you should remember the following keys:
- j: Move down one line.
- k: Move up one line.
- b: Move up one page.
- Space: Move down one page.
- ‘/’ + word: Search for word. For the next occurrence, press n, and for the previous one, press N.
- q: exit from the pager.
If you recall from our previous chapter, these are the same shortcuts that are used to navigate man pages.
So far, we have learned how to create, move, copy, read and remove files. But all this would mean nothing if we don’t know how to write and edit our files. Just imagine having to interact only with empty files. I can picture how stupid it would like to try to run a cat command knowing that there is no content to display.
Thankfully, we live in a world where text editors exist, and in Linux, they come in two flavors: the Graphical User Interface (GUI) and the command-line text editors.
GUI editors are the ones you are probably already used to, examples of this include Notepad in Windows and TextEdit in macOS. Linux also has its share of GUI editors. One very well known example is Gedit.
Another example is the GUI text editor that comes by default with Linux Mint.
On the other hand, command-line text editors are less common outside Linux. They don’t support the mouse, and you need to rely only on your keyboard to work your way around them. Nano, Vim, and Emacs are well-known command-line text editors.
Vim and Emacs are by far the most dominant command-line text editors. They have created a division among Linux users and programmers who disagree as to which editor is superior to the other. Die-hard fans exist on both sides and a never-ending war is being fought to this day on online forums and groups.
Vim and Emacs are more powerful than Nano, but they are difficult to learn at first for the beginner. Therefore, we will learn here about Nano, and only after a few chapters will we explore Vim. (There is no political reason for why I chose Vim rather than Emacs. We need to learn one powerful editor and I happen to be a user of the former).
But we’re getting ahead of ourselves. For now, let’s learn about Nano to get a good grasp of how command-line text editing works.
To edit a file using Nano, type ‘Nano’ followed by the name of the file.
If the given file name already exists, it will open for editing. If not, once you save it in Nano, the file will be created in your current folder.
When you run the above command, Nano will open, and you can start editing the file.
Below the editing zone, you are provided with the common shortcuts you can use. Note that the character ‘^‘ refers to the Control key on your keyboard, and the sequence starting with ‘M-‘ means ‘Alt +’.
Here are some important key sequences to remember :
- Accessing help: Ctrl+G.
- Save file to disk: Ctrl+O.
- Search for a string: Ctrl+W.
- Exit from Nano: Ctrl+X.
- Undo: Alt+U.
- Redo: Alt+E.
For simple text editing, that’s all you need to know. If you want to benefit fully from Nano and see what other options it provides, you can check the help. However, I’m sure that you won’t be using Nano very often once we learn about Vim, so don’t get too absorbed in the help section.
You should now be able to perform basic operations on files and directories in Linux. The commands we’ve learned here are the ones you will be using the most. So I invite you to take some time to practice everything we’ve learned so far. Only by practice will you improve and become comfortable with the command-line environment of Linux.
Once you are ready, I’ll see you in the next chapter, where we will learn about users and permissions.
1 thought on “Chapter 4 – Interacting with Files and Directories”