Chapter 4: Essential Linux Commands (part 2) __Linux 101 Hacks

Chapter 4: Essential Linux Commands

        __Linux 101 Hacks

Hack 23. Sort Command

Sort command sorts the lines of a text file.

    employee information in the format:

        employee_name:employee_id:department_name.

 

  
    
$ cat names.txt
Emma Thomas:
100 :Marketing
Alex Jason:
200 :Sales
Madison Randy:
300 :Product Development
Sanjay Gupta:
400 :Support
Nisha Singh:
500 :Sales

 


Sort a text file in ascending order

 

  
    
$ sort names.txt
Alex Jason:
200 :Sales
Emma Thomas:
100 :Marketing
Madison Randy:
300 :Product Development
Nisha Singh:
500 :Sales
Sanjay Gupta:
400 :Support


 

Sort a text file in descending order

 

  
    
$ sort - r names.txt
Sanjay Gupta:
400 :Support
Nisha Singh:
500 :Sales
Madison Randy:
300 :Product Development
Emma Thomas:
100 :Marketing
Alex Jason:
200 :Sales

 

 

 

Sort a colon delimited text file on 2nd field (employee_id)

 

  
    
$ sort - t: - k 2 names.txt
Emma Thomas:
100 :Marketing
Alex Jason:
200 :Sales
Madison Randy:
300 :Product Development
Sanjay Gupta:
400 :Support
Nisha Singh:
500 :Sales

 

 

 

Sort a tab delimited text file on 3rd field (department_name) and suppress duplicates

 

  
    
$ sort - t: - u - k 3 names.txt
Emma Thomas:
100 :Marketing
Madison Randy:
300 :Product Development
Alex Jason:
200 :Sales
Sanjay Gupta:
400 :Support

 

 

 

Sort the passwd file by the 3rd field (numeric userid)

 

代码
   
     
$ sort - t: - k 3n / etc / passwd | more
root:x:0:0:root:
/ root: / bin / bash
bin:x:
1 : 1 :bin: / bin: / sbin / nologin
daemon:x:
2 : 2 :daemon: / sbin: / sbin / nologin
adm:x:
3 : 4 :adm: / var / adm: / sbin / nologin
lp:x:
4 : 7 :lp: / var / spool / lpd: / sbin / nologin

Sort /etc/hosts file by ip-addres

 

 

代码
   
     
$ sort - t . - k 1 ,1n - k 2 ,2n - k 3 ,3n - k 4 ,4n / etc / hosts
127.0 . 0.1 localhost.localdomain localhost
192.168 . 100.101 dev - db.thegeekstuff.com dev - db
192.168 . 100.102 prod - db.thegeekstuff.com prod - db
192.168 . 101.20 dev - web.thegeekstuff.com dev - web
192.168 . 101.21 prod - web.thegeekstuff.com prod - web

 

Combine sort with other commands

 

代码
   
     
ps –ef | sort : Sort the output of process list

ls
- al | sort + 4n : List the files in the ascending order of the file - size. i.e sorted by 5th filed and displaying smallest files first.

ls
- al | sort + 4nr : List the files in the descending order of the file - size. i.e sorted by 5th filed and displaying largest files first.

 

 

Hack 24. Uniq Command

Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file.

1. When you have an employee file with duplicate entries, you can do the following to remove duplicates.

 

  
    
$ sort namesd.txt | uniq
$ sort –u namesd.txt

2. If you want to know how many lines are duplicates, do the following.

 

 

  
    
$ sort namesd.txt | uniq –c
2 Alex Jason: 200 :Sales
2 Emma Thomas: 100 :Marketing
1 Madison Randy: 300 :Product Development
1 Nisha Singh: 500 :Sales
1 Sanjay Gupta: 400 :Support

3. The following displays only the entries that are duplicates.

 

 

  
    
$ sort namesd.txt | uniq –cd
2 Alex Jason: 200 :Sales
2 Emma Thomas: 100 :Marketing

 

 

Hack 25. Cut Command

Cut command can be used to display only specific columns from a text file or other command outputs.

Display the 1st field (employee name) from a colon delimited file.

 

  
    
$ cut - d: - f 1 names.txt
Emma Thomas
Alex Jason
Madison Randy
Sanjay Gupta
Nisha Singh

Display 1st and 3rd field from a colon delimited file

 

 

  
    
$ cut - d: - f 1 , 3 names.txt
Emma Thomas:Marketing
Alex Jason:Sales
Madison Randy:Product Development
Sanjay Gupta:Support
Nisha Singh:Sales

Display only the first 8 characters of every line in a file

 

  
    
$ cut - c 1 - 8 names.txt
Emma Tho
Alex Jas
Madison
Sanjay G
Nisha Si

 

Misc Cut command examples

 

 

  
    
cut - d: - f1 / etc / passwd Displays the unix login names for all the users in the system.

free
| tr - s ' ' | sed ' /^Mem/!d ' | cut - d " " - f2 Displays the total memory available on the system.

 

 

 

 

Hack 26. Stat Command

Stat command can be used either to check the status/properties of a single file or the filesystem.

 

代码
   
     
$ stat / etc / my.cnf
File: `
/ etc / my.cnf '
Size: 346 Blocks: 16 IO Block: 4096
Device: regular file Inode:
279856 Links: 1
Access:((
0644 /- rw - r -- r -- ) Uid: (0 / root) Gid:(0 / root)
Access:
2009 - 01 - 01 02 : 58 : 30.000000000 - 0 800
Modify:
2006 - 06 - 01 20 : 42 : 27.000000000 - 0700
Change:
2007 - 02 - 02 14 : 17 : 27.000000000 - 0 800
代码
   
     
$ stat / home / ramesh
File: `
/ home / ramesh '
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h
/ 2051d Inode: 5521409 Links: 7
Access: (
0755 / drwxr - xr - x) Uid: ( 401 / ramesh) Gid: ( 401 / ramesh)
Access:
2009 - 01 - 01 12 : 17 : 42.000000000 - 0 800
Modify:
2009 - 01 - 01 12 : 07 : 33.000000000 - 0 800
Change:
2009 - 01 - 0 9 12 : 07 : 33.000000000 - 0 800

Display the status of the filesystem using option –f

 

 

  
    
$ stat - f /
File:
" / "
ID: 0 Namelen:
255 Type: ext2 / ext3
Blocks: Total:
2579457 Free: 2008027 Available: 1876998 Size: 4096
Inodes: Total:
1310720 Free: 1215892

 

 

 

Hack 27. Diff Command

diff command compares two different files and reports the difference. 

The output is very cryptic and not straight forward to read.

Syntax: diff [options] file1 file2

option -w in the diff command will ignore the white space while performing the comparison.

    In the following diff output:

        The lines above ---, indicates the changes happened in first file in the diff command (i.e name_list.txt).

        The lines below ---, indicates the changes happened to the second file in the diff command (i.e name_list_new.txt). The lines that belong to the first file starts with < and the lines of second file starts with >.

 

  
    
# diff -w name_list.txt name_list_new.txt
2c2, 3
< John Doe
---
> John M Doe
> Jason Bourne

 

 

Hack 28. Display total connect time of users

ac command will display the statistics about the user’s connect time.

Connect time for the current logged in user

option –d will break down the output for the individual days. 

Connect time for all the users

Option –p To display connect time for all the users.

 

  
    
# ac -d

# ac -p

Connect time for a specific user
To get a connect time report for a specific user, execute the following:

  
    
$ ac - d sanjay

 

 

 

Chapter 4: Essential Linux Commands (part 1) __Linux 101 Hacks

 

__ end.

你可能感兴趣的:(command)