Quickly Find a PID
pgrep
looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
pgrep ssh
This will list all PIDs associated with the ssh process.
Execute The Last Command
!!
This will execute the last command you used on the command line.
Execute The Last Command Starting With…
If you want to execute a command from history starting with the letter S you can use:
!s
This will execute the last command used on the command line that started with s.
Repeatedly Run a Command and Display the Output
watch
runs a command repeatedly, displaying its output. This allows you to watch the program output change over time. By default, the program is run every 2 seconds. watch
is very similar to tail
.
watch -d ls -l
This will watch the current directory for any file changes and highlight the change when it occurs.
Save Quickly in VI/VIM
If you’re in a hurry, you can save and quit the file you’re editing in vi by exiting insert mode, holding shift, and hitting z twice.
Quickly Log Out of a Terminal
You can quickly log out of a terminal session by using: CTRL+D
Navigate to the Last Directory
cd -
will take you to the last directory you were in.
Make Parent Directories the Smart Way
mkdir -p /home/danny/make/all/of/these/directories/
will create all directories as needed even if they do not exist. Why waste time doing something like: mkdir make ; cd make ; mkdir all ; cd all ; mkdir of ; cd of
… you get the point. Use mkdir -p
!
Delete an Entire Line
If you’ve just typed a long string of commands that you don’t need to enter anymore, delete the entire line by using: CTRL+U
.
Set the Time stamp of a File
touch -c -t 0801010800 filename.c
will show the time stamp as 2008-01-01 8:00. The format is (YYMMDDhhmm).