Hello 👋, this blog includes pretty short and precise explanation of the commands used in Linux for Devops in day to day scenario. So sit-back and have a glance at it.
Basic Linux Commands
1.pwd
It is short form for Present Working directory, shows which directory you are currently working in.
2. ls
It shows all the files and directories in the pwd (expect the hidden one).For hidden the command is different.
ls -a
The command ls -a
Lists all the hidden files too .The hidden files are represented by ( . )
ls -t
The ls -t
command in Linux is used to list files and directories sorted by modification time, with the newest files or directories appearing first.
3.cd
cd stands for Change directory, we shift and move between different directories with the help of cd <name-of-directory>
command.
cd ..
cd ..
lets you move one directory back.
cd ../.
cd ../..
takes you two directory back.
4. touch
Helps us to create a new file by the command touch <filename>
touch file1.txt
A new file will be created file1.txt , we can use any formats of file as per needs like .py, .yml, .tf and many more.
5.mv
Its a shortform for move, lets you move file and folders from path A to path B inside the system. mv <source-name> <destination-name>
Trick : You can use cp command as rename tool too if you want to rename any file or folder by using the same command.
6. cp
Its a shortform for copy, lets you copy a file or a folder using this command. Its simliar to mv command.
cp <source-name> <destination-name>
7.cat
It shows the content of the file cat <filename>
cat Resource.txt
it will show all the content present in Resource.txt file.
cat > {filename}.txt
It creates and adds data in a file . Use CTRL+d after you finish adding data to save the file. ( > ) symbol directly adds data to the file you want to create.
8. echo
The echo
command in Linux is used to display text or variables to the terminal.
echo "Hello Guys"
it will print the Hello Guys to the terminals. \n to print things on new line.
9.locate
This command will list out all files or paths with in their path or file names
locate <file-name>
Command for working with Filesystem 📁
10.mkdir
By this command mkdir <name>
, we can create a new directory.
mkdir Resource
A new directory will be created named Resource.
mkdir -p
The -p
option in the mkdir
command on Linux allows you to create parent directories as needed.
mkdir -p project/src project/bin # Creates necessary parent directories
11. rmdir
Removing a directory is done with rmdir. The directory must be empty or the command will fail. rmdir <directory-name>
12.rm -rf
To remove a directory and all of its contents recursively, it is extremely dangerous and should be used with the utmost care (-rf means remove forcefully)
rm -r
Forcefully remove a file. rm -r <filename>
13.sudo
The sudo
command stands for "superuser do" or "substitute user do." It is used to execute commands with elevated privileges, typically as the root user.
sudo <command>
sudo apt-get update
this is the most basic command used in terminal to update the system to latest version just takes about a minute to complete the task.
14. head
This command prints the first ten lines of a file head <filename>
head -n 5
it will print start 5 lines, n stands for number of lines
15. tail
It prints the last ten lines of a file. tail <filename>
tail -n 5
it will print last 5 lines, n stands for number of lines
16. man
The man
command in Linux is used to display the manual pages for various commands, utilities, and system calls. man <command>
man ls
It will list all the information about ls command.
File Permissions
17.chmod
There are two types of permissions;
1. Alphabetic:
read (r)
write (w)
execute (x)
2. Numeric:
write: 2
read: 4
execute: 1
no permission: 0
Alphabetic permissions are straight enough. Let's see the numeric permissions.
For example:
chmod 561 file1.txt
We use chmod to set permissions. Here we are setting permission on file1.txt
You might be thinking, why are there 3 numbers?
That is because:
5 is the permission for user.
6 is the permission for group.
1 is the permission for others.
When setting permissions we do it for the above-mentioned 3 types.
Here how were those numbers calculated
We granted the read(4) and execute(1) permissions to user. Therefore 4+1 =5.
We granted the read(4), write(2), and execute(1) permissions to group. Therefore 4+2+1 =6.
To others, we only granted the execute permission, hence the 1.
18.chown
The chown
command is used to change the ownership of files and directories. It allows you to assign ownership to a specific user and group. sudo chown <username> <filename>
sudo chown Krish Resource.txt
19.grep
You can search for a pattern in which specific words lies
cat file.txt | grep hello
This command will search for "hello" in the text file mentioned and will highlight where it found "hello". This symbol | is a called pipe through which we can pass other functions .
20.zip
This command is used to zip a file zip <filename>
gunzip
This command is used to unzip a file gunzip <filename>
21. tar
The tar
command in Linux is used for creating and manipulating archive files.
To create a tarball (a compressed archive file), you can use the following syntax:
tar -cvf archive.tar [files/directories]
To extract files from a tarball, use the following syntax:
tar -xvf archive.tar
-x
: Extract files from an archive.-c
: Create a new archive.-v
: Verbose mode, print the names of the files as they are extracted.
-f
: Specifies the filename of the archive.
Adding Users and Creating Group 🧑🏫
22.useradd
Adds a new user to the the system. useradd <name-of-user>
useradd Krsna
A new user will be created named Krsna. To check cat /etc/passwd
23.passwd
It creates the password for the user sudo passwd <username>
24.userdel
It is used to delete a user . But you have to be use sudo or logged in as root user.
sudo userdel <username>
25. groupadd
It is used to add a group. Only when you are logged in as rootuser.
groupadd <name>
26. gpasswd
It is used to set password for a group. Only when you are logged in as rootuser.
gpasswd <name-of-group>
27. Adding users to a group
The following command creates a new user and a new group. Then adds that user to the group.
sudo adduser <name-of-user> <name-of-group>
28. groupdel
It is used to delete a group.
groupdel <name-of-group>
29. Add existing user to an existing group
Use the following command to perform this task:
sudo usermod -aG <name-of-group> <name-of-user>
Trick :
sudo usermod -aG docker $user
Mostly while logging into docker and working we commonly face issue user not found so the above code solves the issue.
Other basic commands ⚙️
30.hostname
The command is used to view or set the system's hostname, the syntax is hostname
31.hostname -i
The hostname -i
command is used to display the IP address associated with the current hostname
32. uname
The command shows the name of the Linux, we are currently using.
uname -a
The command gives detailed information about the Linux.
33.netstat
The netstat
command is used to display various network-related information such as open ports, active network connections, routing tables, and network interfaces.
34.lscpu
The lscpu
command in Linux is used to display information about the CPU architecture. It provides detailed information about the CPU(s) installed on your system, including the number of cores, threads per core, architecture, CPU family, and more.
Architecture: x86_64
CPU(s): 4
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 142
Model name: Intel Core i5-825
35.wget
The wget
command in Linux is used for downloading files from the internet.
wget <options> <URL>
wget
https://example.com/file.zip
36.ping
The ping
command is a network utility used to test the reachability of a host (usually a computer or a server) on an Internet Protocol (IP) network.
ping <options> <hostname or IP address>
ping google.com
37.df -h
The df
command is used to display information about disk space usage. -h stands for human readable form. The syntax is df -h
and df -m
shows the data in mb
38.du
The du
command is used to estimate file space usage. It displays the space used by files and directories.
39.free -m
The free
command in Linux is used to display information about the system's memory usage. The -m
option is used to display the information in megabytes.
total used free shared buff/cache available
Mem: 3965 1831 582 84 1551 1892
Swap: 2047 0 2047
Vi Editor ⬛
To create and open a file vi <filename>
Command Mode:
When you open a file with
vi
, you start in command mode.In this mode, you can navigate, delete, copy, and perform other operations on text.
Press
Esc
to enter command mode from other modes.
Insert Mode:
In this mode, you can actually insert and edit text.
Press
i
in command mode to enter insert mode.Type your text.
Press
Esc
to return to command mode.
Basic Commands in Command Mode:
x
: Delete the character under the cursordd
: Delete the current lineyy
: Copy (yank) the current linep
: Paste the text after the cursor:w
: Save the file:q
: Quit (exit) the editor:wq
orZZ
: Save and quit:q!
: Quit without saving changes
Thanks for sticking till the end of this article. Would love to hear about your views and comments !
🚀 Let's connect on !
✨Twitter.