When it comes to Linux, most of the time you would be working on the command line. With the advent of configuration management tools like Chef, Ansible etc, developers can now work on Linux system automation efficiently without worrying much about the system internals. However, working with the command line is required at any state of the process. Let it be debugging, editing a configuration or testing an application for the first time. Linux command line tips will increase your productivity. You can avoid lots of repeated tasks using these tips and shortcuts.
Linux Command Line Tips
In this article, we have listed out the command hacks that you could use in a Linux terminal for increasing your productivity.
Note: In all the examples explained below, “+
” means just the combination. It does not mean you have to use the “+” button in keyboard.
Search Previously Executed Command
You can search for a previously executed command using the reverse search functionality.
Use “Ctrl + R
” button and type the keyword to search. It will return the matching command executed before.
Delete the last Word and Line
When you are typing,
1. If you want to delete the last word, use “Ctrl + W
”
2. If you want to delete the whole line, use “Ctrl + U
”
3. If you want to enter next line, use “Ctrl + C
”
Also read: Linux networking and troubleshooting commands
Find and kill and process
Let’s say you want to find a process id and kill it. You can do this using the following command.
ps -aef | grep <service name> | grep -v grep | awk '{print $2}' | xargs kill -9
List out all process and Listening ports
Use the following command to list all the listing ports.
netstat -tlnp
Process related
1. Use pgrep
to get the process id of a service.
pgrep <service name>
And you can use xargs
to kill all the related processes.
pgrep <service name> | xargs kill -9
2. User nohup
and &
to send a process to the background and run forever.
nohup <some process script> &
Output as file Input
You can use the output of a command as a file to be the input of another command using “<
” symbol.
For example, if you want to compare the /etc/hosts of a remote host with your current host, you can do the following.
diff /etc/hosts <(ssh user@<IP> -i ~/.ssh/key-file cat /etc/hosts)
Suppress the standard output
If you want to suppress the standard output, you can redirect the stdout to /dev/null
For example,
ls -l > /dev/null
File Permissions in Octal form
To view the file permissions in octal form, you can use the following syntax.
stat -c '%A %a %n' <file name>
disk/CPU/network status
To know disk/CPU/network status you could use the following command line utilities.
iostat netstat top htop dstat vmstat
Other useful Tips
1. Use Ctrl + Z
to suspend a process
2. Use grep
for filtering outputs
3. Use !!
to execute the last command
4. Use dmesg
command to identify hardware or driver problems
5. Use the mtr
command to troubleshoot network issues.
6. To empty a file use > followed by the filename. For example >example.txt
For example,
mtr 8.8.8.8
6. Use pstree
command to view the process tree of your system.
Useful Links
1. Linux process management tutorial for beginners.
2. Linux commands every developer should know.