1. List all processes whose commands match firefox

ps aux | grep firefox
ps ax|grep firefox
ps -e|grep firefox
pgrep firefox

 

2. List all processes whose entire command lines match firefox

pgrep -f firefox

 

3. List all processes whose commands exactly match firefox

pgrep -x firefox

 

4. List files name starting from “2016_06_01” and text contains “ExchangeRatesUpdater” (with the containing line)

grep -r --include="2016_06_01*" "ExchangeRatesUpdater"

 

4. List files names ONLY that containes HAPPYBKNGSUS108889 text inside the /var/log/rezg/app/hotelxml folder

grep -rl HAPPYBKNGSUS108889 /var/log/rezg/app/hotelxml

 

5. List all files in ride-server-1.0.0.jar whose name is start with “ExchangeRatesUpdater”

jar -tvf ride-server-1.0.0.jar | grep "ExchangeRatesUpdater"

 

6. List all classes in a jar order by size

jar -tvf ride-server-1.0.0.jar | sort -n -r

 

7. Finding a file inside a jar.

7.1 To prints all the classes contains the class name with package structure.

find filePath -name '*.jar' | xargs -I @ jar -tvf @ | grep ClassName

7.2 To prints all the classes contains the class name with package structure.

find filePath -name '*.jar' | xargs -I @ jar -tvf @ | grep /ClassName

7.3 To prints the jar name if the class exists then prints the class name.

find filePath -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep ClassName.class) done

7.4 To prints the jar name if the class exsists then prints the class name. (exact match only)

find filePath -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep /ClassName.class) done

 

8. Finding the process (with an id) that is running on a particular port.

netstat -lpn | grep 9001

Note : Here, 9001 is the particular port number.

You can get all the ports using following command

sudo netstat -tulpn

Use grep as follows to filter a specific port.

sudo netstat -tulpn | grep 9001

 

9. tail commands

9.1. Outputs the last 10 lines of the file myfile.txt.

tail myfile.txt

9.2. Outputs the last 100 lines of the file myfile.txt.

tail myfile.txt -n 100

9.3. Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates;

tail -f myfile.txt

9.4. Selectively monitor a log file in real time.

 tail -f access.log | grep 24.10.160.10 

 

10. vi commands
10.1. Start vi
If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

* vi filename edit filename starting at line 1
vi -r filename recover filename that was being edited when system crashed

 

10.2. Adding / Changing / Deleting text

* i insert text before cursor, until <Esc> hit
I insert text at beginning of current line, until <Esc> hit
* a append text after cursor, until <Esc> hit
A append text to end of current line, until <Esc> hit
* o open and put text in a new line below current line, until <Esc> hit
* O open and put text in a new line above current line, until <Esc> hit

* u UNDO WHATEVER YOU JUST DID; a simple toggle

 

10.3. Searching text

/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction

 

10.4. Saving and Reading

:r filename<Return> read file named filename and insert after current line
(the line with cursor)
:w<Return> write current contents to file named in original vi call
:w newfile<Return> write current contents to a new file named newfile
:12,35w smallfile<Return> write the contents of the lines numbered 12 through 35 to a new file named smallfile
:w! prevfile<Return> write current contents over a pre-existing file named prevfile

10.5. Exit vi

* 😡<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call

Reference

Advertisements