It’s too much important for a DevOps Engineer to know Linux from basic to advance level as I Explained in my first blog. I’m going to discuss about the basic Linux command in this blog.
- Too see the user we’ve to use command:
# whoami
The output will be the current username of your user by which you’ve logged in to System
For me it’s titu my nickname which I’ve used as username
Some others command to know about the system:
# uname ..>> To know the kernel/os name
# uname –r >>To see the version of the OS
# date >>Shows date and time of the system
# history >> All the previous command we’ve run and
# clear >> For clearing the screen
[if !supportLists]2. [endif]For creating folder/directories we’ve to use below commands
# mkdir <folder name>
If I want to create folder name devops then the command is
# mkdir devops
More about folder/directory:
# mkdir foldername1 foldername2 foldername3 >> To create more folder at a time
# mkdir foldername{1..100} >> Same name sequential folder 1 to 100
# mkdir –p /A/B/C/D/E >> To make a path of of the directories or folder inside folder
# cd <Folder name> >> Change folder of same user
Example : # cd /devops >> Command will take us on /devops folder, to go folder inside folder we’ve to use command cd <folder/directory path> , cd /A/B/C/D/E will take us on E folder directly
# cd .. >> Will return you on previous directory
[if !supportLists]3. [endif] File create is not too much different from folder. Here the command for creating file is
# touch <file name>
If I want to create a text file named abc the command will be
# touch abc.txt >>This is an empty text file
To input data in file we’ve to use vim, vi or nano editor [ I will discuss later ]
More about file:
# touch filename1 filename2 filename3 >> Creating more than one file at a time
# touch filename{1..100} >> Same name sequential file 1 to 100
[if !supportLists]4. [endif]Cope file or folder we use cp command ,
# cp <option> <file/folder name that will be copy> <file/folder name where will be copy>
Here the options are:
-r >> For recursively
-v >> Verbose
-f >> Forcefully
[ N.B: Please follow the options carefully , it will also be used in remove/delete file and folder ]
Example :
# cp –rvf abc /devops >> Copy all data of abc directory to devops directory
# cp –rvf /home/titu/A* /devops >> will copy all data start with A of /home/titu/ folder to /devops folder
For deleting and file/directory we’ve to follow the same process but the command will
# rm <options> <file/foldername>
For rename/ move file and directory we use the command
# mv <file/folder name that will be move> <file/folder name where will be move>
More about file and directory:
# ls >>The command is used to see list the contents of a directory/folder.
Here we’ve to remember some attributes of ls
# ls –l >> Will show the type of the contents, permission, user and last modified date
# ls –a >> Will show all list of file including hidden files
# ls * .sh/.txt >> will show all the specific files only
# ls – d */ >> will show only directory
7. To edit text in file vim/vi/nano is used in different flavor of Linux. For example if I want to edit file bangla.txt then the command will be :
# vim/vi bangla.txt >> it will open the text editor format of file bangla.txt
It has 3 mode like :
Normal mode: This is the default mode in which vim starts. In normal mode, we can use various commands to navigate and edit the text.
Insert mode: In insert mode, we can type text into the file. To enter insert mode, press the "i" key. To exit insert mode and return to normal mode, press the "Esc" key.
Command mode: In command mode, we can enter commands to perform various actions, such as saving the file or quitting vim. To enter command mode, press the ":" key.
8. To see what’s written in file we use cat command. If my filename is abc.txt and inside it there is written something then we will use the command:
# cat abc.txt >> See whats written in abc.txt file
It has some options to view content. They are :
# cat –b <filename> >> Will add line numbers to non-blank lines
# cat –n <filename> >> Will add line numbers to all lines
# cat –s <filename> Will squeezes blank lines into one line
If we want to see specific line of file the command will be :
# head –n 3 <filename> >>First 3 lines of file
# tail –n 3 <filename> >>Last three lines of file
# diff <filename1> <filename> >>to find difference between files
9. Linux file system has three types of permission. They are:
[if !supportLists]1. [endif]Basic Permission
[if !supportLists]2. [endif]Special Permission
[if !supportLists]3. [endif]Access control List(ACL)
The command # ls –l<filename>is used to check file permission. The output is show like:
-rw-rw-r-- | 1 | titu | titu | 26 mar 6.10 |
Content type with permission | Link | User | Group | Create and modify |
For directory command is # ls –ld<directory name> and the output will show below:
drwxrwxr-x | 3 | titu | titu | 4096 | Mar 29 04:42 |
Content type with permission | Link | User | group | size | Create and modify time |
In permission:
# r for means user can copy and read file and for directory user can view contents
# w for means user can modify file contents and for directory,it’s also same
# x for means user can execute the executable file and for directory allow to use cd command
Change Permission in different user’s details:
For owner (u)
For group (g)
For others (o)
Permission in numeric in details:
Number | Permission | Symbol |
0 | No Permission | --- |
1 | Execute | --x |
2 | Write | -w- |
3 | Execute + Write | -wx |
4 | Read | r-- |
5 | Read + Execute | r-x |
6 | Read + Write | rw- |
7 | Read + Write + Execute | rwx |
For changing permission of owner the command is:
# chmod u+x abc.txt >>Add execute permission to user in abc.txt file
# chmod g+r abc.txt >>Add write permission to group user in abc.txt file
# chmod o+r abc.txt >>Add write permission to user in abc.txt file
We can also change ownership of the file and directory, For that the command is:
# chown <username> <file/foldername> >>Will change owner of the file
# chgrp <groupname> <file/foldername> >>Change group ownership of the file
The command is used to sort the results of search either alphabetically or numerically of file directories and also content of file. The command will be:
# sort -f <filename/foldername> >> Here -f for case sensitive sorting, -r for reverse sort of data and -n for sorting data in numerical way
The command ping is user to check if host is responding or not.
# ping <domain name/ ip address>
9. lsof is used to display a list of all the open files on a Linux system just like windows task manager for windows
# sudo lsof -u root >> The command sudo is for super user permission and root is the user
- The command ifconfig is used to configure network interfaces.
# ifconfig
14. Grep is a good tool to use when you need to search for a text pattern in a file
# grep <option> pattern [Files/Location]
For example, to find mango from fruits.txt file the command is:
# grep “ mango” /home/titu/fruits.txt
# grep “word” *< file type> >> It will show all same file type
Attributes of grep:
# -c <File name> >> For Count Number of lines match the word
# –l “word” <File name> * >> Search word with file among all file name in system
# -h >> For all matching lines with word
# -n >> Display all the matching lines with number
# -v >> For all the no matching line
# -o >>Display only matching pattern
# -i >> To make our search case-insensitive
# -w >> Only for whole word
# -e >> For multiple search pattern
15. Find is also used for finding the data in different way. The syntax for find command is:
# find < option> <file name>
It has also some attributes. Let discuss that
# find . >> All files and directory in current working directory
# find . –type d >> For finding all the directory
# find . –type f >> All the files in directory
# find . –name <filename> >> Only specific file
# find . –name “ * <File type>” >>All the files of specific type with location
# find . –size +/- <size of file > >>specific/more or less size files
Thus we can also check max modified time , min modified time etc
[ To make search case-insensitive we’ve to use –i before attributes of find ]
16. The command apt-get is used to install, update, and remove packages, as well as to manage the package repository sources.
Some common apt commands are
# apt-get update >> The command is used to updates the package index files from the package repositories .
# apt-get upgrade >> The command is used to versions of packages that are already installed on the system.
# apt-get install <package name >>> This installs one or more packages.
# apt-get remove <Package name > >> To removes one or more packages, but it does not remove the configuration files for the package.
17. The command ssh-keygen is used to generate a public/private authentication key pair.
This is a process to connect remote server without password as we did it AWS connect with pc in Linux advance class [Will discuss in next blogs]
Some bonus of Linux:
# free >> The command displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel
# df (disk free) >> command will show account of available disk space, used by file system.
# du (disk usage) command reports the size of directory tree including all the content.
# tnc is mostly used command while troubleshooting. It displays diagnostic information for network connection The Command format:
# tnc <server_name> -port <port>
# kill is used to terminate processes manually just like process end at task manger in windows
# htop is used to monitor the system’s resources and processes that are running in real time.
# Curl is a tool used for transferring data to or from a server, using various protocols, such as HTTP, HTTPS, FTP, and more.
This my second blog on my Devops journey. We can also use the blog as a hand note of Basic Linux for devops beginner.