Linux Commands
Linux is based on File System. Every directory starts from root directory which is called as '/'. The symbol '/' is a parent directory.
We need to know these shell command if you want to learn Linux.
1)mkdir: Used to create a directory
Eg: ubuntu@ip-172-31-25-118:~$ mkdir test
2)ls: Used to list the files
Eg: ubuntu@ip-172-31-25-118:~$ ls
Output: test
3)pwd: On which directory you are currently in. It is also known as Present Working Directory.
Eg: ubuntu@ip-172-31-25-118:~$ pwd
Output: /home/ubuntu
Whatever command you are executing like echo, cd, ls. These commands have some code or binary files which are stored in /bin directory.
Inside / directory there are multiple directories like:-
/bin: stores binary files.
/home: list all the users.
/var: It contains variable information.
/log: stores the logs.
Eg: ubuntu@ip-172-31-25-118:~$ cd /
ubuntu@ip-172-31-25-118:/$ ls
You will see the output on terminal when you perform.
4)cd: Used to change the directory.
Eg: ubuntu@ip-172-31-25-118:/$ cd /home/ubuntu/
5)lsb_release -a: You can identify which OS you are running and on which month is it release
Eg: ubuntu@ip-172-31-25-118:~$ lsb_release -a
Output:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04 LTS
Release: 24.04
Codename: noble
This indicates that you are using a Ubuntu (flavors of Linux) OS and it is released on April 2024. The term LTS stands for Long Term Support which means you can use such OS in lifelong.
6)touch <filename>: You can create a file using touch command.
Eg: touch testing
7)mv: Move (like a cut-paste) the files from source to destination
Eg: ubuntu@ip-172-31-25-118:~$ mv testing.txt test/
I am moving testing.txt file inside the test directory (Moving File to Directory)
Note:- We need to call folder on Windows whereas in Linux, we are calling folder as directory.
If you want to move one directory inside another directory
See the example
ubuntu@ip-172-31-25-118:~$ ls
Output:- A test testing.txt
ubuntu@ip-172-31-25-118:~$ cd test/
ubuntu@ip-172-31-25-118:~/test$ ls
Output:- majedar testing.txt
ubuntu@ip-172-31-25-118:~/test$ cd ..
ubuntu@ip-172-31-25-118:~$ mv test A
I have moved the test directory inside A directory
8)ls -l: To distinguish between file and directory we are using ls -l command.
ubuntu@ip-172-31-25-118:~$ ls -l
total 4
drwxrwxr-x 2 ubuntu ubuntu 4096 Sep 4 22:00 test -> See the first word 'd'. Here test is a directory
-rw-rw-r-- 1 ubuntu ubuntu 0 Sep 4 22:03 testing.txt --> See the first symbol '-'. Here testing.txt is a file
Another way to identify the difference between them using color code. Blue color indicates a directory whereas white color considered as a file. You can find color code using ls command.
ls -a: used to see the hidden files.
9)cd .. -> You can step back one directory above.
Eg: ubuntu@ip-172-31-25-118:~$ cd test
ubuntu@ip-172-31-25-118:~/test$ cd ..
ubuntu@ip-172-31-25-118:~$
10)cd ../.. -> You can understand with an example.
ubuntu@ip-172-31-25-118:~$ cd test
ubuntu@ip-172-31-25-118:~/test$ mkdir majedar
ubuntu@ip-172-31-25-118:~/test$ cd majedar/
ubuntu@ip-172-31-25-118:~/test/majedar$ cd ../..
ubuntu@ip-172-31-25-118:~$
11)You can create parent directory including it's sub directories
Eg: ubuntu@ip-172-31-25-118:~$ mkdir -p A/B/C -> I am creating 3 directories. Here '-p' indicates a parent
ubuntu@ip-172-31-25-118:~$ ls
Output:- A test testing.txt
12)You can use clear command to clear the screen on your terminal.
13)cp: used to copy the files and directories (like a copy paste)
Eg: I am coping testing.txt file inside test directory.
ubuntu@ip-172-31-25-118:~$ ls
Output:- A test testing.txt
ubuntu@ip-172-31-25-118:~$ cp testing.txt test/
Eg: I am copying test directory into A directory
ubuntu@ip-172-31-25-118:~$ ls
Output:- A test testing.txt
ubuntu@ip-172-31-25-118:~$ cp test A
cp: -r not specified; omitting directory 'test'
ubuntu@ip-172-31-25-118:~$ cp -r test A
Here -r stands for recursive. If we have subdirectory inside parent directory and if we want to copy the parent directory then use -r flag.
14)If you see which commands you have executed in your terminal then you can use history command.
Eg: ubuntu@ip-172-31-25-118:~$ history
15)You can use rm command to remove a file
Eg: ubuntu@ip-172-31-25-118:~$ ls
Output:- A test testing.txt
ubuntu@ip-172-31-25-118:~$ rm testing.txt
If you want to remove a directory then refer below example
ubuntu@ip-172-31-25-118:~$ ls
Output:- A test
ubuntu@ip-172-31-25-118:~$ rm test rm: cannot remove 'test': Is a directory
ubuntu@ip-172-31-25-118:~$ rm -r test -> here -r stands for recursive.
16)You can use vim editor to edit the file
Eg: ubuntu@ip-172-31-25-118:~$ ls
Output:- A testing.txt
ubuntu@ip-172-31-25-118:~$ vim testing.txt
Then press 'Insert' to insert some content in a file. Once you finish writing a file then press 'Esc' and put wq(write and quit).
17)If you want to see the content of a file use cat command.
Eg: ubuntu@ip-172-31-25-118:~$ cat testing.txt
Output:-
Apple
Mango
Banana
Orange
Pineapple
Grapes
18)If you want to see the top 2 lines of a file use head command.
Eg: ubuntu@ip-172-31-25-118:~$ head -n 2 testing.txt
Output:- Apple Mango
To see the last 2 content of a file use tail command
Eg: ubuntu@ip-172-31-25-118:~$ tail -n 2 testing.txt
Output:-
Pineapple
Grapes
- If you want to know your current user, use the command whoami.
Eg: ubuntu@ip-172-31-25-118:/home$ whoami
ubuntu
20)If you want to add the user in Linux, you need to run below command using 'super user do' which is known as sudo. With sudo you won't be able to create a user in the /home directory.
Eg: ubuntu@ip-172-31-25-118:~$ sudo useradd -m jethalal
If you want to switch to the user jethalal then you need to create a password for the user jethalal.
Eg: ubuntu@ip-172-31-25-118:/home$ sudo passwd jethalal
Output:- New password:
Retype new password: passwd: password updated successfully
You need to perform below steps to switch the user. Use su (switch user) command.
ubuntu@ip-172-31-25-118:/home$ whoami
Output:- ubuntu
ubuntu@ip-172-31-25-118:/home$ su jethalal
Output:-
Password: <type the password for jethalal user>
$whoami
Output:- jethalal
To exit from the jethalal user and go back to your current user, use exit command.
Output:-
$ exit
ubuntu@ip-172-31-25-118:/home$
21)If you want to view the user details then run cat /etc/passwd command
Eg: ubuntu@ip-172-31-25-118:/home$ cat /etc/passwd
Output:-
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash jethalal:x:1001:1001::/home/jethalal:/bin/sh
22)To create a group, run sudo groupadd <group-name>
Eg: ubuntu@ip-172-31-25-118:/home$ sudo groupadd devops
To view a group info run cat /etc/group command
Eg: ubuntu@ip-172-31-25-118:/home$ cat /etc/group
Output:-
devops:x:1002:
23)There are two types of groups:-
a)Primary Group:- Whenever you create a user like ubuntu bydefault it's group will be created with same name.
Eg: ubuntu@ip-172-31-25-118:/home$ ls
Output: jethalal ubuntu
ubuntu@ip-172-31-25-118:/home$ cat /etc/group
Output:
ubuntu:x:1000:
jethalal:x:1001:
b)Secondary Group:- When you create a group with any name and you have added all the users inside that group, then the group will be called as Secondary Link
Eg: ubuntu@ip-172-31-25-118:/home$ cat /etc/group
devops:x:1002:
24)How to add user inside Group
a)Adding a single user in a group
Syntax: sudo usermod -aG <group-name> <user-name>
Here, usermod indicates to modify the user, a stands for add or append, G stands for groupname.
Eg: ubuntu@ip-172-31-25-118:/home$ sudo usermod -aG tester iyer
ubuntu@ip-172-31-25-118:/home$ cat /etc/group
tester:x:1005:iyer
b)Adding multiple users in a group
Syntax: sudo gpasswd -M <user1>,<user2> <group-name>
Eg: ubuntu@ip-172-31-25-118:/home$ sudo gpasswd -M daya,jethalal devops
ubuntu@ip-172-31-25-118:/home$ cat /etc/group
devops:x:1002:daya,jethalal