NazmulHyderNeshat
310 views
14 slides
Nov 03, 2017
Slide 1 of 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
About This Presentation
I just shortly describe some Linux shell script and shell commands.Hopefully, it will help you to file edit, make, delete directory operations, grep, pipeline and lots of stuff to your Linux/Mac terminal.
Shell Command Identity and Manual Command Description $uame Linux will tell you his name to you $whoami If you want to know your name $man uname For each command manual
Continue.. File Listing Command Description $ls -a Do not hide entries starting with. $ls -h Print size in human readable format $ls -l Use a long listing format $ls -s Sort by file size
Continue.. Directory Operations Command Description $pwd If u want to see your current directory $cd “directory name” To change the directory $mkdir “directory name” Create a new directory $rmdir “directory name” Remove an empty directory $rmdir -r “directory name” Remove directory with file
Continue.. File Operations Command Description $cp “file” “copyfile.txt” Copy file $rm “file.txt” To remove file $mv “file.txt “ “directory name” Create a new directory $file -i “file name” To know the file type $file -v “file name” Print the version of the file $cat “file1” “file2” To concat files and print on the standard output
Continue.. File Viewing Command Description $echo “some text” You can a line of text in the shell $more file.txt It uses for the Spacebar and the B key for the forward and backward navigation $less “file.txt” Less allows forward and backward movement using arrow keys $head filename To see the first 10 contents of the file $tail filename To see the last 10 contents of the file
Continue.. File Viewing Command Description $filename -n To print the line number of the file $sort file1 file2 To write the sorted concatenation of all the file and standard output $wc filename Print bytes,words,total lines $wc -l or -w You can see specific
Continue.. Piping and Grep Command Description $head -10 a.txt | tail -5 To see 5-10 number lines of a file $grep “a” file.txt Print lines which contains “a” $grep -i For case insensitive search $grep -c Count total matches $grep -n Display the line number of matches
Continue.. Grep Command Description $grep -E [^F] filename Anything except capital F $grep -E [$a] filename Anything that ends with a $grep -E [a-zA-Z] filename Anything that contains small a to z and capital A to Z
Shell Script Advantage of shell script : It take input from user , file and output them on screen. Useful to create own commands. Save time.
Continue.. Write and Execute : We can use any editor to write the shell script. The extension is .sh Two thing you can need to do before execute file Chmod +x script_name “set execute permission for your script” ./script_name “execute script”
Continue.. Shell Script Examples A rithmetic if-else Case #!/bin/bash expr 1 + 1 expr 1 ‐1 expr 1 \* 1 expr 1 / 1 var=`expr 1 + 1` x=1 x=`expr $x + 1` #!/bin/bash echo "Enter first number " read num1 echo "Enter second number" read num2 if [ $num1 ‐gt $num2 ] ; then echo "$num1 is greater than $num2" elif [ $num1 ‐lt $num2 ] ; then echo "$num1 is less than $num2" else echo "$num1 and $num2 are equal" fi #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes) echo “Good Morning”;; no ) echo “Good Afternoon”;; y ) echo “Good Morning”;; n ) echo “Good Afternoon”;; * ) echo “Sorry, answer not recognized”;; esac