Power of Linux command line
- Use
grep
utility to filter out your output. Find all Java processes:
ps -AF | grep java
- How to see terminal history (commands executed in the terminal):
history
The command supports scrolling with up / down arrows. Use filtering by substring to find specific command:
history | grep ssh
- How to find files by name:
from current folder:find . -name '*Listener.java'
from root:find / -name '*.txt'
- How to find files by content:
grep -lir 'dialog.show()' *.java
arguments:
-l show only files with matches
-i ignore case
-r recursive - Remove files by extension in the current folder and its subfolders:
find -name *.pyc -print0 | xargs -0 rm -rf
- Search substring in the file:
less -pYour_pattern filename
- How to print file content:
cat filename
cat filename | less
less filename
less
command supports scrolling (up / down keys), search (press '?' or '/', type pattern and press Enter). Press 'q' or Ctrl-C to exitless
viewer.The scroll forward command (press 'F' in the
less
viewer) is useful when viewing logs. It scrolls forward if the end of file is reached but the file is growing. - Use
kill
command to terminate a process. The command requires an integer argument - process ID. Theps
command will help to find out the process id.For example, if you need to kill a Java process, execute:
ps -AF | grep java
and if you see only one process that's it! If there are several java processes then find yours by command line parameters, user or executable path.
The 2nd column (1st integer) in each process row is the process ID.
- If you can't terminate your process using:
kill processId
then execute force kill:
kill -9 processId
- ssh command - terminal on remote computer:
ssh username@host/folder
username and folder are optional. - Transfer files from / to remote computer:
scp username@remoteHost:remoteFile localFile
scp localFile username@remoteHost:remoteFile
- How to launch a program on remote computer that the program will not be terminated when you close the remote computer session.
- Open screen manager:
screen -r -d
- Create a new window in it:
ctrl p c
- Launch your program
- Leave screen manager:
ctrl p d
- Open screen manager:
- Display free disk space:
df
- How to see which program listens to specific port?
netstat -lpn | grep port_no
The
-l
parameter shows programs that listen for connections,-p
shows processes PIDs,-n
doesn't change port numbers to names.