Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 4: Essential Linux Commands'''= =='''Hack 17. Grep Command'''== grep command is used to search files for a specific text. This is incredibly powerful command with lot of options. Syntax: grep <nowiki>[options] </nowiki>pattern <nowiki>[files]</nowiki> '''How can I find all lines matching a specific keyword on a file?''' In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines. <nowiki># </nowiki>grep John /etc/passwd jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash Option -v, will display all the lines except the match. In the example below, it displays all the records from /etc/password that doesn't match John. '''Note: '''There are several lines in the /etc/password that doesn’t contain the''' '''word John. Only the first line of the output is shown below. <nowiki># </nowiki>grep -v John /etc/passwd jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash '''How many lines matched the text pattern in a particular file?''' In the example below, it displays the total number of lines that contains the text John in /etc/passwd file. <nowiki># grep -c John /etc/passwd 2</nowiki> You can also get the total number of lines that did not match the specific pattern by passing option -cv. <nowiki># grep -cv John /etc/passwd 39</nowiki> '''How to search a text by ignoring the case?''' Pass the option -i (ignore case), which will ignore the case while searching. <nowiki># </nowiki>grep -i john /etc/passwd jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash '''How do I search all subdirectories for a text matching a specific pattern?''' Use option -r (recursive) for this purpose. In the example below, it will search for the text "John" by ignoring the case inside all the subdirectories under /home/users. This will display the output in the format of "filename: line that matching the pattern". You can also pass the option -l, which will display only the name of the file that matches the pattern. <nowiki># </nowiki>grep -ri john /home/users /home/users/subdir1/letter.txt:John, Thanks for your contribution. /home/users/name_list.txt:John Smith /home/users/name_list.txt:John Doe <nowiki># </nowiki>grep -ril john /root /home/users/subdir1/letter.txt /home/users/name_list.txt =='''Hack 18. Find Command'''== find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command. Syntax: find <nowiki>[pathnames] </nowiki><nowiki>[conditions]</nowiki> '''How to find files containing a specific word in its name?''' The following command looks for all the files under /etc directory with mail in the filename. <nowiki># </nowiki>find /etc -name "*mail*" '''How to find all the files greater than certain size?''' The following command will list all the files in the system greater than 100MB. <nowiki># </nowiki>find / -type f -size +100M '''How to find files that are not modified in the last x number of days?''' The following command will list all the files that were modified more than 60 days ago under the current directory. <nowiki># </nowiki>find . -mtime +60 '''How to find files that are modified in the last x number of days?''' The following command will list all the files that were modified in the last two days under the current directory. <nowiki># </nowiki>find . –mtime -2 '''How to delete all the archive files with extension *.tar.gz and greater than 100MB?''' Please be careful while executing the following command as you don’t want to delete the files by mistake. The best practice is to execute the same command with ls –l to make sure you know which files will get deleted when you execute the command with rm. # find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec ls -l {} \; # find / -type f -name <nowiki>*.tar.gz </nowiki>-size +100M -exec rm -f {} \; '''How to archive all the files that are not modified in the last x number of days?''' The following command finds all the files not modified in the last 60 days under /home/jsmith directory and creates an archive files under /tmp in the format of ddmmyyyy_archive.tar. <nowiki># find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`</nowiki> On a side note, you can perform lot of file related activities (including finding files) using [http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ ][http://www.thegeekstuff.com/2008/10/midnight-commander-mc-guide-powerful-text-based-file-manager-for-unix/ midnight commander GUI], a powerful text based file manager for Unix. =='''Hack 19. Suppress Standard Output and Error'''== '''Message''' Sometime while debugging a shell script, you may not want to see either the standard output or standard error message. Use /dev/null as shown below for suppressing the output. '''Suppress standard output using > /dev/null''' This will be very helpful when you are debugging shell scripts, where you don’t want to display the echo statement and interested in only looking at the error messages. # cat file.txt > /dev/null # ./shell-script.sh > /dev/null '''Suppress standard error using 2> /dev/null''' This is also helpful when you are interested in viewing only the standard output and don’t want to view the error messages. # cat invalid-file-name.txt 2> /dev/null # ./shell-script.sh 2> /dev/null =='''Hack 20. Join Command'''== Join command combines lines from two files based on a common field. In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine the data from these two files using employee-id as shown below. $ cat employee.txt 100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma $ cat bonus.txt 100 $5,000 200 $500 300 $3,000 400 $1,250 $ '''join employee.txt bonus.txt''' 100 Jason Smith $5,000 200 John Doe $500 300 Sanjay Gupta $3,000 400 Ashok Sharma $1,250 =='''Hack 21. Change the Case'''== '''Convert a file to all upper-case''' $ cat employee.txt 100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma $ '''tr a-z A-Z <nowiki>< </nowiki>employee.txt''' 100 JASON SMITH 200 JOHN DOE 300 SANJAY GUPTA 400 ASHOK SHARMA '''Convert a file to all lower-case''' $ cat department.txt 100 FINANCE 200 MARKETING 300 PRODUCT DEVELOPMENT 400 SALES $ '''tr A-Z a-z <nowiki>< </nowiki>department.txt''' 100 finance 200 marketing 300 product development 400 sales =='''Hack 22. Xargs Command'''== xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively. 1. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem. find ~ -name ‘*.log’ -print0 | xargs -0 rm -f 2. Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs. # ## find /etc -name "*.conf" | xargs ls –l # If you have a file with list of URLs that you would like to download, you can use xargs as shown below. # ## cat url-list.txt | xargs wget –c # Find out all the jpg images and archive it. # ## find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz # Copy all the images to an external hard-drive. # ## ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory =='''Hack 23. Sort Command'''== Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has 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 2<sup>nd</sup> 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 3<sup>rd</sup> 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''' o '''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 5<sup>th</sup> 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 5<sup>th</sup> 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. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. Following are some common examples. 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. The first field in the following examples indicates how many duplicates where found for that particular line. So, in this example the lines beginning with Alex and Emma were found twice in the namesd.txt file. $ '''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 # 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. Following are some of the examples. '''Display the 1<sup>st</sup> 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 1<sup>st</sup> and 3<sup>rd</sup> 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. '''Display statistics of a file or directory.''' {| style="border-spacing:0;" | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /etc/my.cnf''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/etc/my.cnf' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div> | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 346 Blocks: 16 IO Block: 4096 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| regular file |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 801h/2049d | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 279856 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 1 |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div> | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0644/-rw-r--r--) Uid: ( | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| root) | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid:</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ( | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0/</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root)</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div> | colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2009-01-01 02:58:30.000000000 -0800 | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Modify:</div> | colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2006-06-01 20:42:27.000000000 -0700 | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Change:</div> | colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2007-02-02 14:17:27.000000000 -0800 | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat /home/ramesh''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| `/home/ramesh' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Size:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks: 8 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| IO Block: |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4096 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">directory</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Device:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 803h/2051d | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode: 5521409 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Links: 7 |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Access:</div> | colspan="4" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| (0755/drwxr-xr-x) Uid: ( 401/ramesh) | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Gid: (</div> |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 401/ramesh) | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} Access: 2009-01-01 12:17:42.000000000 -0800 Modify: 2009-01-01 12:07:33.000000000 -0800 Change: 2009-01-09 12:07:33.000000000 -080043 {| style="border-spacing:0;" | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| www.thegeekstuff.com |- | colspan="5" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Display the status of the filesystem using option –f''' | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''stat -f /''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">File:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| "/" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">ID:</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Namelen: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 255 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Type: ext2/ext3 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks:</div> | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 2579457 | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 2008027 | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Available: |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1876998</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Size: 4096 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Inodes:</div> | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Total: 1310720 | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free: 1215892 | colspan="0" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} =='''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 <nowiki>[options] </nowiki>file1 file2 '''What was modified in my new file when compare to my old file?''' The 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). # ## <nowiki>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 >. </nowiki> # '''diff -w name_list.txt name_list_new.txt ''' 2c2,3 <nowiki>< </nowiki>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''' With the option –d, it will break down the output for the individual days. In this example, I’ve been logged in to the system for more than 6 hours today. On Dec 1<sup>st</sup>, I was logged in for about 1 hour. {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''–d''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.08</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.99</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">3.39</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4.50</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">6.10</div> |} '''Connect time for all the users''' To display connect time for all the users use –p as shown below. Please note that this indicates the cumulative connect time for the individual users. $ '''ac -p''' {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| john | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3.64 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| madison | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.06</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| sanjay | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 88.17 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| nisha | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 105.92 |} ramesh111.42 total 309.21 '''Connect time for a specific user''' To get a connect time report for a specific user, execute the following: {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| $ '''ac''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''-d''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''sanjay''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Jul | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">12.85</div> |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Aug 25 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.05</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 3 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.03</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sep | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 4 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">5.37</div> |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 24 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">8.15</div> |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Dec 29 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1.42</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Today | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| total | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2.95</div> |}
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)