The Linux command is a utility of the Linux operating system. When operating Linux, you need to use a shell – a program that gives you access to the operating system’s services. Most Linux distributions use a graphical user interface (GUI), making them beginner-friendly.
However, we recommend utilizing the command-line interface (CLI) because it’s quicker and offers more control. Tasks that require multiple steps on the GUI can be done in a matter of seconds by entering commands into the CLI
In this blog, you will learn all the commands that you should know to get comfortable with the terminal.
To start let’s first understand some basic terms:
- Command Line Interface ( CLI ): A CLI is a text-based user interface to run programs.
- Shell: A shell is a piece of software that interprets typed commands and runs them.
- Terminal: A terminal is software that a shell program runs inside of.
What are commands?
Commands are programs that are available on a system. When we run a command, the system takes some specific action. Most of the commands have a short name.
General command syntax:
command option(s) argument(s)
We don’t always need to have options and arguments.
The option tells the command how to operate.
ex:
ls -l
ls -a -l
ls -alh
Here -l is an option. We can have the option of single characters starting with a dash(-), and multiple single-character options can be written together as shown above in the third command. Options with more than one alphabets start with two dashes(—) and those options cannot be written along with other options.
You can use —help option to know details about some commands.
The argument is where we tell the command what to operate on.
ex:
ls -l ./Documents
Here ./Documents is an argument.
File system in Linux:
The above image shows what the file system looks like in Linux.
/home is the directory where normal user store files.
There are two ways to specify a path:
- Absolute path: An absolute path is defined as specifying the location of a file or directory from the root directory(/).
- Relative path: Relative path is defined as a path relative to the present working directory (pwd).
In the above image shivam is the username, ASUS is the hostname and ~/Desktop is the present working directory.
It is good to know the following directory to get a better understanding of how the commands are working.
- Root user’s home directory:
/root
- User’s home directory:
/home
- Common configuration files reside in:
/etc
- Common programs and commands in:
/bin
,/sbin
- Shared libraries and modules:
/lib
- Shared location for mounting other file systems:
/mnt
,/media
- Kernel and system information:
/dev
,/proc
, and/sys
.
Some useful keyboard shortcuts:
-
Up and Down arrow keys
: Using the up arrow key you can get previously written commands. -
Tab
: It comes very handy while writing arguments in a command if there is some directory with a long name then you can press the tab after writing some of the starting portions of the command if that portion uniquely matches with some directory then the tab will autocomplete it for you. -
Tab-Tab
: In case you remember only some initial characters of a command, then pressing the tab twice will give a list of commands starting with those characters. -
Ctrl-A
: Move to the beginning of the line. -
Ctrl-E
: Move to the end of the line. -
Ctrl-Shift-C
: Copy text to the clipboard. -
Ctrl-Shift-V
: Paste text from the clipboard.
Let’s start to look into commands now:
COMMANDS:
-
man
: This command is used to open the manual of any other command.ex: on typing command
man ls
.
-
apropos
: This command will help you to find the command you want to use. Let’s say you wanted to list everything present in the current directory, but you don’t remember the ls command for it, then using theapropos list
you will get all the commands related to listing things.
-
pwd
: This command is used to know the present working directory.
-
cd
: Changes the shell working directory.To move into the folder which has space in its name.
For example: Let’s say our current working directory is
~/Desktop
and there is a folder with the name “folder name”, then in order to change the working directory to~/Desktop/folder name/
, we will need to use the command like this:cd folder\ name
. Here\
is used to ignore the space between the words.
-
ls
: Lists directory contents. We can use various useful options with the ls command.
-
ls -l
: Here -l will tell the ls command to use the long listing format. -
ls -a
: Here -a will tell the ls command to show all the files including hidden, files that start with ‘.’ are hidden files. -
ls -R
: Here -R will tell ls to show all the files recursively i.e. everything that is present in the directory will be printed in the output. If there is a folder inside the current working directory and it has some files inside it, then even those files will be printed as output in this case.
-
stat
: Displays the status of a file.
-
mkdir
: This will help to create directories
Here we can see all the files present in the current working directory were listed. Then we wrote the command mkdir newFolder
which created a new folder in the working directory, which we can see using the ls command.
-
rmdir
: This will help to delete a directory. Writing a command followed by the path name of the directory will delete the directory, but it will work only when the directory is empty.
In the above image, we can see that when the directory is not empty we cannot delete it.
Here we can see that we deleted everything from the newFolder folder, then we are able to delete it using the rmdir
command.
-
cp
: This command is used to copy files and folders from one location to other.
In the above image, we can see that we copied the abc.txt
file from the Desktop to the newFolder
folder present on the desktop.
-
mv
: This command can be used to move files between different directories. This command can also be used to move files to different directories with different names, which we can also use to rename files.
In the above image, we can see that Desktop has a file with the name abc.txt and newFolder folder doesn’t have anything inside it. Then by using the command mv abc.txt xyz.txt, we are renaming abc.txt to xyz.txt, then by using the command mv xyz.txt newFolder/mno.txt
, xyz.txt is moved to newFolder folder.
-
rm
: This command can be used to delete files from directories, we can also use it to delete non-empty directories using the -r option, which could not be done byrmdir
command.
In the above image, we can see that abc.txt was present in the Desktop directory but after using the command rm abc.txt, it is not there anymore. Similarly, we can see newFolder folder in the Desktop directory with mno.txt file inside it, using the command rm -r newFolder folder newFolder is not there anymore.
-
find
: This command can be used to search files or folders using options as filters. You can explore this command using man.
In the above image, using -name option we are able to find files after applying some filter to the search.
Multi-user Environment
Before moving forward let’s understand the multi-user environment in Linux. In a multi-user environment, the system allows keeping the user’s files separate from other users’ profiles.
-
whoami
: In order to check which user is currently logged in, we can use whoami command.
-
su
: This command can be used to substitute the user. One of the uses of switching users is to do system administration tasks.
There are generally two kinds of users, Normal users, and Superusers.
Super Privileges
- The root is often disabled on the modern Linux system.
- Admin can borrow the root’s privilege using the sudo command.
-
sudo
: Root privileges can be borrowed using sudo command followed by a password verification.
In the above image, we can see that using ls without sudo to see the root folder’s file is not allowed. But on using sudo command followed by the password we can use it.
To switch to a root user, we can use the -s option after sudo, and to go back to the normal user, you can use the exit command.
File Permissions
For every file, there are permissions details for three types of users:
- user (owner of the file)
- group (collection of users for whom we can define specific actions)
- others (all other users)
When we write command stat with some file name then we get the details of access related to that file.
In the above image, we can see the Access details: (0664/-rw-rw-r—).
For -rw-rwr—, first - is to tell that it is a file then the next three characters belong to the user, the next three to the group, and the last three to others.
There are two methods to represent permission:
- Octet (like 755, 644, and 777)
- Symbolic (like r—, rw-, —x)
In the symbolic representation, ‘r’ means read, ‘w’ means write, and ‘x’ means to execute.
If a user has permission rwx, that means he can read, write and execute the file.
In octet way of writing permission:
Changing File Permissions
-
chmod
: This command can be used to change the permission of different users.
In the above image, we can use that, the first user in one.txt had permission to read and write only, but after using the command chmod u+x one.txt
it also gets permission to execute.
-
chown
: This command can be used to change the file’s owner. This command needs root permission to run.
In the above image, we can see on writing the command ls -l all the details of file one.txt got outputted and there we can see two shivam written, first shivam is in the place of the user and the second shivam is in the place of the group. On writing the command chown without sudo, we are getting operation not permitted, therefore we are using sudo with chown command, which is changing the user from shivam to root.
Links
Links are files that reference other files. It is used to avoid having duplicate files. A link establishes a pointer in one place to a destination in another place.
There are two types of links:
- Soft Link: A soft link or symbolic link points to another file.
- Hard Link: A hard link points to specific data on the disk.
-
ln
: This command is used to create a new link for some file. We can use -s option with ln for soft links followed by the file we want to point to and then the pointer name
If we would not pass any option with the command then the link will be a hard link.
One thing to note with the soft link is that if we change the location of the file to which the pointer points to then the pointer would not give the correct output or even if the location of the pointer file gets changed, in that case also it would not work.
Commands to read text
-
cat
: This command displays the contents of one or more files without having to open the file for editing.
-
head
: This command will output the first 10 lines of the file you give as an argument. But we can also pass the option to get as many lines as you want using the -n option.
-
tail
: This command is similar to the head command but the only difference is that it will print the last lines.
-
less
: This command displays the text of one page or screenful at a time and provides navigation control.
If you have any suggestions, please write them in the comment section. I would love to add it to the blog. ✌️
Top comments (2)
Well explained 🙌
Drop your suggestions in comments section. 🙂