Linux Commands
Linux Commands
LINUX
COMMAND LINE INTERFACE
unix
COMMANDS
Command line interface
1 BASIC COMMANDS
01. Basic Command page no :1
1. pwd -- Print Working Directory.
2. mkdir dir-name -- create directories( folders).
3. mkdir dir1 dir2 -- create multiple dirtory
4. mkdir my-dir{1..5} --create the range of dir’s
5. mkdir -p demo/de2/de3/de4 -- to create dir within their parent dir
6. mkdir .my-dir -- to create the hidden dirtory
7. mkdir .dir1 .dir2 -- to craete multiple hidden dir
8. mkdir .my-dir{1..5} -- create the range of hidden dir’s
9. rmdir dir-name -- delete the particular dir
10. rm -rf dir-name -- force fully delete dir with files
01. Basic Command page no :2
TWO
2 MANAGEMENT
01. user and group management page no :8
Types of users:-
1) super user (root)
2) normal user
3) sudo user
1. sudo adduser username -- create the user with home dir and primary group
2. sudo useradd username -- create user with home dir
3. sudo userdel username -- delete the user
4. sudo cat /etc/passwd -- check the users
5. sudo cat /etc/shadow -- to check encripted password
6. sudo passwd username -- modify or set-up password for user
7. usermod -l login-name old-name -- change the username
8. users have UID (user ID) for the kernel referance & username is for our referance
page no :9
Types of groups:
1. primary group --> the default group created while a user with the name as its username
2. secondary group --> except primary group if a user is present any other group is called
as secondary group
THREE
3 PERMISSIONS
page no :12
03. USER AND GROUP PERMISSION FOR FILES
page no :13
03. user , group management and permission
Read Write Execute
4 2 1
0 - - -
1 - - X
2 - W -
3 - W X
4 R - -
5 R - X
6 R W -
7 R W X
1. sudo chmod 764 filename -- file permission for user and group
2. sudo chown user:group filename -- user permission for user and group
page no :14
FILE
• default permission ===> root user ==> 644 (rw-r--r--)
• default permission ===> normal user ==> 664 (rw-rw-r--)
• max permission of file is ==> 666 (rw-rw-rw-)
• max permission of executable file ===> 777 (rwxrwxrwx)
DIRECTORY
• default permission ==> root user ==> 755 (rwxr-xr-x)
• default permission ==> normal user ==> 775 (rwxrwxr-x)
• max permission of dir is ==> 777 (rwxrwxrwx)
page no :15
02. file management
1. top -- to check the real-time running process and cpu and memory usage.
• PID -- Shows task’s unique process id.
• PR -- The process’s priority. The lower the number, the higher the priority.
• VIRT -- Total virtual memory used by the task.
• USER -- User name of owner of task.
• %CPU -- Represents the CPU usage.
• %MEM --Shows the Memory usage of task.
• COMMAND -- The name of the command that started the process. \
1. top -n 10 -- top 10 cpu and utlization process
2. top -u root -- to check the spcific user process
page no :18
Four
4 GENREL COMMAND
Click here to add content of the text.
page no :19
01. Regular Linux Commands
1. tar -cvf file.tar file1.txt file2.txt -- to compress two into one file
2. tar -cvf file.tar “file*” -- create the tar (zip) file or compressed file
3. tar -cvzf file.tar “file*” -- create extra commpressed file [10 gb -->2gb]
4. tar -xvf file.tar -- extract the tar file
5. tar -xvf file.tar -C dir-name -- extract the file to spcific folder
6. zip test.zip file1.txt file2.txt -- zip the files
7. zip test.zip file* -- zip the all files starting with files
8. unzip test.zip -- extrac the file in current dirtory
9. unzip test.zip -d /home/ubuntu -- diexterc the file in another dirtory
10. head -n 4 > output.txt -- output copy to new file
page no :20
01. Regular Linux Commands
1. grep -v word file-name -- Search the without word all other lines.
2. grep -r word * . -- Search word from all files in current dirtory
3. grep -i word file-name -- Search the work without case sensitivity
4. grep -c word file-name -- count the word no.of time present in line
5. grep -n word file-name --Search the word and line number that containe
6. grep -w word file-name -- Search for exact matching word using the -w option
7. grep --color word file-name -- Search word with color
8. grep -A2 word file-name -- display the line after the result.
9. grep -B2 word file-name -- display the line before the result.
10. grep -C2 word file-name -- display the line after and line before the result.
page no :24
04. Reguler Linux Command
1. cut -b 1,2,3 file-name -- Search the first three bytes in all rows.
2. cut -b 1-3,6-8 file-name -- Search the first three and next 5 to 7 bytes all rows.
3. cut -b 2- file-name -- Search the after 2 bytes in all rows.
4. cut -b -3 file-name -- Search the first three bytes in all rows.
5. cut -f 2 file-name -- Search the 2 field in table.[if sprated with tab]
6. cut -f 1,3 file-name -- Search the 1 and 3 field in table.[if sprated with tab]
7. cut -d “ ” -f 2,3 file-name -- Search the 2 and 3 field.[if sprated with -,:,;]
8. cut --complement -f 2 file-name -- Search the without 2 field.
9. cut --output-delimiter="_" -f 1,2 file-name -- Search the output with underscore.
10. cut -c 1,2,3 file-name -- Search the first three characters lines.
page no :25
04. Reguler Linux Command
1. sort file-name -- sort used for arranging the records in a particular order.
2. sort filename > new-file-name -- arraged file copy to another file.
3. sort -r file-name -- Reverse-oder sort the file.
4. sort -n file-name -- Numer sorting .
5. sort -nr file-name -- Reverse number sorting.
6. sort -k 2 file-name --sort the secound column if that numbers.
7. sort -c file-name -- check the file already sorted or not .
8. sort -u file-name -- sort and remove the duplicates.
9. sort -M file-name -- sort by month.
10. ls -l | sort -nk 5 -- sort the files and dirtorys in size.
page no :26
04. Reguler Linux Command
1. sed ‘s/sad/happy/’ file-name -- find and replace new word in every line first occre.
2. sed ‘s/sad/hay/g’ file-name -- find and replace every where on file.
3. sed ‘s/sad/hay/3’ file-name -- replacing the nth occurrence of in same line a file.
4. sed ‘ 4 s/sad/hay/’ file-name -- replacing the word in only fourth line of file.
5. sed -n ‘ 4 s/sad/hay/p’ file-name -- print only replaced line of file.
6. sed ‘ y/sd/no/’ file-name -- find and replace the character.
7. sed ‘ y/sa/no/’ file-name > new-file -- find and replace the character and save to new.
8. sed ‘3d’ file-name -- delete the 3rd line on file
9. sed ‘1,3d’ file-name --delete the line range
10. sed ‘/unix/d’ file-name -- delete the line where ever unix is there.
page no :28
04. Reguler Linux Command
1. cat filename | tr ‘a-z’ ‘A-Z’ -- to change all lower case into upper case.
2. cat filename | tr [:lower:] [:upper:] -- to change all lower case into upper case.
3. cat filename | tr -d character -- to delete the word in file.
4. wc file-name -- to count the no.of line,no.of words and no.of charater in file.
5. wc -l file-name -- count only no.of lines in file.
6. wc -c file-name -- count only no.of charters in file.
7. wc -w file-name -- count only no.of words in file.
8. sleep 10s --server will sleep 10s
9. cal;date -- display the cal and date at a same time
10. # commad -- # unused command
page no :30
04. Reguler Linux Command
1. cat filename1 && cat filename2 -- if first command success after that only next will
run.
2. cat filename1 || cat filename2 -- if first command will fail but secound will run.
3. history -- to check the history of excuted commands.
4. history -c -- to clear the history
5. history 10 -- to check the history of excuted last 10 commands.
6. ln sorce-name destiname -- copy the file and live connection between files.
7. ln -s source-name dest-name -- make the soft link for file path
8. apt install -- Advanced Packaging Tool (ubntu,debian)
9. yum install -- Red Hat Package Manager (RPM)[ Fedora, CentOS,
RHEL, etc.]
page no :31
FIVE
5 NETWORKING
page no :32
05. Networking Commands
Hard Link :
• A hard link acts as a copy (mirrored) of the selected file. It accesses the data available in
the original file.
• If the earlier selected file is deleted, the hard link to the file will still contain the data of
that file.
Soft Link :
• A soft link (also known as Symbolic link) acts as a pointer or a reference to the file
name. It does not access the data available in the original file.
• If the earlier file is deleted, the soft link will be pointing to a file that does not exist
anymore.
page no :35
07. Reguler Linux Command
Absolute Path
• An Absolute Path is a full path specifying the location of a file or directory from the root
directory or start of the actual filesystem.
• Example: /home/javatpoint/Desktop/CollegeStudent
Relative Path
• The relative path of a file is its location relative to the current working directory. It never
starts with a slash (/). It begins with the ongoing work directory.
• Single Dot (.) resolves to the current directory.
• Double Dot (..) resolves to the parent directory of the present work directory.
• Tilde (~) represents the home directory of logged in user.
Disk Partitioning page no :36
• Disk Partitioning is the process of dividing a disk into one or more logical areas,
often known as partitions, on which the user can work separately.
• It is one step of disk formatting.
• If a partition is created, the disk will store the information about the location
and size of partitions in the partition table.
Why we need it?
• To upgrade Hard Disk (to incorporate new Hard Disk to the system)
• Dual Booting (Multiple Operating Systems on the same system)
• Efficient disk management
• Ensure backup and security
• Work with different File Systems using the same system
Linux File System page no :37
• Shell is a UNIX term for an interface between a user and an operating system
service.
• Shell provides users with an interface and accepts human-readable commands
into the system and executes those commands which can run automatically and
give the program’s output in a shell script.
Types of shell
• Bash (Bourne Again SHell)
• c shell
• ksh shell (korn shell)
• zsh shell
Shell Scripting page no :47
• sh is also called Bourne Shell . sh is implemented by programs like dash, kash, and
original Bourne Shell.
• sh is not a programming language itself. It is just a specification.
• Users can provide human-readable commands to a shell and then shell convert
them into kernel understandable form.
• For avoiding manual and repeted work.
• Shell scripting is used by system admins for many routine backups.
crontab -e -- set up cronjob page no :48
crontab -l -- check the cron jobs
cd /root -- cron executed files if root is set-up cronjob
sample script install apache2 page no :49
#!/bin/sh #!/bin/sh
pwd apt update -y
mkdir demo apt install apache2 -y
cd demo sudo systemctl restart apache2
pwd sudo systemctl enable apache2
touch file.txt sleep 10s
echo “this new dirtory” > file.txt cd /var/www/html/
cp file.txt /home/ubuntu rm -rf index.html
ls -l echo “this is my script” > index.html
chmod 655 file.txt systemctl restart apache2
page no :50
script for send log file to aws s3
#!/bin/sh
pwd
cd /var/log/apache2/
#copy the access.log file to s3 bucket
aws s3 cp -r * s3://test234567875/
sleep 20s
# delete the file content without deleteing file
> access.log
page no :51
#!/bin/sh
apt update -y
apt install unzip -y
sleep 20s
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
10s
unzip awscliv2.zip
3m
sudo ./aws/install
1m
BASH SCRIPT page no :52
echo " Hello World! " #This is a single-line comment in Bash Script.
echo "Enter your name:"
Bash variables
read name
#! /bin/bash echo
# User-Defined Variables #echo output, its also a single line comment
name=Peter echo "The current user name is $name"
ROLL_NO=5245325 #This is another single line comment
echo "The student name is $name and his Roll
number is $ROLL_NO."
Bash Arithmetic Operators page no :54
Num1=10 Num1=10
Num2=3 Num2=3
A=$((Num1+Num2)) A=$((Num1*Num2))
echo "Sum = $A" echo "Sum = $A"
Num1=10 Num1=10
Num2=3 Num2=3
A= (Num1-Num2)) A=$((Num1/Num2))
#if condition (greater than) is true #if condition (equal to) is true
if [ 10 -gt 3 ]; if [ 10 -eq 10 ];
then then
echo "10 is greater than 3." echo "10 is equal to 10."
fi fi
#!/bin/bash #!/bin/bash
#!/bin/bash #!/bin/bash
#!/bin/bash
#!/bin/bash
for num in {1..10}
#For Loop to Read Three-expression
do
echo $num
for ((i=1; i<=10; i++))
done
do
echo "$i"
echo "Series of numbers from 1 to 10."
done
page no :58
while loop
#!/bin/bash
#Table of 2
#!/bin/bash
for table in {2..100..2} #An infinite while loop
do
echo $table while true
if [ $table == 20 ]; then do
break echo "Welcome to linux class.";
fi done
done
page no :59
bash sh
Bourne Again SHell SHell
bash is the default SHELL sh is the not default SHELL
#!/bin/bash #!/bin/sh
It has more Functionality with up- It has less functionality.
gradation.
Easy to use not as easy as bash
less portable than sh. more portable than bash.
Bash scripting is scripting specifically Shell scripting is scripting in any shell
for Bash
supports command history. does not supports command histoy.
Developed by Brain Fox Developed by Stephen R. Bourne
page no :60
THANK YOU
Click here to add content of the text.