Hello, This is Kandy. Today we are going to have a look at a list of Linux commands that every Linux user must know but before that, we must know what is a shell.
What is a Linux shell?
A shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. - Wikipedia
Shell helps a user to communicate with the Linux kernel directly with the help of utility programs such as ls, sort, cc, who, etc. We are going to learn about a few of those utility programs (Linux commands) that we are most likely to use in our daily lives as Linux users. Even though there are many other types of shells in Linux such as Bourne Shell (SH), C Shell (CSH), Korn Shell (KSH), Z Shell (ZSH), etc, Here our primary focus will be on Bourne Again shell (BASH) which is most popular and default shell in many Linux distributions (aka. Distros) of Linux. So Without any further ado, let's open a terminal and get started!
Type bash
into the terminal if your default shell is not BASH. (You can also type sh
to switch to bourne shell.) and your Terminal would look something like this:
Notice how it displays my username and system name separated by @ character. The operating system that I am using is KDE NEON. So the user interface might differ on your side. Also, Notice the ~ character. What it tells us is that our current working directory is the Home Directory. If the symbol were / then it would be root directory. Before we start discussing other commands, There is one command which is popular as a prank in the Linux community that you must know but not use.
Note: The command Sudo rm -rf /
will force delete your root directory which holds important files that your operating system needs to function properly. It will wipe out your entire Linux system. So DO NOT use this command unless you want to utterly break your system.
Essentiel Linux Commands
Man Command
the man command stands for manual. It comes on top of the list because it is one of the most important commands for any Linux user. It allows you to learn about what any command in Linux does, What parameters you can pass to it. Let's try and see what man command outputs for the man utility program. Type
man man
in the terminal window.
Now we know what this command does, press Q on the keyboard to close the manual and get some insight into the next command we are going to try.
Clear Command
So to not clutter our terminal with outputs that are no longer our concern, type clear
and press enter to clear the terminal window. It works the same as cls
in the command prompt. (That was easy! Isn't it? Let's move ahead.)
Cd Command
Think of it as you want to "Change Directory" to remember what cd
does. If you type just cd
(and press enter) it will switch your present/current working directory to Home Directory (same as cd ~
). But with an additional parameter, You can Define a path to which you want to switch to.
For Example:
cd ~/Documents/
will switch the current working directory to the Documents Directory which is present in Home Directory.cd /
will switch pwd (present/current working directory) to the root directory.Useful Tip: Type
cd
(add space) and double press TAB Key to list subdirectories.After the above command, Entering
cd ..
will take you to the parent directory. In our case, the Parent Directory to Documents folder is Home Directory. You may see something likecd ../..
chaining but it just means jump to the parent of the parent directory of the current directory. Confusing? Let's see that in action.
Here What we are doing is switching to ~/Documents/Office directory, again and again, to notice how the cd ..
command is changing our pwd. An important point to notice is that even the Home directory is located in our root directory and using the cd ..
command on the root directory does literally nothing because it is the root directory! just like a root node in a tree structure. The File system of Linux looks something like in the image below:
Though the Linux File system is not our concern right now still it's good to know. But now Is there a way to know our present working directory using command? Absolutely!
Pwd Command
This Command prints the current working directory. To see that in action:
- Let's switch to bourne shell using the
sh
command. now we cannot see the pwd in the terminal. - Type
cd ~/Desktop
to switch to the desktop folder. - Type
pwd
and see if we get the absolute path of the Desktop folder.
And here we have it!
Ls Command
ls
command helps us to list all the directories and files under the pwd(present working directory).
Let's check what files and folders we have in Home Directory.
As you can see when I typed ls
, It lists the files and directories. But those are not all the files and directories that are present in our home Directory. To see all the files and folders including the hidden files and folders, you type ls -a
which means list all the files. So now you know all the hidden files and folders start with the .
character and so we can make our own hidden files as well. Note that these files are not secret and can be seen by turning on the visibility of hidden files in file explorer. (For Example: In Dolphin File Explorer, You do so by pressing Alt + .
and you can see all the hidden files. )
Here's the side-by-side comparison of the same Directory with and without hidden files:
Now if you noticed, ls -a allows us to see all the files and directories but how do we tell which one is a directory and which one is a file? For the same, we use the ls -l
command. Now we don't have to use only 1 parameter, we can combine the power of both parameters by using ls -a -l
or simply ls -al
or ls -la
.
Now we can see a lot of information here, Let's dig deeper, It would be fun!
The first thing we see is "Total 109100" specifies storage that is being consumed by the current directory in KBs. But it's not very human-readable. So to make it human readable, Let's combine it with another flag -h
which makes such information more human readable. ls -alh
Pretty Neat! Now that we know what that is, what is drwxr-xr-x ?
The d in drwxr-xr-x tells us that the file named Android is a directory.
Did I say "File is a directory"? Yes, Even Directories are files. Linux makes no difference between a file and a directory since a directory is just a file containing the names of other files. (Source: https://tldp.org/)
If the first character is "-" instead of "d", that means the file is a file. Uhmmm, Yes, Worst definition ever but I hope you understand. If not then try this:
and below are the all possible characters that define file type:
pretty intense, Let's not dig deeper here, And continue with the rest of the part in the string which is fairly less complicated.
you can break the drwxrwxr-x example string into 4 parts: why 4? because they specify different things.
d| rwx| rwx| r-x
1st partition
specifiesfile type
.2nd partition
specifies permissions that the current User has over the file.3rd partition
specifies Group permissions over the file.4th partition
specifies permissions for Other users.
filetype | U | G | O
R - Read permission, W - Write permission, x - eXecute permission
If any of the characters is "-" in UGO partitions of the string, then it means that permission is not granted to that particular user.
until now we only covered 5 commands and we already realize why they are one of the most important commands to know. We also learnt a few fundamental concepts which will help us understand Linux better.
Though File permissions are not something specific to Linux. These permissions can be manipulated with the next command we are going to discuss. And before let's create a test file in our desktop directory using the command cd ~/Desktop/ && touch test.txt
. Ufff more Explanation, What this command means is
"Go to Desktop directory in home folder" AND(&&
) create(touch
) a file named "test.txt" and because there's no file named test, it creates one. So the Touch
command creates a file? Ummm Yes, but not necessarily. I won't go much in detail to not make it boring so if you are curious TRY COMMAND man touch
(That's why it's the most important command.)
Also, &&
makes sure that the second part of the command will only run if the first command succeeds. The same happens if you have used it in IF Conditions in programming. so yes you can use ||
and piping |
. (You can explore more but I will not dig into it deeper. Just know we can combine multiple commands.)
Let's put some text into our file next.
Echo Command
Though there are multiple ways to enter text in our text file, We will use the echo Command for now.
Type echo "hello here's my sample text" >> test.txt
and then cat test.txt
to see what we have in our file.
I ran the echo command 3 times:
echo "hello here's my sample text" >> test.txt
echo "hello here's my sample text" >> test.txt
echo "hello" > test.txt
This is to emphasize the difference between >
and >>
.
>
- overwrite the previous content of the file
>>
- append the new text on the next line preserving the previous text stored in the file.
What's the other way to edit a file in a more user-friendly way? try nano test.txt
, edit content, ctrl + x
to close the editor, It will prompt you if you want to save the changes. To confirm the changes press Y and Enter to confirm the file name.
If you are still reading, You already have covered a lot of things. But coming back to the point, How do we change permissions to a file? This one is important to know.
Chmod Command
Chmod command allows us to change permissions of a file or directory. Adding -R
will execute the command recursively. Know that there are two ways to assign permissions:
- Using integral values (7,6,4,2,1,0) Where
0 - means no permission 1 - means execute permission, 2- write permission
4 - read permission, 6 - means read and write permissions, 7 - Read, write and execute permissions(All)
- Using characters (r - read ,w - write ,x - execute)
(+ means grant permission, - means revoke permission and = means give exact permissions)
For example:
This grants all the permissions to the User, group as well as others. The equivalent of this in character form would look something like this:
Let's say I want to revoke the permission for others. So I can do it by using the command below:
and as you see, for others the permission is "- - -" which means others won't have read, write or execute permission on the "test.txt" file.
This File's name is weird, so let's change it to Hello world.txt
mv command
Let's rename the file using the mv
command which actually stands for move and It is used to move files or directories but we are going to use it to rename. (Because why not?) Experiment on how to move the file yourself.
and so our file is renamed to "Hello World.txt". Notice that the name is enclosed within double quotes because our file name consists of space in between words. We don't want them to be treated as different parameters!
Cp Command
It allows us to create a copy of the files and directories. Let's try it and create a copy of our hello world.txt
file.
As you can see using backslash \
is another way to escape spaces in file names. And we successfully copied a file and displayed the contents of both the files using the cat
command. Which is mainly used to concatenate the contents of the files but we can use it to display the contents of a file as well.
The example below shows how we can concatenate the files using cat
Su command
This command allows you to switch to Superuser also known as the root user.
Here You can see that at the first attempt, Authentication failed because I missed the Sudo
keyword which is only available to those users that belong to the Sudo group
. The second attempt failed because I entered an incorrect password, While in the third attempt I was able to access the root terminal. A root user can do anything and everything and so It is not prefered to use a root account. Let's switch back to our main account using sudo su <your username>
.
you can also check groups using the groups
command.
Rm Command
It is used to delete files and directories.
rm
- means remove.
-r
- means recursively. (What is the meaning of recursive? It means applying the command to subfolders and files in it as well. it is necessary if you want to delete a directory containing subfolders and files).
-f
- means force the deletion.
/
- means root directory. which is actually a path.
So Syntax for using the rm command is as follows: rm -flags <file/directory>
.
Let's clean up our test files and delete them.
As we can see I was able to successfully delete both files.
Linux is huge, I cannot possibly cover all the commands and so you will need to experiment and explore the world of Linux yourself. But I hope this article helped you learn Linux and It's a pretty good start. There are probably lots of questions on your mind but I am sure you know enough to carry on with your journey to learn Linux.
Experiment. Explore. keep Learning.
(And most importantly follow me. Because there will be a lot of interesting content.)