際際滷

際際滷Share a Scribd company logo
Linux Commands
Yakinur Rahman
July 2023
Introduction
 Can know background details using the command line.
 Before diving into the command lines we need to know two things first.
1. Shell:Is a program that takes command from the keyboard and gives
them to the operating system to perform.
2. Terminal:Is a tool where you can pass your shell command.
Type wsl in cmd to go to linux terminal.
 File System
1. Hierarchical Directory Structure
2. It uses Root directory.
3. Root folder starts from /
 Commands
1. pwd Command: To know where I am.
2. clear Command: To clear the terminal.
3. cd Command: To change the directory.
If use cd you will change to home directory.
To go to root folder use cd /.
To change to home directory use cd 
1
To get back to the previous folder or one folder
above use cd ..
To go to a directory use cd [path]
When going to a directory named with white
space like My books use cd My books or
cd Mybooks or cd My books but not cd
My books.
4. ls Command: Shows the list of all the files and
directories in the current folder.
The syntax is ls [options][files or directory].
If we write ls filename/ we will see the directory
content in that file. For the root folder, it is ls /.
For the list of files and directories in the home
folder use ls .
To go back to the previous directory we use ls
... To go back two directory we use ls ../...
To get the list of directories in long format(detail
of the files and directories) use ls -l.
To know the hidden files also use ls -a. To get
2
the hidden files in the long format use ls -al.
To get the file list in long format and in sorted
order according to size use ls -lS.
And if you want sorted according to name use
ls -ls.
To get all the .c files in home/yakin use ls home/yakin/*.c
And to get all the files with whatever extension
in home/yakin use ls home/yakin/*.*
If I want to save the output of ls command in
a file I use ls -lS > out.txt
To get only the directories, use: ls -d */
T see a directory structure use : ls -R
To know more about ls command use: man ls
5. cat command: It has three relative functions
with regard to the text files.They are:
(a) Displaying the text files
(b) Combining copies of text files
(c) Creating new text file
Syntax of using cat command is : cat options
file1 file2 ...
If we use cat and then type hello world the
output will be hello world
3
To get out of cat command use ctrl+d
To display the contents of a text file use cat file-
name.txt
Adding line number to each non-blank line in the
file use cat -b filename.txt
To add line number to all the lines in the file
use cat -n filename.txt
To display all the lines in a file with a single line
gap between the lines(if had multiple blank lines)
use cat -s filename.txt
To add dollar symbol at the end of each line use
cat -E filename.txt
To know more about cat command use man cat
To write to a file use cat > filename.txt, then
type whatever you want in the file.Use ctrl+d
to get out of cat command. Then use cat file-
name.txt to be sure what you wrote were per-
sistent or not.
If we write anything that overrides the previous
contents again, use cat > filename.txt.
If you dont want to override but append then
4
use cat >> filename.txt.
To copy the contents of files to another file use
cat filename1 filename2 ... > destination-
filename
To append files to a file use cat filename1 file-
name2 ... >> destinationfilename
6. mkdir command:
It is make directory command.
To create a directory, use mkdir directoryName
To create a subdirectory in a directory, use mkdir
dirName/subDirName. If dirName dont ex-
ist than it will show No such file or directory.
To creat directory with subdirectory use mkdir
-p dirName/subDirName.
To create a list of subdirectory in a directory use
mkdir -p dirName/{subDirName1,subDirName2,...}
.Remember subdirectory names should be sepa-
rated by ,(comma) and there cant be any space
before and after comma.
7. rmdir command:
5
Remove directory command.Syntax of rmdir com-
mand is :
rmdir options directoryName
To remove a directory, use rmdir dirName
To remove a subdirectory use rmdir dirName/subdir1/.../subdirN.
Using the command you will remove subdirN. To
check use ls -R
To remove the whole directory structure, use rmdir
-p dirName/subdir1/.../subdirN. Check the
result using ls -R.
To know how rmdir command is working in the
background on a directory structure like a/b/c/d/e
use rmdir -pv a/b/c/d/e
Now for your kind information rmdir command
will not work if the directory is not empty. Like,
if the directory contain any .txt, .c, .html file,
etc. And to accomplish it use rm command.
8. rm command:
It is remove command.
To delete a directory together with its files use
rm -r structureOfTheDirectory.Suppose, a
directory is a/b/c/d/e and b contains a text file
like test.txt then using rmdir -p a/b/c/d/e
will remove c,d, and e from the directory struc-
6
ture. To remove b and its file use rm -rv a/b, it
will also who whats happening behind. You will
have only directory a. Instead of using rm -rv
a/b you could have used rm -rv a.To delete the
structure altogether.
9. cp command:
It is copy command. Used for copying files and
directories.The syntax of this command is
cp options source destination
If used cp file1.txt file2.txt, then it will copy
file1 to file2. If file2 does not exist, then it will
create file2.
If I want to have a copy of a file in a directory,
then use cp fileName1 fileName2 directory-
Name.Now fileName1 and fileName2 will have a
copy in the directoryName. It will override any
existing file.
To know is the cp command overriding anything
or not use cp -i fileName1 fileName2 direc-
toryName. If you enter y it will override, else
if you enter n then it will not override.
If we are in a subdirectory, but want to copy file
from the directory, use cp ../fileName . . cp is
the command. .. is used to go up the directory.
And the last . indicates the current directory.
7
To copy file to a directory cp filePath direc-
toryPath.
To copy the contents of a directory to another
directory and to create the destination directory
use cp -R direct1 direct2. The contents of di-
rect1 will be copied to direct2.
If the direct2 already exists a copy of direct1 will
also be in direct2.To observe use cp -vR direct1
direct2
For more details use man cp
10. mv command:
Used to change the name of a file or directory.
Syntax is
mv options source destination, source and
destination are files or directories.
If you want to move a file named test.txt to yakin/
directory use mv test.txt yakin/
If test.txt already exists in yakin/ directory, then
using mv test.txt yakin/ will override the al-
ready existing one.
If you want to prevent the overriding, use mv
8
-i test.txt yakin/
If you want to move a directory suppose yakin/
into another directory, suppose family/ then use
mv yakin family. But the condition is both
directory exists. Otherwise, it will just rename
yakin/ directory to family/ directory.
To know more about this command use man mv
11. less command:
Used to find something (word|pattern) in a file.
When having a large size of text, if you want
to read from the start use less big.txt, if the
file name is big.txt.Press the up arrow and down
arrow to read.
If you want to read page by page instead of line
by line, use space to down instead of down-
arrow and b instead of up-arrow.
To reach the END of the file, use Shift+G.
To reach the TOP press g.
To search for something, press / and then type
the word. After typing the word use ENTER.It
will get you the first occurrence of the word. To
9
go to the next occurrence, press n.
If you want to search the word from the END
of the file, press ?, then type the word. If you
press n you will get the occurrences from bot-
tom to top.
To exit less command press q.
To know more use man less
12. touch command:
Used to create new empty files in Linux. Also
used to change the timestamps of directories.
To create an empty file, use touch filename-
WithExtension You can create files only using
touch command.
To change the timestamp of the already existing
file to the current time, use touch filename-
WithExtension
13. nano command:
Nano is a useful and friendly text editor.It has
many features.
To create a new file : nano filename.extension
10
File will not be created, until saved.
You can write programs too. For example, nano
hello.cpp
14. sudo command:
sudo command provides us administrative privi-
leges. When we want to install any third-party
software we need this command.
To install gcc , use sudo apt-get install gcc
To install g++ , use sudo apt-get install g++
To get to super user mode, use sudo -s
15. top command:
Provides dynamic view of the running system.
Used to know which program is using more re-
sources.
To know which program is using how much re-
sources, enter top. PID refers to process id.
By default, after every 3 seconds the view is re-
freshed.
11
To change the refreshing time, press s when the
view after top is screening.
To filter out idle processes or to screen active
processes, press i.
To kill a process, press k. Then input the pro-
cess id.Or, PID.
An alternative is ps -ux.
If you want to know all the processes under a
userName then use ps -U userName
16. kill command:
It is used to kill processes. The syntax is:
kill -flags PID
To know the PID of a process, use pidof pro-
cessName
Then use kill PID to kill the process.
If you were not able to kill use this command
instead kill -KILL PID
Or, you can use kill -9 PID too.
12
17. echo command:
Displays anything you write after the echo com-
mand.The echo command is important when script-
ing.
To do the echo use echo statement Or, echo
statement
var=Hello, no space before and after = sign.Then
use echo $var, and you will be displayed Hello.
Or, x=10 then use echo The value of x is
$x. This will display what you are thinking.
If you use echo -e Some text  then the
escape character will be enabled. And this will
print: Some ext. If used echo Some
text then it would have printed: Some text.
18. chmod command:
Here instead of filename, you can use directory-
Name. The function is the same.
chmod o+[x|r|w] filename will grant to ex-
ecutable or read or write permission to others on
a file.
chmod o-[x|r|w] filename will sack executable
or read or write permission of others on a file.
13
Use ls -l to confirm these operations.
chmod g+[x|r|w] filename these will grant ex-
ecutable or read or write permission to groups on
a file.
chmod g-[x|r|w] filename these sack executable
or read or write permission of groups on a file.
chmod ug=[x|r|w] filename used to give per-
mission to both user, group, and everyone on a
file. Remember, no space before and after of =.
To remove all the permissions use chmod a-
[x|r|w] filename
To give permission to all, use chmod a+[x|r|w]
filename
To specifically give permission to user,group and
others, use chmod u[+|-][x|r|w],g[+|-][x|r|w],o[+|-
][x|r|w] filename.
Octal and Numerical Permissions: Remem-
ber (u,g,o) can be of combinations like([r|w|x],[r|w|x],[r|w|x]).
So, u can be expressed as a 3-bit combination.
100 means granting reading privileges to the user,
and 111 means granting all privileges to the user.
The same is applied to groups and others. 100 in
binary represents 4 in octal and 111 represents 7
14
in octal. Now, if you use chmod 000 filename
then you give no permissions to anyone. But if
you use chmod 700 filename then you provide
all permissions to the user but nothing to groups
and others.
 I/O Redirection
Redirection simply means capturing output from a file or command or
program and sending it into another file or command or program as an
input. If we want to send output to a file use output > file . Now use
cat5 command to do it.
 File/Directory Permission:
The r stands for read, w stands for write, d stands for directory, and
x stands for executable. Use chmod18 command to change the file per-
mission.
 Bash Scripting:
A script is a text file that contains a sequence of commands for an op-
erating system. Create a script file using nano myscript1.sh. And we
need to know where our bash is. Then type #! [location of the bash].
To know where the bash is, use which bash. So, now write the com-
mands you want in the myscript1.sh file.If permission is already given,
use ./myscript1.sh to do the execution.
15

More Related Content

Similar to Linux_Commands.pdf (20)

LinuxLabBasics.ppt
LinuxLabBasics.pptLinuxLabBasics.ppt
LinuxLabBasics.ppt
CharuJain396881
40 basic linux command
40 basic linux command40 basic linux command
40 basic linux command
Teja Bheemanapally
40 basic linux command
40 basic linux command40 basic linux command
40 basic linux command
Teja Bheemanapally
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsChapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
Shankar Mahure
Linux commands
Linux commandsLinux commands
Linux commands
penetration Tester
Examples -partII
Examples -partIIExamples -partII
Examples -partII
Kedar Bhandari
Unix3
Unix3Unix3
Unix3
Krishna Prasad
Linux presentation
Linux presentationLinux presentation
Linux presentation
Nikhil Jain
Linux System commands Essentialsand Basics.pptx
Linux System commands Essentialsand Basics.pptxLinux System commands Essentialsand Basics.pptx
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
linux-lecture4.ppt
linux-lecture4.pptlinux-lecture4.ppt
linux-lecture4.ppt
LuigysToro
Directories description
Directories descriptionDirectories description
Directories description
Dr.M.Karthika parthasarathy
Linux ppt
Linux pptLinux ppt
Linux ppt
Sanmuga Nathan
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
MohanKumar Palanichamy
Linux
LinuxLinux
Linux
sravan kumar
Unix Basics For Testers
Unix Basics For TestersUnix Basics For Testers
Unix Basics For Testers
nitin lakhanpal
11 unix osx_commands
11 unix osx_commands11 unix osx_commands
11 unix osx_commands
Macinfosoft
Unix primer
Unix primerUnix primer
Unix primer
dummy
Linux commands and file structure
Linux commands and file structureLinux commands and file structure
Linux commands and file structure
Sreenatha Reddy K R
Cp command in Linux
Cp command in LinuxCp command in Linux
Cp command in Linux
Syed SadathUllah

Recently uploaded (20)

How to Connect Devices and Kitchen Printers in Odoo 17 POS
How to Connect Devices and Kitchen Printers in Odoo 17 POSHow to Connect Devices and Kitchen Printers in Odoo 17 POS
How to Connect Devices and Kitchen Printers in Odoo 17 POS
Celine George
PLAY: Types, value and selection of play material PLAY THERAPY.pptx
PLAY: Types, value and selection of play material PLAY THERAPY.pptxPLAY: Types, value and selection of play material PLAY THERAPY.pptx
PLAY: Types, value and selection of play material PLAY THERAPY.pptx
PRADEEP ABOTHU
Financial Forms, Awards & Appeals for Families & Counselors
Financial Forms, Awards & Appeals for Families & CounselorsFinancial Forms, Awards & Appeals for Families & Counselors
Financial Forms, Awards & Appeals for Families & Counselors
Cyndy McDonald
Requirement Engineering and Software Requirement Specification
Requirement Engineering and Software Requirement SpecificationRequirement Engineering and Software Requirement Specification
Requirement Engineering and Software Requirement Specification
NitinShelake4
Creating Opportunities and Access for Every Student in North Carolina
Creating Opportunities and Access for Every Student in North CarolinaCreating Opportunities and Access for Every Student in North Carolina
Creating Opportunities and Access for Every Student in North Carolina
Mebane Rash
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Systematic Reviews Network (SRN)
Utilization of the Hague System in light of the Expansion of Global Economic...
Utilization of the Hague System  in light of the Expansion of Global Economic...Utilization of the Hague System  in light of the Expansion of Global Economic...
Utilization of the Hague System in light of the Expansion of Global Economic...
MIPLM
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTSARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
sanjoemiguel
More Than Both Sides Redefining Objectivity March 2025.pdf
More Than Both Sides  Redefining Objectivity March 2025.pdfMore Than Both Sides  Redefining Objectivity March 2025.pdf
More Than Both Sides Redefining Objectivity March 2025.pdf
Logan Aimone
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational TrustVani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Savipriya Raghavendra
Software Engineering and Traditional Software development models
Software Engineering and Traditional Software development modelsSoftware Engineering and Traditional Software development models
Software Engineering and Traditional Software development models
NitinShelake4
New Widget to Record Invoice Line Description Odoo 18
New Widget to Record Invoice Line Description Odoo 18New Widget to Record Invoice Line Description Odoo 18
New Widget to Record Invoice Line Description Odoo 18
Celine George
Design Policy and Strategy in the Expansion of Global Economic Initiatives
Design Policy and Strategy in the Expansion of Global Economic InitiativesDesign Policy and Strategy in the Expansion of Global Economic Initiatives
Design Policy and Strategy in the Expansion of Global Economic Initiatives
MIPLM
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdf
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdfChoosing the Right Marketing Technology Stack for Your Nonprofit.pdf
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdf
TechSoup
Next Gen Project Delivery - Disrupting the Status Quo
Next Gen Project Delivery - Disrupting the Status QuoNext Gen Project Delivery - Disrupting the Status Quo
Next Gen Project Delivery - Disrupting the Status Quo
Association for Project Management
Fuel Injection in Compression Ignition Engine
Fuel Injection in Compression Ignition EngineFuel Injection in Compression Ignition Engine
Fuel Injection in Compression Ignition Engine
NileshKumbhar21
How to Simplify Reconciliation Process using Reconciliation Models using odoo...
How to Simplify Reconciliation Process using Reconciliation Models using odoo...How to Simplify Reconciliation Process using Reconciliation Models using odoo...
How to Simplify Reconciliation Process using Reconciliation Models using odoo...
Celine George
Kothari Commission Recommendations And Their Implementation.pptx
Kothari Commission Recommendations And Their Implementation.pptxKothari Commission Recommendations And Their Implementation.pptx
Kothari Commission Recommendations And Their Implementation.pptx
Dr. JN Gorai, PhD
Unit 2 Analysis of Financial Statements.pptx
Unit 2 Analysis of Financial Statements.pptxUnit 2 Analysis of Financial Statements.pptx
Unit 2 Analysis of Financial Statements.pptx
NileshKumbhar21
How to Configure Authorized Signatory on Invoice in Odoo 18
How to Configure Authorized Signatory on Invoice in Odoo 18How to Configure Authorized Signatory on Invoice in Odoo 18
How to Configure Authorized Signatory on Invoice in Odoo 18
Celine George
How to Connect Devices and Kitchen Printers in Odoo 17 POS
How to Connect Devices and Kitchen Printers in Odoo 17 POSHow to Connect Devices and Kitchen Printers in Odoo 17 POS
How to Connect Devices and Kitchen Printers in Odoo 17 POS
Celine George
PLAY: Types, value and selection of play material PLAY THERAPY.pptx
PLAY: Types, value and selection of play material PLAY THERAPY.pptxPLAY: Types, value and selection of play material PLAY THERAPY.pptx
PLAY: Types, value and selection of play material PLAY THERAPY.pptx
PRADEEP ABOTHU
Financial Forms, Awards & Appeals for Families & Counselors
Financial Forms, Awards & Appeals for Families & CounselorsFinancial Forms, Awards & Appeals for Families & Counselors
Financial Forms, Awards & Appeals for Families & Counselors
Cyndy McDonald
Requirement Engineering and Software Requirement Specification
Requirement Engineering and Software Requirement SpecificationRequirement Engineering and Software Requirement Specification
Requirement Engineering and Software Requirement Specification
NitinShelake4
Creating Opportunities and Access for Every Student in North Carolina
Creating Opportunities and Access for Every Student in North CarolinaCreating Opportunities and Access for Every Student in North Carolina
Creating Opportunities and Access for Every Student in North Carolina
Mebane Rash
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Introduction to PRISMA: Common Pitfalls & Best Practices in Systematic Review...
Systematic Reviews Network (SRN)
Utilization of the Hague System in light of the Expansion of Global Economic...
Utilization of the Hague System  in light of the Expansion of Global Economic...Utilization of the Hague System  in light of the Expansion of Global Economic...
Utilization of the Hague System in light of the Expansion of Global Economic...
MIPLM
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTSARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
ARGUMENTATIVE COMMUNICATION AND ITS CONCEPTS
sanjoemiguel
More Than Both Sides Redefining Objectivity March 2025.pdf
More Than Both Sides  Redefining Objectivity March 2025.pdfMore Than Both Sides  Redefining Objectivity March 2025.pdf
More Than Both Sides Redefining Objectivity March 2025.pdf
Logan Aimone
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational TrustVani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine April 2025 - Quarterly Magazine of Seshadripuram Educational Trust
Savipriya Raghavendra
Software Engineering and Traditional Software development models
Software Engineering and Traditional Software development modelsSoftware Engineering and Traditional Software development models
Software Engineering and Traditional Software development models
NitinShelake4
New Widget to Record Invoice Line Description Odoo 18
New Widget to Record Invoice Line Description Odoo 18New Widget to Record Invoice Line Description Odoo 18
New Widget to Record Invoice Line Description Odoo 18
Celine George
Design Policy and Strategy in the Expansion of Global Economic Initiatives
Design Policy and Strategy in the Expansion of Global Economic InitiativesDesign Policy and Strategy in the Expansion of Global Economic Initiatives
Design Policy and Strategy in the Expansion of Global Economic Initiatives
MIPLM
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdf
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdfChoosing the Right Marketing Technology Stack for Your Nonprofit.pdf
Choosing the Right Marketing Technology Stack for Your Nonprofit.pdf
TechSoup
Fuel Injection in Compression Ignition Engine
Fuel Injection in Compression Ignition EngineFuel Injection in Compression Ignition Engine
Fuel Injection in Compression Ignition Engine
NileshKumbhar21
How to Simplify Reconciliation Process using Reconciliation Models using odoo...
How to Simplify Reconciliation Process using Reconciliation Models using odoo...How to Simplify Reconciliation Process using Reconciliation Models using odoo...
How to Simplify Reconciliation Process using Reconciliation Models using odoo...
Celine George
Kothari Commission Recommendations And Their Implementation.pptx
Kothari Commission Recommendations And Their Implementation.pptxKothari Commission Recommendations And Their Implementation.pptx
Kothari Commission Recommendations And Their Implementation.pptx
Dr. JN Gorai, PhD
Unit 2 Analysis of Financial Statements.pptx
Unit 2 Analysis of Financial Statements.pptxUnit 2 Analysis of Financial Statements.pptx
Unit 2 Analysis of Financial Statements.pptx
NileshKumbhar21
How to Configure Authorized Signatory on Invoice in Odoo 18
How to Configure Authorized Signatory on Invoice in Odoo 18How to Configure Authorized Signatory on Invoice in Odoo 18
How to Configure Authorized Signatory on Invoice in Odoo 18
Celine George

Linux_Commands.pdf

  • 1. Linux Commands Yakinur Rahman July 2023 Introduction Can know background details using the command line. Before diving into the command lines we need to know two things first. 1. Shell:Is a program that takes command from the keyboard and gives them to the operating system to perform. 2. Terminal:Is a tool where you can pass your shell command. Type wsl in cmd to go to linux terminal. File System 1. Hierarchical Directory Structure 2. It uses Root directory. 3. Root folder starts from / Commands 1. pwd Command: To know where I am. 2. clear Command: To clear the terminal. 3. cd Command: To change the directory. If use cd you will change to home directory. To go to root folder use cd /. To change to home directory use cd 1
  • 2. To get back to the previous folder or one folder above use cd .. To go to a directory use cd [path] When going to a directory named with white space like My books use cd My books or cd Mybooks or cd My books but not cd My books. 4. ls Command: Shows the list of all the files and directories in the current folder. The syntax is ls [options][files or directory]. If we write ls filename/ we will see the directory content in that file. For the root folder, it is ls /. For the list of files and directories in the home folder use ls . To go back to the previous directory we use ls ... To go back two directory we use ls ../... To get the list of directories in long format(detail of the files and directories) use ls -l. To know the hidden files also use ls -a. To get 2
  • 3. the hidden files in the long format use ls -al. To get the file list in long format and in sorted order according to size use ls -lS. And if you want sorted according to name use ls -ls. To get all the .c files in home/yakin use ls home/yakin/*.c And to get all the files with whatever extension in home/yakin use ls home/yakin/*.* If I want to save the output of ls command in a file I use ls -lS > out.txt To get only the directories, use: ls -d */ T see a directory structure use : ls -R To know more about ls command use: man ls 5. cat command: It has three relative functions with regard to the text files.They are: (a) Displaying the text files (b) Combining copies of text files (c) Creating new text file Syntax of using cat command is : cat options file1 file2 ... If we use cat and then type hello world the output will be hello world 3
  • 4. To get out of cat command use ctrl+d To display the contents of a text file use cat file- name.txt Adding line number to each non-blank line in the file use cat -b filename.txt To add line number to all the lines in the file use cat -n filename.txt To display all the lines in a file with a single line gap between the lines(if had multiple blank lines) use cat -s filename.txt To add dollar symbol at the end of each line use cat -E filename.txt To know more about cat command use man cat To write to a file use cat > filename.txt, then type whatever you want in the file.Use ctrl+d to get out of cat command. Then use cat file- name.txt to be sure what you wrote were per- sistent or not. If we write anything that overrides the previous contents again, use cat > filename.txt. If you dont want to override but append then 4
  • 5. use cat >> filename.txt. To copy the contents of files to another file use cat filename1 filename2 ... > destination- filename To append files to a file use cat filename1 file- name2 ... >> destinationfilename 6. mkdir command: It is make directory command. To create a directory, use mkdir directoryName To create a subdirectory in a directory, use mkdir dirName/subDirName. If dirName dont ex- ist than it will show No such file or directory. To creat directory with subdirectory use mkdir -p dirName/subDirName. To create a list of subdirectory in a directory use mkdir -p dirName/{subDirName1,subDirName2,...} .Remember subdirectory names should be sepa- rated by ,(comma) and there cant be any space before and after comma. 7. rmdir command: 5
  • 6. Remove directory command.Syntax of rmdir com- mand is : rmdir options directoryName To remove a directory, use rmdir dirName To remove a subdirectory use rmdir dirName/subdir1/.../subdirN. Using the command you will remove subdirN. To check use ls -R To remove the whole directory structure, use rmdir -p dirName/subdir1/.../subdirN. Check the result using ls -R. To know how rmdir command is working in the background on a directory structure like a/b/c/d/e use rmdir -pv a/b/c/d/e Now for your kind information rmdir command will not work if the directory is not empty. Like, if the directory contain any .txt, .c, .html file, etc. And to accomplish it use rm command. 8. rm command: It is remove command. To delete a directory together with its files use rm -r structureOfTheDirectory.Suppose, a directory is a/b/c/d/e and b contains a text file like test.txt then using rmdir -p a/b/c/d/e will remove c,d, and e from the directory struc- 6
  • 7. ture. To remove b and its file use rm -rv a/b, it will also who whats happening behind. You will have only directory a. Instead of using rm -rv a/b you could have used rm -rv a.To delete the structure altogether. 9. cp command: It is copy command. Used for copying files and directories.The syntax of this command is cp options source destination If used cp file1.txt file2.txt, then it will copy file1 to file2. If file2 does not exist, then it will create file2. If I want to have a copy of a file in a directory, then use cp fileName1 fileName2 directory- Name.Now fileName1 and fileName2 will have a copy in the directoryName. It will override any existing file. To know is the cp command overriding anything or not use cp -i fileName1 fileName2 direc- toryName. If you enter y it will override, else if you enter n then it will not override. If we are in a subdirectory, but want to copy file from the directory, use cp ../fileName . . cp is the command. .. is used to go up the directory. And the last . indicates the current directory. 7
  • 8. To copy file to a directory cp filePath direc- toryPath. To copy the contents of a directory to another directory and to create the destination directory use cp -R direct1 direct2. The contents of di- rect1 will be copied to direct2. If the direct2 already exists a copy of direct1 will also be in direct2.To observe use cp -vR direct1 direct2 For more details use man cp 10. mv command: Used to change the name of a file or directory. Syntax is mv options source destination, source and destination are files or directories. If you want to move a file named test.txt to yakin/ directory use mv test.txt yakin/ If test.txt already exists in yakin/ directory, then using mv test.txt yakin/ will override the al- ready existing one. If you want to prevent the overriding, use mv 8
  • 9. -i test.txt yakin/ If you want to move a directory suppose yakin/ into another directory, suppose family/ then use mv yakin family. But the condition is both directory exists. Otherwise, it will just rename yakin/ directory to family/ directory. To know more about this command use man mv 11. less command: Used to find something (word|pattern) in a file. When having a large size of text, if you want to read from the start use less big.txt, if the file name is big.txt.Press the up arrow and down arrow to read. If you want to read page by page instead of line by line, use space to down instead of down- arrow and b instead of up-arrow. To reach the END of the file, use Shift+G. To reach the TOP press g. To search for something, press / and then type the word. After typing the word use ENTER.It will get you the first occurrence of the word. To 9
  • 10. go to the next occurrence, press n. If you want to search the word from the END of the file, press ?, then type the word. If you press n you will get the occurrences from bot- tom to top. To exit less command press q. To know more use man less 12. touch command: Used to create new empty files in Linux. Also used to change the timestamps of directories. To create an empty file, use touch filename- WithExtension You can create files only using touch command. To change the timestamp of the already existing file to the current time, use touch filename- WithExtension 13. nano command: Nano is a useful and friendly text editor.It has many features. To create a new file : nano filename.extension 10
  • 11. File will not be created, until saved. You can write programs too. For example, nano hello.cpp 14. sudo command: sudo command provides us administrative privi- leges. When we want to install any third-party software we need this command. To install gcc , use sudo apt-get install gcc To install g++ , use sudo apt-get install g++ To get to super user mode, use sudo -s 15. top command: Provides dynamic view of the running system. Used to know which program is using more re- sources. To know which program is using how much re- sources, enter top. PID refers to process id. By default, after every 3 seconds the view is re- freshed. 11
  • 12. To change the refreshing time, press s when the view after top is screening. To filter out idle processes or to screen active processes, press i. To kill a process, press k. Then input the pro- cess id.Or, PID. An alternative is ps -ux. If you want to know all the processes under a userName then use ps -U userName 16. kill command: It is used to kill processes. The syntax is: kill -flags PID To know the PID of a process, use pidof pro- cessName Then use kill PID to kill the process. If you were not able to kill use this command instead kill -KILL PID Or, you can use kill -9 PID too. 12
  • 13. 17. echo command: Displays anything you write after the echo com- mand.The echo command is important when script- ing. To do the echo use echo statement Or, echo statement var=Hello, no space before and after = sign.Then use echo $var, and you will be displayed Hello. Or, x=10 then use echo The value of x is $x. This will display what you are thinking. If you use echo -e Some text then the escape character will be enabled. And this will print: Some ext. If used echo Some text then it would have printed: Some text. 18. chmod command: Here instead of filename, you can use directory- Name. The function is the same. chmod o+[x|r|w] filename will grant to ex- ecutable or read or write permission to others on a file. chmod o-[x|r|w] filename will sack executable or read or write permission of others on a file. 13
  • 14. Use ls -l to confirm these operations. chmod g+[x|r|w] filename these will grant ex- ecutable or read or write permission to groups on a file. chmod g-[x|r|w] filename these sack executable or read or write permission of groups on a file. chmod ug=[x|r|w] filename used to give per- mission to both user, group, and everyone on a file. Remember, no space before and after of =. To remove all the permissions use chmod a- [x|r|w] filename To give permission to all, use chmod a+[x|r|w] filename To specifically give permission to user,group and others, use chmod u[+|-][x|r|w],g[+|-][x|r|w],o[+|- ][x|r|w] filename. Octal and Numerical Permissions: Remem- ber (u,g,o) can be of combinations like([r|w|x],[r|w|x],[r|w|x]). So, u can be expressed as a 3-bit combination. 100 means granting reading privileges to the user, and 111 means granting all privileges to the user. The same is applied to groups and others. 100 in binary represents 4 in octal and 111 represents 7 14
  • 15. in octal. Now, if you use chmod 000 filename then you give no permissions to anyone. But if you use chmod 700 filename then you provide all permissions to the user but nothing to groups and others. I/O Redirection Redirection simply means capturing output from a file or command or program and sending it into another file or command or program as an input. If we want to send output to a file use output > file . Now use cat5 command to do it. File/Directory Permission: The r stands for read, w stands for write, d stands for directory, and x stands for executable. Use chmod18 command to change the file per- mission. Bash Scripting: A script is a text file that contains a sequence of commands for an op- erating system. Create a script file using nano myscript1.sh. And we need to know where our bash is. Then type #! [location of the bash]. To know where the bash is, use which bash. So, now write the com- mands you want in the myscript1.sh file.If permission is already given, use ./myscript1.sh to do the execution. 15