Five Linux tips to save you time!
What will I learn?
Below you will find five time-saving tips for navigating the Linux operating system more efficiently.
Requirements
You are expected to have some experience using Linux.
Difficulty
Basic
Let's go!
1) History Expansion
While history expansion can easily be its own article, I am going to show a few examples.
Run the last command that started with...
Say you used the command vim .bashrc and then did a few other things. If you know that was the last file you edited, you can type !vim to execute the last command that started with vim.
Forget to use sudo?
How many times have you tried to run a command that required escalation but you forgot to use sudo? Probably not as often as you should, because you are likely using root (shame on you!).
apt update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
Oops, this is where !! comes in handy, it will allow you to prefix your last command run.
sudo !! will be replaced with sudo apt get
Execute the last command that contains a string
Similar to the !vim command, there is also a search command if you just want to run something that contained a string but did not have to start with it.
vim /etc/nginx/nginx.conf
!?nginx will execute the following command again:
vim /etc/nginx/nginx.conf
These exclamation point commands will save you a lot of time while using Linux.
2) Kill a process by name
Most everyone is familiar with the ability to kill a process by pid. Did you know you can kill a process by name using killall?
Is a process hung or you want to abort it? Without using ps to find its pid you can use the following command to kill all instances of a process and it's child processes.
killall mysql
You can also use SIGKILL which is more successful for stubborn processes.
killall -SIGKILL mysql
3) Reset your terminal
This one is easy but is super helpful. Have you ever used a curses program that exits and leaves your terminal all funky looking? You likely just exit SSH and reconnect when this happens right?
Well now you will know to just type reset and your terminal will be as good as new.
4) Aliases
I love this trick, do you have a command you type often but it is a syntactical nightmare or you just want a shortcut?
In your .bashrc file, you can specify aliases that will be available to you when you are logged in.
The syntax is alias [alias name]='[command]'
Some good examples.
alias untar='tar -zxvf '
alias wget='wget -c '
alias ls='ls --color=auto'
and my favorite
alias yolo='git commit -am "DEAL WITH IT" && git push -f origin master'
These can be big time savers but they also can cause you to forget the real syntax of commands you use often. Keep that in mind and review them from time to time.
5) Get suggestions and tips for your shell scripts.
This is an amazing tip, there is a tool called shellcheck by Koalaman that will lint your shell scripts and give you feedback on how you can clean them up. If you are particularly weak at creating shell scripts, this will be your best friend.
Install shellcheck
sudo apt update && sudo apt install shellcheck
Once installed, all you need to do is type shellcheck <your script> and it will analyze your script and give you suggestions on how to clean it up. It will detect portability issues, beginner mistakes, style issues, data errors, and more.
Source
If you don't write shell scripts all day, this tool will become your best friend when the time comes to write one.
Hopefully, you found these helpful and they will save you some time. Let me know if you want to see more Linux tips or my previous Python Tips series.