Friday, June 29, 2007

commands

1. List the files in current directory sorted by size ? - ls -l grep ^- sort -nr
2. List the hidden files in current directory ? - ls -a1 grep "^\."
3. Delete blank lines in a file ? - cat sample.txt grep -v ‘^$’ > new_sample.txt
4. Search for a sample string in particular files ? - grep “Debug” *.confHere grep uses the string “Debug” to search in all files with extension“.conf” under current directory.
5. Display the last newly appending lines of a file during appendingdata to the same file by some processes ? - tail –f Debug.logHere tail shows the newly appended data into Debug.log by some processes/user.
6. Display the Disk Usage of file sizes under each directory in currentDirectory ? - du -k * sort –nr (or) du –k . sort -nr
7. Change to a directory, which is having very long name ? - cd CDMA_3X_GEN*Here original directory name is – “CDMA_3X_GENERATION_DATA”.
8. Display the top most process utilizing most CPU ? - top –b 1
9. Set the Display automatically for the current new user ? - export DISPLAY=`eval ‘who am i cut -d"(" -f2 cut -d")" -f1′`Here in above command, see single quote, double quote, grave ascent is used. Observe carefully.
10. Display the processes, which are running under yourusername ? - ps –aef grep MaheshvjHere, Maheshvj is the username.
11. List some Hot Keys for bash shell ? - Ctrl+l – Clears the Screen. Ctrl+r – Does a search in previously given commands in shell. Ctrl+u - Clears the typing before the hotkey. Ctrl+a – Places cursor at the beginning of the command at shell. Ctrl+e – Places cursor at the end of the command at shell. Ctrl+d – Kills the shell. Ctrl+z – Places the currently running process into background.
12. Display the files in the directory by file size ? - ls –ltr sort –nr –k 5
13. How to save man pages to a file ? - man col –b > Example : man top col –b > top_help.txt
14. How to know the date & time for – when script is executed ? - Add the following script line in shell script.eval echo "Script is executed at `date`" >> timeinfo.infHere, “timeinfo.inf” contains date & time details ie., when script is executed and history related to execution.
15. How do you find out drive statistics ? - iostat -E
16. Display disk usage in Kilobytes ? - du -k
17. Display top ten largest files/directories ? - du -sk * sort -nr head
18. How much space is used for users in kilobytes ? - quot -af
19. How to create null file ? - cat /dev/null > filename1
20. Access common commands quicker ? - ps -ef grep -i $@
21. Display the page size of memory ? - pagesize -a
22. Display Ethernet Address arp table ? - arp -a
23. Display the no.of active established connections to localhost ? - netstat -a grep EST
24. Display the state of interfaces used for TCP/IP traffice ? - netstat -i
25. Display the parent/child tree of a process ? - ptree Example: ptree 1267
26. Show the working directory of a process ? - pwdx Example: pwdx 1267
27. Display the processes current open files ? - pfiles Example: pfiles 1267
28. Display the inter-process communication facility status ? - ipcs
29. Alternative for top command ? - prstat -a
30. Run grep for multiple strings? - grep "string1\string2"
31 search multiple strings on the same line egrep 'string1.*string2' filename
32. how to remove to values from using sed - sed 's/value1\value2//g'

Sed
----
change value in a file recursively and display ? - sed 's/oldvalue/newvalue/g' filename
Remove a special character from file eg; "/" - sed -e 's,/,,g' Test - Change the delimiter to ","
Use underscore ( _) as a paramter when you are trying to search and replace anything that involves front/backslash - This avoids the delimiter and the front slash confussion
Eg: To change /usr/sbin/test to /var/log/test in a file
1. sed 's_/usr/sbin/test_/var/log/test_g' file > file-new
2. sed -s '/startline/,/endline/p' filename -- This will collect all the lines between startline and endline including both startline and endline.
3. sed -n 5p -- Will print the line5 from the file. - To read a particular line from a file
4. awk NR==5 -- -- Will print the line5 from the file. - To read a particular line from a file
5. cat output| awk '{FS=" " ; $0=$0 ; print $NF"|"$0}' | sort -rn | cut -d"|" -f2 |head -15 -- Sort file based on the last field with irregular number of fields

5. From line
open("/lib64/librt.so.1", O_RDONLY) = 3
----------------
$sed -n 5p ls.output |
sed 's/.*.1//g'
", O_RDONLY) = 3
# Will show everything after .1 from the above line
------------------
$sed -n 5p ls.output |
sed 's/1.*//g'
open("/lib64/librt.so.
# Will show everything before 1 as output

Good Link for SED - http://www.grymoire.com/Unix/Sed.html
Another very good link for sed http://sed.sourceforge.net/sed1line.txt

find
----
Display the all files recursively - find . -depth -print
Search file recursively - find /home/david -name 'index*'
Search file recursively (Case insensitive) -find /home/david -iname 'index*'
find /mp3collection -name '*.mp3' -size -5000k
$ find / -size +10000k
ind /home/david -amin -10 -name '*.c'
$ find /home/david -atime -2 -name '*.c'
$ find /home/david -mmin -10 -name '*.c'
$ find /home/david -mtime -2 -name '*.c'
ind /mp3-collection -name 'Metal*' -and -size +10000k
$ find /mp3-collection -size +10000k ! -name "Metal*"
$ find /mp3-collection -name 'Metal*' -or -size +10000k

Cut
----
Cut output from the line to get all usernames from the file - Eg.filename.txt
/kolab/var/imapd/spool/domain/m/sample.domain/s/user/suresh/Calendar/1.
/kolab/var/imapd/spool/domain/m/sample.domain/s/user/sathish/Calendar/11.

cat filename.txt cut -d/ -f11
Explaination - This will set "/" as delimiter and gives field 11 - suresh and sathish as output


Track command history based on time
export HISTTIMEFORMAT="%x %X " in .bashrc of shell

Command to extract contents from an RPM file
rpm2cpio cpio -ivd

Command to view the contents of an RPM file
rpm -qpil /path/to/the/rpmfile

No comments: