Basis Commands:-----
===================================================
1) pwd command
‘pwd’ command prints the absolute path to current working directory.
2) cal command
Displays the calendar of the current month.
3) echo command
This command will echo whatever you provide it.
4) date command
Displays current time and date.
5) tty command
Displays current terminal.
6) whoami command
This command reveals the user who is currently logged in.
7) clear command
This command clears the screen.
8) help option
With almost every command, ‘--help’ option shows usage summary for that command.
9) whatis command
This command gives a one line description about the command. It can be used as a quick reference for
any command.
10) Changing Directories Command
$ cd [path-to-directory]
11) Listing File And Directories Command
$ ls [files-or-directories]
12) Listing File And Directories Command
$ ls [files-or-directories]
13) touch command
For creating an empty file, use the touch command.
$ touch file1 file2 file3
14) copy command
$cp source destination
15) move command
$ mv source destination
16) To remove or Delete
$ rmdir
To remove files and directories
$ rm files|directories
17) cat command
The 'cat' command is actually a concatenator but can be used to view the contents of a file.
18) head command
Displays the first few lines of a file. By default, the ‘head’ command displays the first 10 lines of a file.
But with -n option, the number of lines to be viewed can be specified.
19) tail command
Similar to ‘head’; the ‘tail’ command shows the last 10 lines by default, and -n option is available as well.
20) wc command
Word count
This command counts lines, words and letters of the input given to it.
21) grep command
$ grep nologin /etc/passwd
22) VI editor
The VI stands for Visual editor; another text editor in Linux. This is a standard editor in many Linux/Unix
environments.
$ vi hello.txt
23) alias command
The ‘alias’ is another name for a command. If no argument is given, it shows current aliases. Aliases can
be used for short names of commands. For example, you might use the clear command frequently. You
can create an alias for it:
$ alias c="clear"
24) w command
w command is used to check which users are logged in to the system, and what command they are
executing at that particular time:
25) last command
Displays information about the users who logged in and out of the system. The output of the last
command can be very large, so the following output has been filtered (through head) to display the top
10 lines only:
26) du command
The du command determines disk usage of a file. If the argument given to it is a directory, then it will list
disk usage of all the files and directories recursively under that directory:
27) df command
The df reports file system usage. For example:
$ df
28) history command
29) passwd command
30) Shutdown Command
In Linux, you can use shutdown command to gracefully halt your system. Most commonly used
command is
shutdown -h now
================================================================
1) Simple Message print
Use an editor like vim or nano to create the file hello-world.sh
and witer into it below line:-
#!/bin/bash
echo "Hello World"
File permission must have to like
$ chmod a+x hello-world.sh
when you want to run the file then use below command
$ bash hello-world.sh
$ ./hello-world.sh
2) Some number of line formet like :-
#!/bin/bash
echo "Printing text"
echo -n "Printing text without newline"
echo -e "\nRemoving \t special \t characters\n"
3)Adding two values
#!/bin/bash
# Adding two values
((sum=25+35))
#Print the result
echo $sum
4)Multi-line comments
#!/bin/bash
:'
This script calculates
the square of 5.
'
((area=5*5))
echo $area
5)The While Loop
Syntax:-
while [ condition ]
do
commands 1
commands n
done
#!/bin/bash
i=0
while [ $i -le 2 ]
do
echo Number: $i
((i++))
done
6)The For Loop
#!/bin/bash
for (( counter=1; counter<=10; counter++ ))
do
echo -n "$counter "
done
printf "\n"
7) Receive Input from User
#!/bin/bash
echo -n "Enter Something:"
read something
echo "You Entered: $something"
8. The If Statement
Syntax
if CONDITION
then
STATEMENTS
fi
#!/bin/bash
echo -n "Enter a number: "
read num
if [[ $num -gt 10 ]]
then
echo "Number is greater than 10."
fi
9. More Control Using If Else
#!/bin/bash
read n
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
10. Using the AND Operator
#!/bin/bash
echo -n "Enter Number:"
read num
if [[ ( $num -lt 10 ) && ( $num%2 -eq 0 ) ]]; then
echo "Even Number"
else
echo "Odd Number"
fi
11. Using the OR Operator
#!/bin/bash
echo -n "Enter any number:"
read n
if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won"
else
echo "You lost!"
fi
12. Using Elif (Else IF)
#!/bin/bash
echo -n "Enter a number: "
read num
if [[ $num -gt 10 ]]
then
echo "Number is greater than 10."
elif [[ $num -eq 10 ]]
then
echo "Number is equal to 10."
else
echo "Number is less than 10."
fi
13. The Switch Construct
#!/bin/bash
echo -n "Enter a number: "
read num
case $num in
100)
echo "Hundred!!" ;
200)
echo "Double Hundred!!" ;
*)
echo "Neither 100 nor 200" ;
esac
14)Concatenating Strings
#!/bin/bash
string1="Ubuntu"
string2="Pit"
string=$string1$string2
echo "$string is a great resource for Linux beginners."
15). Slicing Strings
#!/bin/bash
Str="Learn Bash Commands from UbuntuPit"
subStr=${Str:0:20}
echo $subStr
16) Adding Two Values (Input by user)
#!/bin/bash
echo -n "Enter first number:"
read x
echo -n "Enter second number:"
read y
(( sum=x+y ))
echo "The result of addition=$sum"
17)Adding Multiple Values
#!/bin/bash
sum=0
for (( counter=1; counter<5; counter++ ))
do
echo -n "Enter Your Number:"
read n
(( sum+=n ))
#echo -n "$counter "
done
printf "\n"
echo "Result is: $sum"
18) Functions in Bash
#!/bin/bash
function Add()
echo -n "Enter a Number: "
read x
echo -n "Enter another Number: "
read y
echo "Adiition is: $(( x+y ))"
Add
19)Creating Directories from Bash Scripts
#!/bin/bash
echo -n "Enter directory name ->"
read newdir
cmd="mkdir $newdir"
eval $cmd
20) Create a Directory after Confirming Existence
#!/bin/bash
echo -n "Enter directory name ->"
read dir
if [ -d "$dir" ]
then
echo "Directory exists"
else
`mkdir $dir`
echo "Directory created"
fi
21) Deleting Files
#!/bin/bash
echo -n "Enter filename ->"
read name
rm -i $name
22) Appending to Files
#!/bin/bash
echo "Before appending the file"
cat editors.txt
echo "6. NotePad++" >> editors.txt
echo "After appending the file"
cat editors.txt
23) Send Mails from Shell Scripts
#!/bin/bash
recipient=”[email protected]”
subject=”Greetings”
message=”Welcome to UbuntuPit”
`mail -s $subject $recipient <<< $message`
24) Removing Duplicate Lines from Files
#! /bin/sh
echo -n "Enter Filename-> "
read filename
if [ -f "$filename" ]; then
sort $filename | uniq | tee sorted.txt
else
echo "No $filename in $pwd...try again"
fi
exit 0
25) The Sleep Command
#!/bin/bash
echo "How long to wait?"
read time
sleep $time
echo "Waited for $time seconds!"
26) The Wait Command
#!/bin/bash
echo "Testing wait command"
sleep 5 &
pid=$!
kill $pid
wait $pid
echo $pid was terminated.
27) Displaying the Last Updated File
#!/bin/bash
ls -lrt | grep ^- | awk 'END{print $NF}'