- Downloaded
- 62.3 GB
- Uploaded
- 11.3 TB
- Ratio
- 186.25
- Seedbonus
- 1,020
- Upload Count
- 89 (104)
Member for 4 years
Here are some tiny but useful Linux commands, terminal tricks and shortcuts that will save you a lot of time while working with Linux command line.
Some of these tips depend on how your shell is configured.
For example, if you are trying to copy a file named my_test_file_1.txt, you can just type ‘cp m’ and hit tab to see the possible options.
You can use tab in completing commands as well. In case there is only 1 file, the tab key pressed twice also enables to complete the command.
This will revert to the last working directory. You don’t need to type the long directory path or copy paste it anymore.
However, you can also use just cd to go back to home directory:
Most modern Linux distributions have their shell pre-configured for this command. It saves you at least two keystrokes.
However, if your shell is configured to allow, then use:
- Double L
You can use the ‘;’ separator for this purpose. This way, you can run a number of commands in one line. No need to wait for the previous commands to finish the execution.
Imagine a situation where you want to build a code and then if the build was successful, run the make?
You can use the && separator for this case. && makes sure that the next command will only run when the previous command was successful.
A perfect example of this command is when you use sudo apt update && sudo apt upgrade to upgrade your system.
Reverse search is your savior here. You can search for the command in the history using a search term.
Just use the keys ctrl+r to initiate reverse search and type some part of the command. It will look up into the history and will show you the commands that matches the search term.
By default, it will show just one result. To see more results matching your search term, you will have to use ctrl+r again and again. To quit reverse search, just use Ctrl+C.
Note that in some Bash shells, you can also use Page Up and Page Down key with your search term and it will autocomplete the command.
Just use Ctrl+Q and you can use the terminal again.
You can use Home and End keys here of course but alternatively, you can use Ctrl+A to go to the beginning of the line and Ctrl+E to go to the end.
In the Linux terminal, if you press Ctrl+U, it deletes everything from your current cursor position to the beginning of the line.
Similarly, if you press Ctrl+K, it deletes everything from your cursor position to the end of the line.
Possible made a mistake in typing the password? Instead of using backspace key all the way, simply use Ctrl+U and retype the password.
You can also use the regular grep options to display only those lines that are meaningful to you:
You can also use the option F here. This will keep the tail running even if the log file is deleted. So if the log file is created again, tail will continue logging.
Thankfully, z commands save you in such situations. z commands provide alternatives of the regular commands that you use to deal with log files such as less, cat, grep etc.
So you get zless, zcat, zgrep etc and you don’t even have to explicitly extract the compressed files.
You can use Vi, Vim or other terminal based text editors but if you just want to read a file, less command is a far better choice.
You can search for terms inside less, move by page, display with line numbers etc.
Say you have to create a directory and then go into the newly created directory. There you can use the !$ options.
A better way to do the same is to use alt+. . You can use . a number times to shuffle between the options of the last commands.
A quick sudo !! saves plenty of keystrokes here.
Another way if your shell configuaration provides is by pressing the up arrow key, then home key and typing sudo. Though this is a longer method, it works if one cannot recall !! command.
This way you won’t have to retype the command again.
Select the text for copying and right click for paste (works in Putty and other Windows SSH clients)
Select the text for copying and middle click (scroll button on the mouse) for paste
Ctrl+Shift+C for copy and Ctrl+Shift+V for paste
Just use it in the below fashion:
Another example:
Find if there are files containing a particular text
There are multiple ways to search and find in Linux command line. But in the case when you just want to see if there are files that contain a particular text, you can use this command:
Just use it in this fashion:
Some of these tips depend on how your shell is configured.
- Using tab to autocomplete
For example, if you are trying to copy a file named my_test_file_1.txt, you can just type ‘cp m’ and hit tab to see the possible options.
You can use tab in completing commands as well. In case there is only 1 file, the tab key pressed twice also enables to complete the command.
- Switch back to the last working directory
Code:
cd -
This will revert to the last working directory. You don’t need to type the long directory path or copy paste it anymore.
- Go back to home directory
Code:
cd ~
However, you can also use just cd to go back to home directory:
Code:
cd
Most modern Linux distributions have their shell pre-configured for this command. It saves you at least two keystrokes.
- List the contents of a directory
However, if your shell is configured to allow, then use:
Code:
ll
- Running multiple commands in one single command
You can use the ‘;’ separator for this purpose. This way, you can run a number of commands in one line. No need to wait for the previous commands to finish the execution.
Code:
command_1; command_2; command_3
- Running multiple commands in one single command only if the previous command was successful
Imagine a situation where you want to build a code and then if the build was successful, run the make?
You can use the && separator for this case. && makes sure that the next command will only run when the previous command was successful.
Code:
command_1 && command_2
A perfect example of this command is when you use sudo apt update && sudo apt upgrade to upgrade your system.
- Easily search and use the commands that you had used in the past
Reverse search is your savior here. You can search for the command in the history using a search term.
Just use the keys ctrl+r to initiate reverse search and type some part of the command. It will look up into the history and will show you the commands that matches the search term.
Code:
ctrl+r search_term
By default, it will show just one result. To see more results matching your search term, you will have to use ctrl+r again and again. To quit reverse search, just use Ctrl+C.
Note that in some Bash shells, you can also use Page Up and Page Down key with your search term and it will autocomplete the command.
- Unfreeze your Linux terminal from accidental Ctrl+S
Just use Ctrl+Q and you can use the terminal again.
Code:
ctrl+Q
- Move to beginning or end of line
You can use Home and End keys here of course but alternatively, you can use Ctrl+A to go to the beginning of the line and Ctrl+E to go to the end.
- Delete entire line from cursor position
In the Linux terminal, if you press Ctrl+U, it deletes everything from your current cursor position to the beginning of the line.
Similarly, if you press Ctrl+K, it deletes everything from your cursor position to the end of the line.
Possible made a mistake in typing the password? Instead of using backspace key all the way, simply use Ctrl+U and retype the password.
- Reading a log file in real time
Code:
tail -f path_to_Log
You can also use the regular grep options to display only those lines that are meaningful to you:
Code:
tail -f path_to_log | grep search_term
You can also use the option F here. This will keep the tail running even if the log file is deleted. So if the log file is created again, tail will continue logging.
- Reading compressed logs without extracting
Thankfully, z commands save you in such situations. z commands provide alternatives of the regular commands that you use to deal with log files such as less, cat, grep etc.
So you get zless, zcat, zgrep etc and you don’t even have to explicitly extract the compressed files.
- Use less to read files
You can use Vi, Vim or other terminal based text editors but if you just want to read a file, less command is a far better choice.
Code:
less path_to_file
You can search for terms inside less, move by page, display with line numbers etc.
- Reuse the last item from the previous command with !$
Say you have to create a directory and then go into the newly created directory. There you can use the !$ options.
A better way to do the same is to use alt+. . You can use . a number times to shuffle between the options of the last commands.
- Reuse the previous command in present command with !!
A quick sudo !! saves plenty of keystrokes here.
Another way if your shell configuaration provides is by pressing the up arrow key, then home key and typing sudo. Though this is a longer method, it works if one cannot recall !! command.
- Using alias to fix typos
Code:
alias gerp=grep
This way you won’t have to retype the command again.
- Copy Paste in Linux terminal
Select the text for copying and right click for paste (works in Putty and other Windows SSH clients)
Select the text for copying and middle click (scroll button on the mouse) for paste
Ctrl+Shift+C for copy and Ctrl+Shift+V for paste
- Kill a running command/process
- Using yes command for commands or scripts that need interactive response
Just use it in the below fashion:
Code:
yes | command_or_script
Another example:
Code:
sudo apt-get install -y php7.4*
- Empty a file without deleting it
Code:
> filename
Find if there are files containing a particular text
There are multiple ways to search and find in Linux command line. But in the case when you just want to see if there are files that contain a particular text, you can use this command:
Code:
grep -Pri Search_Term path_to_directory
- Using help with any command
Just use it in this fashion:
Code:
command_tool --help