© Ashwin Pajankar 2021
A. PajankarPractical Linux with Raspberry Pi OShttps://doi.org/10.1007/978-1-4842-6510-9_3

3. Directory Commands and Text Editors

Ashwin Pajankar1  
(1)
Nashik, Maharashtra, India
 

In the last chapter, we learned how to work with the command prompt. We also updated our Pi’s OS and the firmware. We learned how to access the Pi remotely. All these skills will be very helpful to us for all the demonstrations we will see in this and remaining chapters.

From this chapter onward, we will start learning the Linux commands. We will learn the commands related to files and directories.

We will explore the following concepts in this chapter:
  • Absolute and Relative Paths

  • Commands: pwd, tree, and cd

  • Command: ls

  • Command: touch

  • Various Text Editors

  • Create and Delete Directories

  • Case-Sensitive Names of Directories and Files

After completing this chapter, we will be very comfortable with exploring the filesystem using the File Explorer as well as the command prompt.

Absolute and Relative Paths

When we are logged in as the user pi, if we open the File Explorer or lxterminal utility, by default, they show us the home directory of the user pi. The path of this directory is /home/pi. This is the absolute path. We know that the / is the root of the filesystem. When we refer to any file or a directory in Unix-like operating systems, it can be referred with the absolute path. It means that it includes all the subdirectories, starting from the root directory /. /home/pi means that in the root directory /, there is a directory home that has a subdirectory pi. We usually write the absolute path in code or documentation in order to avoid any confusion. For example, config.txt is usually referred as /boot/config.txt.

The relative path of a file or a directory is the path in relation to a directory. For example, /home/pi is the absolute path. We can also say that just /pi is the path of the same directory relative to the /home directory.

We can paste the absolute path of a directory in the address bar of the File Explorer utility and press the Enter key to go to that directory. We can use the absolute path in the command prompt too to traverse to that directory. We will see that in the next section.

Commands: pwd, tree, and cd

We can check the name of the current directory with the command pwd. It means present working directory . Open the lxterminal, and type in the command
pwd

Note that all Unix-like operating systems including all the distributions of Linux treat commands and filenames as case sensitive. Type all the commands and filenames mentioned in this book and any other documentation you come across as they are mentioned. A wrong case or mixed case will return an error.

The output of the command we executed is as follows:
pi@raspberrypi:~ $ pwd
/home/pi

In the preceding output, we can see pi@raspberrypi. It means that the user pi is logged in to the computer as raspberrypi. $ is the prompt sign, and pwd is the command. We can see the output (the present working directory) in the next line. This is the absolute path. This is how we can see the path of the current directory.

We have learned that the Unix filesystem is like a tree structure and the root directory / is at the root of the filesystem. We can see this tree structure through a command tree that shows this. If the version of the RPi OS you are using does not have this command, then you can install it by running the following command:
sudo apt-get install tree
APT stands for Advanced Package Tool . It is the tool to manage software installation, removal, and upgrade in Debian and derivatives. In order to install a new software from the repository, we have to mention its name after sudo apt-get install. Similarly, we can remove any software with sudo apt-get remove followed by the name of that software. For example, have a look at the following command:
sudo apt-get remove tree

In this case, you have removed the tree utility. Install it again with the command we learned earlier.

Execute the following command in the lxterminal in the home directory of the pi user:
tree
The results are shown in Figure 3-1.
../images/503786_1_En_3_Chapter/503786_1_En_3_Fig1_HTML.jpg
Figure 3-1

Output of the tree command

The output shows all the directories, subdirectories, and files present in the current directory in the form of a tree as shown in Figure 3-1.

We can traverse to any directory by running the command cd as follows:
cd /
Executing the preceding command takes us to the root (/) directory of the filesystem. If the directory we want to switch to is in the current directory, we can use the relative path; otherwise, we must use the absolute path. We can directly go to the home directory of the current user by running the following command:
cd ~
We will frequently be using this command to switch working directories. We can go to the parent directory of the current directory with the following command:
cd ..

In Unix and derivatives, .. refers to the parent directory of the current directory.

Command: ls

We have learned that we can use the command tree to show all the files, directories, and subdirectories under the current directory in a tree structure. There is another command, ls, that shows similar information. It means list, and it shows the files and directories (but not the subdirectories unless specified) in the form of a list. Run the command as follows:
ls
It shows the following output:
Bookshelf  Documents  Music     Public     Videos
Desktop    Downloads  Pictures  Templates
Many Unix commands can be run with options. We can pass one or more options by typing in the command followed by an empty space and then a hyphen sign followed by the options immediately. For example, if we want to see the list of files and directories in a long list format, then we can run the command as follows:
ls -l
The output is as follows:
pi@raspberrypi:~ $ ls -l
total 36
drwxr-xr-x 2 pi pi 4096 May 27 12:48 Bookshelf
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Desktop
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Documents
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Downloads
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Music
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Pictures
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Public
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Templates
drwxr-xr-x 2 pi pi 4096 May 27 13:16 Videos
Let us see more options as follows:
  • -l: It is the long listing format. This outputs file types, permissions, the number of hard links, owner, group, size, last-modified date, and filename.

  • -F: It shows the nature of a file by appending a character after the filename. For example, * is for an executable and / for a directory. Regular files do not have a suffix.

  • -a: It lists all the files in the given directory including the hidden files and directories (their names start with a . character in Unix).

  • -R: This recursively lists all the subdirectories.

  • -t: This sorts the list of files by modification time.

  • -h: It print sizes of files in a human-readable format.

  • -1: This forces the output to be one entry per line.

Let us try combining a few options:
ls -la
The output is as follows:
pi@raspberrypi:~ $ ls -la
total 104
drwxr-xr-x 19 pi   pi   4096 Aug 17 15:18 .
drwxr-xr-x  3 root root 4096 May 27 12:40 ..
-rw-------  1 pi   pi    979 Aug 17 15:56 .bash_history
-rw-r--r--  1 pi   pi    220 May 27 12:40 .bash_logout
-rw-r--r--  1 pi   pi   3523 May 27 12:40 .bashrc
drwxr-xr-x  2 pi   pi   4096 May 27 12:48 Bookshelf
drwxr-xr-x  8 pi   pi   4096 Jul 22 15:35 .cache
drwx------  6 pi   pi   4096 Aug 17 12:23 .config
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Desktop
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Documents
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Downloads
drwx------  3 pi   pi   4096 May 27 13:16 .gnupg
drwxr-xr-x  2 pi   pi   4096 Jul 26 11:58 .ipynb_checkpoints
drwxr-xr-x  5 pi   pi   4096 Jul 26 11:58 .ipython
drwxr-xr-x  6 pi   pi   4096 Jul 22 15:25 .local
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Music
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Pictures
drwx------  3 pi   pi   4096 Jul 22 15:35 .pki
-rw-r--r--  1 pi   pi    807 May 27 12:40 .profile
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Public
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Templates
drwxr-xr-x  2 pi   pi   4096 May 27 13:16 Videos
drwx------  3 pi   pi   4096 Jul 15 01:58 .vnc
-rw-------  1 pi   pi     56 Aug 15 08:32 .Xauthority
-rw-------  1 pi   pi   2624 Aug 15 08:32 .xsession-errors
-rw-------  1 pi   pi   2445 Aug 13 10:17 .xsession-errors.old

In the preceding output, . refers to the current directory, and .. refers the parent directory. All the files and directories that have . as the first character in their names are hidden. We cannot see them in the File Explorer too unless Hidden items is enabled. You may want to combine different options as an exercise for this section.

Command: touch

The command touch updates the modification date of an existing file passed to it as a parameter. If the file does not exist, then it creates a new empty file. For example, run the following command:
touch test.txt
It creates a file test.txt in the current directory. We can see it as follows:
ls -l touch.txt
The output is as follows:
pi@raspberrypi:~ $ ls -l test.txt
0 -rw-r--r-- 1 pi pi 0 Aug 17 16:48 test.txt
Let’s run the command again after a minute:
ls -l touch.txt
And let’s see its output again:
pi@raspberrypi:~ $ ls -l test.txt
-rw-r--r-- 1 pi pi 0 Aug 17 16:49 test.txt

We can observe that the last updated date has changed. We can run it with the existing filename in the parameter of the command to update the last access date without modifying the actual file.

Various Text Editors

We have already used the nano text editor to modify the network configuration. It is a WYSIWYG type of a plaintext editor for the command line. Other popular command line–based text editors for Unix are vi and vim. vi is included in the RPi OS by default. We need to install vim with the following command:
sudo apt-get install vim -y

This installs the vim editor . We can read more about vi and vim at the following URLs:

https://vim.rtorr.com/

https://devhints.io/vim

Both are a bit heavy for beginners. I prefer nano. If you want a GUI-based editor for editing the text files, you can opt for Leafpad. Install it with the following command:
sudo apt-get install leafpad -y
After the installation, we can find it in the Accessories option in the RPi OS menu. Figure 3-2 shows this in action:
../images/503786_1_En_3_Chapter/503786_1_En_3_Fig2_HTML.jpg
Figure 3-2

Leafpad in action

Create and Delete Directories

We can create and remove directories and files using the File Explorer. In the File Explorer window or on the desktop, if we right-click, we can see the menu shown in Figure 3-3.
../images/503786_1_En_3_Chapter/503786_1_En_3_Fig3_HTML.jpg
Figure 3-3

Options after right-clicking

We can create new directories (folders) and new files from this menu. Or we can use a command to create them. We have already seen how to use the command touch to create an empty file. Let us see how to create and remove a directory. We can use the command mkdir to create a directory as follows:
mkdir testdir
It creates a directory named testdir in the current directory. This is an empty directory. You can switch to it with the command cd and see it in the parent directory with the command ls. This is an empty directory, and an empty directory can be deleted by running any one of the following commands:
rm -d testdir
rmdir testdir
If you have created a few files in this directory, then you can use the command rm as follows to delete the files:
rm testfile1.txt
We can also delete a non-empty directory with all its contents as follows:
rm -r testdir

And of course, we can anytime use the File Explorer GUI to perform any of these operations.

Case-Sensitive Names of Directories and Files

Previously, I had mentioned that commands and names of directories and files are case sensitive in Linux. Let us demonstrate that now. Run the following commands in the lxterminal one by one:
mkdir test
mkdir Test
mkdir TEST
We can run the command ls to see these directories:
ls -lF
The output is as follows:
drwxr-xr-x 2 pi pi 4096 Aug 17 19:34 test/
drwxr-xr-x 2 pi pi 4096 Aug 17 19:34 Test/
drwxr-xr-x 2 pi pi 4096 Aug 17 19:34 TEST/

As we can see, it created all the directories with the same name. The case of characters is different in each name, so they are treated as different directories. In the Microsoft Windows OS, the names of files and directories are case insensitive.

We can also try to create files with the same name (with different cases for characters in names) with the command touch to see this in action for files. Try that as an exercise for this section.

Summary

In this chapter, we have started with a few basic yet important commands related to files and directories. These are very useful commands, and we will use them frequently throughout the book.

In the next chapter, we will continue our journey and learn more Unix commands.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.43.140