Linux Basics commands - Part 2

Linux Basics commands - Part 2

How to print the particular line in a file

1.prints all the line in the file

  sed 'p' file.txt         

2. Prints the 2nd line in the file

sed -n '2p' file1.txt         

head and tail to print the lines

  1. to print the first 5 line

head -n 5 file1.txt         
head -5 file.txt        

Head by default will print 10 lines

2. To print the last 5 lines

tail -n 5 file1.txt        
tail -5 file.txt        

By Default tail will print last 10 lines

Pipe Command (|)

cat file1.txt | grep -i 'Unix'        

Pipe acts as a carrier, in other words , whatever the output of one command will be fed as input to the second command.

Separator: (;)

if you want to execute multiple commands at the single line then you can use separator.

eg:

cat file1.txt ; cat file2.txt ; grep 'system' file1.txt        

Binary Operations (&& and || )

grep 'system' file1.txt && echo "Found"        
grep 'system' file1.txt && echo "Found" || echo "Not Found"        

To delete the Files.

rm filename.txt        

args --

Args will helps to feed one by one to the argument . we can control where to feed using args.

eg:

find . -type f -iname "file.*" | xargs -1{} rm{}        

Cut Command:

cut command is used to get the data by bifurcating them with the delimiter given.

in the below command -d stands for delimiter

cut -d ' ' -f1 emp.txt         

cut always works with single delimiter and cant take the delimiter more than one.

AWK:

1.awk is used to perform below functions

2.Scans a file line by line

3.Splits each input line into fields (based on the delimiter option)

4.Compares input line/fields to pattern

5.Performs action on matched lines

Eg:

awk -F ' ' '{print $3}'        

-F is the Field Separator

The below line gives the lines till the A comes and prints the first column

awk -F 'A' '{print $1}' emp.txt        
awk -F ' ' '{print $NF}' emp.txt         

The above command gives the Last line which is $NF

Disk Usage:

du (disk usage) and df (disk File system)

1.df - df gives the holistic picture of the disk usage of the complete file system.

2.du - This gives the the accurate consumption of a given directory.

we often use -h with these commands , which means human readable form

du -h /home/var/        
df -h         

If you want to know the information of all the process running , then it is ps command

ps -ef        


要查看或添加评论,请登录

社区洞察

其他会员也浏览了