In this post we are taking a step back from networking on Linux and talking about some basic commands that are necessary to navigate ourselves around it. Although we’ll be coming across many more commands along the way, these are some basics that are the most helpful to know. Let’s jump into the commands:
Important Linux Commands to Know
File List (ls)
The command “ls” lists all files in a directory. Here is how you can use it, along with some examples.
netbeez.net$ ls news.txt notes.txt temp
I see that there are three files in this directory “news.txt,” “notes.txt,” and “temp.” If I require more information about these files, I can add the “-l” flag which stands for long:
netbeez.net$ ls -l total 8 -rw-r--r-- 1 pi pi 13 Oct 5 14:07 news.txt -r-------- 1 root root 30 Oct 8 17:43 notes.txt drwxr-xr-x 2 pi pi 4096 Oct 8 17:50 temp
Let’s break this down.
-rw-r--r--
This part tells us what type of file it is, and what the permission rights are. The first dash is empty for “news.txt” and “notes.txt” because they are files. “temp” is a directory and this is noted with a “d”. Then there are three triplets of letters that tell us what file permissions the user, the group, and everyone else has. For example the “temp” directory has:
rwx:
read, write, and execute permissions for the directory owner (which is the user pi)
r-x:
read, execute permissions for the directory group (which is the user pi)
r-x:
read, execute permissions for everyone else
The number next to the file permissions dictates how many links each file has. You can think about this as the number of files and directories inside a directory.
Next is the file owner and the group of the file.
The following numbers are the file sizes in bytes (directories show up with the same number and that doesn’t represent the total of the files and directories they include).
And finally we have the last time the file or directory was modified followed by the name.
If you add the “-a” option, which stands for all, as well you get the following:
netbeez.net$ ls -la total 64 drwxr-xr-x 6 pi pi 4096 Oct 8 17:50 . drwxr-xr-x 3 root root 4096 Apr 17 17:16 .. -rw------- 1 pi pi 8946 Oct 5 14:15 .bash_history -rw-r--r-- 1 pi pi 220 Apr 17 17:16 .bash_logout -rw-r--r-- 1 pi pi 3523 Apr 17 17:16 .bashrc drwxr-xr-x 2 pi pi 4096 Jun 4 14:59 .nano -rw-r--r-- 1 pi pi 13 Oct 5 14:07 news.txt -r-------- 1 root root 30 Oct 8 17:43 notes.txt -rw-r--r-- 1 pi pi 675 Apr 17 17:16 .profile drwxr-xr-x 2 pi pi 4096 Oct 8 17:50 temp -rw------- 1 pi pi 19 Sep 21 14:49 .wpa_cli_history
As you can see now this reveals some files and directories that start with a dot “.” in their name. These are hidden, and they are only listed when we use the “-a” flag.
Command Manual (man)
If you are looking to find information around the options of a command, I’d say Google is your friend. However, every Linux command has a manual with all of the nitty gritty details, which can be accessed if you type:
netbeez.net$ man ls
So, if you are looking for a command’s information details at your fingertips, man becomes your second best friend.
Directory Creation and Navigation (mkdir, cd, pwd)
To create a new directory, type the make directory command:
netbeez.net$ mkdir new_directory
To get into the new directory, type the change directory command:
netbeez.net$ cd new_directory
Hint: if you forgot which directory are you in, you can print the working directory with the “pwd” command:
netbeez.net$ pwd /home/pi/new_directory
With the “ls -la” command we see on top two mysterious listed directories with names “.” and “. .” These are shortcuts that help us refer to the current directory and the parent directory. For example, if you want to change to the parent directory you type:
netbeez.net$ cd .. netbeez.net$ pwd /home/pi
File Manipulation (cp, mv, rm)
To copy a file, type:
netbeez.net$ cp news.txt old_news.txt
To rename or move a file, type:
netbeez.net$ mv news.txt local_news.txt
To delete a file, type:
netbeez.net$ rm local_news.txt
Note: Linux doesn’t have a recycle bin, so if you delete a file, it’s gone without warning and without an easy way to bring it back.
Root commands (sudo)
Now, let’s try to change the ownership of the file “notes.txt” from user root to user pi. As a reminder here are its file permission:
netbeez.net$ ls -la notes.txt -rw-r--r-- 1 root root 30 Oct 8 21:49 notes.txt
Which means, the owner of the file is the root user. Here is how you change ownership:
netbeez.net$ chown pi:root notes.txt chown: changing ownership of 'notes.txt': Operation not permitted
We got an error message that says that we don’t have the permission to do that. We either need to login as root, or use the “sudo” prefix as follows:
netbeez.net$ sudo chown pi:root notes.txt netbeez.net$ ls -la notes.txt -rw-r--r-- 1 pi root 30 Oct 8 21:49 notes.txt
As you can see, the owner now is the user pi, and the group has remained root. “sudo” gives the ability to user pi to run a command as superuser provided that user pi has been given that permission. It stands for “superuser do” and you have to be careful when you use it: with great power comes great responsibility!
The commands you will come across and use are countless, but these few commands that I have listed are the bare minimum that you need to know to navigate a Linux host comfortably. Happy troubleshooting!