If you are a hard core systems administrator or Linux engineer you’ll probably recognize most of these Linux command line tricks. The following Linux command line tips are not typically used by your everyday Linux user.
Quickly Find a PID with pgrep
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
This will list all PIDs associated with the ssh process.
Execute The Last Executed Command
The heading sounds a bit confusing but it’s exactly what it does.
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 a command from history starting with the letter S you can use the following:
This will execute the last command used on the command line that started with s.
Run a Command Repeatedly and Display the Output
watch runs command repeatedly, displaying its output (the first screenfull). 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:
Navigate to the Last Directory You Were In
cd - will take you to the last directory you were in.
Make Parent Directories the Smart Way
mkdir -p /home/adam/make/all/of/these/directories/ will create all directories as needed even if they do not exist. Why waste time doing something silly like:
mkdir make ; cd make ; mkdir all ; cd all ; mkdir of ; cd of … you get the point. Use
Delete the 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:
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).
Can you think of any other Linux commands that are less known to the general Linux community?
Subscribe to my RSS feed here.