Home > Article > Computer Tutorials > 20 Linux commands and tips to double your efficiency
In this article, I will share some efficient Linux command tips that can help you save time and avoid unnecessary trouble. Additionally, these tips can help increase your productivity.
Even if you are already an experienced Linux user, it is still possible that you have not mastered these skills, even though you have been using Linux for many years.
Cool Linux Terminal Tricks to Help You Save Time and Increase Productivity
Perhaps you are already familiar with some or all of these Linux commands. Whether you know it or not, feel free to share your tips and insights in the comments.
Some of these tips also depend on how the shell is configured. Let's get started now!
I'll start with something visible but very important: tab completion.
When typing in the Linux terminal, pressing the Tab key displays all possible options that match the beginning of the characters you have entered.
For example, if you want to copy a file named linuxidc.txt, just type "cp l" and press the tab key to see the possible options.
Use the Tab key for autocomplete
When you find that you need to go back to a previous directory, you can do so by entering the following command: This is the case if your current directory path is very long and you need to go to another directory in a completely different path Next, just type the command:
cd -
This will take you back to the previous working directory. You don't need to enter long directory paths or copy and paste.
Easily switch between directories as follows:
$ cd -bash: cd: OLDPWD 未设定
It's because the cd command sets the OLDPWD environment variable value. The OLDPWD environment variable will not contain any value unless you execute the cd command at least once
The results of the cd – and cd $OLDWPD commands are not the same in all environments.
This is too obvious. You can move to your home directory from anywhere on the Linux command line using the following command:
cd ~
However, you can also just use CD to return to the home directory:
cd
Most modern Linux distributions have a preconfigured shell for this command. Save at least two keystrokes here.
Quickly return to the home directory
You must be wondering what other tricks there are in the command to list directory contents. Everyone knows to use ls -l in this situation.
That's it. Most people use ls -l to list the contents of a directory, but the same thing can be done with the following command:
ll
Again, this also depends on the Linux distribution and shell configuration, but you will most likely be able to use it on most Linux distributions.
Use ll instead of ls -l
Suppose you have to run several commands one after another. Are you waiting for the first command to finish running before executing the next command?
Then, you can use ";" delimiter. This way you can run many commands in one line. No need to wait for previous commands to complete before performing other tasks.
command_1; command_2; command_3
In the previous command, you learned how to run multiple commands in one command to save time. But many times you have to ensure that the command will not fail before executing the next command, so why not?
For example, you want to build the code, and then run make only if the build is successful.
In this case, you can use the <br>&&
separator. <br>&&
Ensures that the next command only runs if the previous command executed successfully.
command_1 && command_2
A good example of this command is when you upgrade your system using <br>sudo apt update && sudo apt upgrade
.
Imagine a situation where you used a long command minutes/hours ago and you have to use it again. The problem is that you no longer remember the exact command.
Reverse search is your savior. You can search the history for commands using search terms.
Simply use the <br>ctrl r
keys to initiate a reverse search and type certain parts of the command. It will query the history and show you commands that match your search terms.
ctrl + r 搜索词
By default it will only show one result. To see more results matching your search terms, you will have to use ctrl r repeatedly. To exit reverse search, just use Ctrl C.
Reverse search in command history
Please note that in some
Bash Shell
, you can also use the Page Up and Down keys on the search term and it will automatically complete the command.
On many Unix-like systems, Ctrl-S has a special meaning: it "freezes" the terminal (it was once used to pause fast scrolling). Because "save" generally uses this shortcut key, people often press this shortcut key without thinking. As a result, most people will be confused (I often make this mistake). To unfreeze a terminal, use Ctrl-Q, so if you suddenly notice that your terminal appears to be frozen, try Ctrl-Q to see if that frees it.
Suppose you are typing a long command, and along the way you realize that you have to make some changes at the beginning. You'll use several left clicks to move to the beginning of the line. And proceed similarly to the end of the line.
Of course you can use the Home and
End keys here
, but you can also use Ctrl A to go to the beginning of the line, and Ctrl E to go to the end.
Animation demonstration is as follows
Move to the beginning or end of the line
I find it more convenient than using the Home and End keys, especially on laptops.
In situations where you need to analyze logs while the application is running, you can use the tail command with the -F option.
tail -F linuxidc_log
Equivalent to <br>--follow=name --retry
, track according to the file name and keep retrying, that is, after the file is deleted or renamed, if Create the same file name again and tracking will continue.
Server logs are usually gzip compressed to save disk space. This creates a problem for developers or system administrators analyzing logs. You may have to scp it locally and then extract it to access the file because sometimes you don't have write permissions to extract the logs.
Thankfully, the z command can help you in this case. The z command provides an alternative to regular commands for processing log files such as less, cat, grep, etc.
This way you can use zless, zcat, zgrep and other commands to view the contents of the compressed package without even having to explicitly extract the compressed file.
$zcat linuxidc_log.zip | more
Read compressed files without decompression
To view the contents of a file, cat is not an option, especially if the file is large. The cat command will display the entire file on the screen.
You can use Vi, Vim, or another terminal-based text editor, but if you just want to read the file, the less command is a better choice.
<code style="padding: 0px;max-width: 1000%;text-align: left;position: relative;font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace"> less -N linuxidc.txt</code> <code style="padding: 0px;max-width: 1000%;text-align: left;position: relative;font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace"> <br></code> <code style="padding: 0px;max-width: 1000%;text-align: left;position: relative;font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace"> //按下v键来编辑文件</code> <code style="padding: 0px;max-width: 1000%;text-align: left;position: relative;font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace"> //退出编辑器后,你可以继续用less浏览了</code>
You can search for words in a smaller range, move by page, highlight and line numbers, etc.
<br>
!$
In many cases, it is convenient to use the parameters of the previous command.
Suppose you have to create a directory and then go into the newly created directory. Well, you can use the <br>!$
option.
Use !$
A better way is to use <br>alt .
. The number of times to move back and forth between the options of the last command.
!!
Reuse the previous command in the current command. You can use !! to call the entire previous command. This is especially useful when you have to run a command and realize that it requires root privileges.
A quick sudo!! Saves a lot of keystrokes.
Use <br>!!
to reuse the previous command in the current command.
You probably already know what the alias command is in Linux. What you can do is use them to correct typos.
For example, you may often type grep as gerp. If you put an alias in your bashrc this way:
alias gerp=grep
This way, you don't need to enter the command again.
This one is a bit ambiguous as it depends on the Linux distribution and terminal application. But generally, you should be able to copy-paste commands using the following shortcut keys:
Select the text you want to copy and right-click to paste (works in Putty and other Windows SSH clients)
Select the text you want to copy and click the middle mouse button (scroll button) to paste
Ctrl Shift C means copy, Ctrl Shift V means paste
This may be too obvious. If there is a command running and you want to exit the command, you can stop the running command by pressing Ctrl C.
If you just want to clear the contents of a text file without deleting the file itself, you can use a command similar to the following:
> 文件名
There are many ways to search and find in the Linux command line. However, when you just want to see if there are files containing specific text, you can use the following command:
grep -Pri 要搜索的字符串 路径
Finally I will end this article with a more obvious but very important "trick", which is to use the help command (help) of a command or command line tool.
Almost all commands and command line tools come with a help page that shows how to use the command. Regular use of the help will tell you the basic usage of this tool/command.
For example, help for the bc command:
$bc -help
What is your favorite Linux command line trick?
The techniques discussed in this article should work on almost all Linux distributions and shells without requiring the installation of new tools. I also recommend using the alias command in Linux to replace complex commands with simple ones. Can save you a lot of time.
The above is the detailed content of 20 Linux commands and tips to double your efficiency. For more information, please follow other related articles on the PHP Chinese website!