✍ mkdir
mkdir linux-basics: creates a new directory with the specified name.
• mkdir dev qa prod : creates multiple directories at once.
• mkdir logfolder{1..10} : creates a range of directories with
sequential numbers from 1 to 10.
✍ sudo: stands for ‘superuser do’
sudo [options] [cmd]: It is used to execute commands as a root user or
super user.
• sudo apt-get update : updates the package list
• sudo -u username touch file.txt : creates a new file “file.txt” with
the ownership of “username”.
• sudo -s : starts a new shell with root privileges.
✍ history
history : display all the commands which we have executed on a
terminal.
✍ ls
ls : list the contents of the current working directory
• ls -l : list the content in long format with permissions,
owner, size, and last modified date for each directory
• ls -a : list all files in the directory, including hidden files
that start with a dot (.) character.
• ls -R : list the content of the current directory and its sub-
directories recursively.
• ls -r : reverse the order of display. Older files are listed
first.
• ls -h : display file size in a human-readable format. eg KB,
MB, or GB.
we can also combine the above options:
• ls -alh : display the content of the directory including
hidden files in a long format with file size displayed in a
human-readable format.
✍ cd
cd <path/to/directory> : will take you to the specified directory path.
• cd ~ : used to navigate to the home directory.
• cd /: take you to the root directory
• cd .. : move up one directory
✍ pwd
pwd: It stands for print working directory. It prints the current
working directory in which the user currently is.
⫸ File Operations 📁
✍ touch
touch: This command helps to create a new file.
✍ cat
cat filename : used to display the content of the file
• cat file1 file2 > newfile : concatenate file1 , file2 content in
newfile.
• cat file1 >> file2 : appends the file1 content to file2.
• cat -n filename : display the content with line numbers.
• cat -E filename : display the content with a $ sign at the end
of each line.
• cat -T filename : display the content of the filename with
tabs.
✍ cp
cp src dest : copy a file/ directory to another location
• cp file1.txt file2.txt: This will copy the content of file1 to file2
which is in the same directory.
• cp -v file.txt dev : It copies the file.txt to the dev directory in
verbose mode. Hence you were able to see the progress.
• cp -r dev qa : copies dir and sub-dir to destination dir
✍ mv
mv file.txt file3.txt : moves or renames a file or directory.
✍ rm
rm file/directory_name : removes a file or directory
• rm file.txt : delete the file
• rm -r qa : recursively delete the file and folders.
• rm -rf prod : forcefully deletes the directory/file, to remove
a directory we need to use -r.
⫸ Text Processing 📋
✍ grep
grep [options] [pattern] [filename] : ‘globally search for a regular expression
and print’. It is used for text search and filtering based on regular
expressions and returns the lines which match the pattern.
Eg: log analysis, system monitoring, and code debugging.
• grep "INFO" logfile.txt : this will return the search which has
the INFO keyword from logfile.txt
• grep "ERROR" -r -i /home/ubuntu : this will try to find the ‘error’
keyword in the path /home.
• top | grep -i systemd : grep can be combined with other Linux
commands and search for the exact pattern.
• df -h | grep -i /dev/shm : here we combined grep with disk
space command and searched for /dev/shm
✍ awk
awk <condition> {<action>} filename : awk is a programming language and
has its own syntax. It is used for text processing and manipulation.
Eg: Data extraction, Report generation or Data Manipulation.
• awk '/INFO/ {print $1 $2 $3 $6} logfile.txt : here, you can pass the
pattern ‘INFO’ and condition to print columns 1, 2, 3,
and 6 of a log file.
• awk -F',' '{print $1, $3}' fruits.txt : This will split on delimiter and
print the values of columns 1 and 3 in the output
• awk -F',' '$2 > 75 {print $1}' marks.txt : This will split on delimiter
and then the marks of students and will print their
names if marks are greater than 75.
✍ find
find : used to find files and directories in a given directory. It uses the
name, size, type, or modified time of the file to search.
Eg: file management, backup, and system administration.
• find . -name "logfile.txt" : this will find the file by name in the
current dir and its sub-directories.
• find . -name "*.txt" : this will find all the files with the
extension .txt
• find . -type d : this finds all the directories from the current
directory.
• find . -type f -size +10M : this finds all the greater than 10M
• find . -name "logfile.txt" -delete : deletes all the files which match
the filename.
✍ sed
sed [options] [pattern] [filename] : It's used to search, transform and replace
in the file/output of the command.
• sed -i 's/bitter/awesome/g' logfile.txt : It will replace the old text
with the new text in a file. -i stands for editing a file in
place, -g stands for replacing all occurrences, and -
s stands for substituting the old value with the new one.
• sed -i '3s/cool/sweet/' file.txt: replace the first occurrence of old
text with new text but only on line 3 of a file
• sed -i ‘/awesome/d’ file.txt : delete all lines containing the word
‘awesome’ in a file.
⫸ System Information
✍ top
top : It gives the system resources usage in real-time.
✍ ps
ps : gives information about running processes.
✍ df
df : gives the disk space usage
✍ free
free : gives the system memory usage
✍ uname
uname -u : displays system name
⫸ Networking
✍ ping
ping google.com : checks the connectivity by sending packets to the
host.
✍ ssh
ssh -i user@remote_host: This allows to connect to remote server via ssh
✍ scp
scp filename remotehost:localpath : these commands copy files from the
remote host to the local system.
⫸ User and Group Management
These commands are used for managing user accounts, groups, and
passwords.
✍ useradd
useradd username : this will add a new user
✍ passwd
passwd username : change the password of the user
• cat \etc\passwd : to view all users
✍ groupadd
groupadd grpname : it will create a new group
• cat \etc\group : to view all the groups
✍ gpasswd
• gpasswd -a username grpname : add a single user to the group
• gpasswd -M user1,user2 groupname : add multiple users to the
group.
• gpasswd -A username grpname : create an admin of the group.
• gpasswd -d username groupname : delete the user from the
group.
✍ deluser
deluser username : this will delete a user account.
⫸ File Permission
✍ chmod
Permission Classes:
Type: Type of file or directory.
1. Owners: They are owners of the file
2. Group: Permission for groups
3. Other: Permission for other users. eg you might have
used the share button in a Word document.
There are three types of permissions: read (r), write (w), and execute
(x).
Let’s take example
In the above example, the fruits.txt has
1. Owner : it has read and write permission (4: read) + (2:
write) = 6
2. Group: it has read and write permission (4: read) + (2:
write) = 6
3. Other: it has only read (4: read) = 4
Hence the current permission for a file is 664
To change the file permission to 772, we will be using chmod cmd.
• The first 7 indicates the owner, to change his permission
we need read(4), write(2), and execute (1) permissions,
which are represented by 4+2+1=7
• The group has read(4), write(2), and execute (1)
permissions, which are represented by 4+2+1=7
• All other users have only write permission which is
represented by 0+2+0=2
chmod 772 fruits.txt : This will change the permission of the fruits.txt file
from 664 to 772.
Here in -rwxrwx-w-` the first — represents the normal file.
⫸ Miscellaneous
✍ head
head filename : display the top contents of the file, default 10 lines.
head -n 3 filename : This displays the top 3 lines of the file.
✍ tail
tail filename : display the bottom lines of files
tail -n 3 filename : display the bottom 3 lines of files
✍ diff
diff file1 file2 : it shows the difference between the two files.
• Any line unique in file1 will be indicated by < symbol
• Lines unique in file2 will be indicated by symbol >