If you are venturing into Raspberry Pi (RPi), without a good hold on Linux, this series of articles might be of interest to you. Let me present a list of useful Linux commands those will be handy for you to operate from the RPi Command Line Interface (CLI). Especially if you have loaded the Raspberry Pi OS Lite version of the OS image, which is the leaner version of the OS, you will be left with the only option of operating from the CLI.
First thought of mentioning a few basics about the directory structure for the new users. As the Raspberry Pi OS is based on Linux, everything is organized under the "root" directory, which is denoted as "/". The working directory for an user is usually at a location "/home/<username>". If the username is "pi", then the working directory for that user would be "/home/pi". Note that, this working directory is under the root directory "/".
The working directory for a particular user, which is know as the home directory for that user is denoted as "~". Once you login to the Raspberry Pi, you will be in your working directory "~". To see which directory you are in currently, type:
$ pwd
The above command reveals the "present working directory" to you.
You could change directory to the root directory by typing:
$ cd /
But it is worth cautioning here, avoid messing up with the root directory unless you know what you are doing. We could move back to the home directory by using the command:
$ cd ~
Or, simply, type:
$ cd
This might be useful to know that your previous working directory is stored in a variable $OLDPWD. In case, you want to change to the previous working directory, type the command:
$ cd $OLDPWD
Next, you might want to know the content of a particular directory. The following command is a quick one to know the other directories and files stored in a directory.
$ ls [flags] [directory path]
The flags, which are optional, control the format and the level of details that will be displayed about the content. The path for the directory for which this command is to be executed is also option. If the path is omitted, the list command will be executed for the present working directory.
To learn more about the options available for the flags, type the following command:
$ man ls
The above command brings out the manual of the command "ls".
Let us now see a handful of frequently used flags and how powerful those could be. Executing the "ls" command without any flags, would just show the names of the directories and files stored under the present working directory. Note that, the hidden items are not shown. In order to show all directories and files including the hidden ones, you need to use the option "-a":
$ ls -a
Any file that begins with a "." is considered a hidden file in the Linux environment.
To know more details about the file or the directory, type the following command:
$ ls -l
The flag "-l" reveals the detailed information such as the owners of the file/directory, permission, date of last modification, size in bytes etc. along with the name of the file or directory.
If you need to see the subdirectories under each of the directories in the present path, type:
$ ls -R
The "-R" (capital R) flag in the "ls" command will list all directories with their corresponding subdirectories down to the last file. Obviously, this command will also show the files in the current directory.
Now let us see how we could list the content of the present directory by sorting them by their size and time. Use the flag "-S" (capital S) to sort the content with respect to their sizes, in descending order (biggest to smallest):
$ ls -S
Similarly, use the flag "-t" (small t) to sort the content according to the time last modified, in descending order (most recent first).
$ ls -t
If you want to reverse the order, simply add "r" to the above two commands:
$ ls -Sr
$ ls -tr
The above two commands will reverse the order of sorting respectively. Now it is interesting to note here, you could combine multiple flags as shown above. However, the above commands will not show the size or time information. In order to show those information too, you could combine "-l" flag with the respecting commands, for example:
$ ls -S -l
OR:
$ ls -Sl
OR:
$ ls -lS
etc.
Is not it so flexible and powerful feature of Linux? I am sure you will love it eventually.
Now, could you please try the command yourself:
$ ls -lSr
What does the above command yield?
In the following part of this series we will see the commands to create, copy and move files/directories.