Seven useful Linux tips and tricks everyone should know 2018
One of the coolest things about using Linux is that you can keep getting new knowledge over time. Every day, you may come across a new utility, or just a less familiar geek, but it is very useful. These piecemeal things don’t always change life, but they are the foundation of professional knowledge.
Even an expert can’t know everything. No matter how much experience you have, there may always be more things that you need to learn. So, here I have listed seven things you might not know about Linux.
1. An interactive mode for finding command history
You may be familiar with the history command, which reads the bash history and outputs it to the standard output (stdout) as a numbered list. However, if you look for a specific link (URL) in the ocean of the curl command, then this list is not always easy to read.
You can also have another option, Linux has an interactive reverse search to help you solve this problem. You can start the interactive mode with the shortcut key ctrl+r and then enter an interactive prompt.
It will search backward bash history according to the string you provided. You can search backwards by pressing ctrl+r again. Command, or press ctrl+s to search forward.
Note that ctrl+s sometimes conflicts with XON/XOFF flow control, which is also used by XON/XOFF flow control. You can disable this shortcut by running the stty -ixoncommand.
This is usually useful on your PC, but make sure you don’t need XON/XOFF before disabling it.
2. Cron is not the only way to schedule a task
The Cron task is very useful for any level of system administrator, whether it is an inexperienced beginner or an experienced expert. However, if you need to schedule a one-time task, the at command gives you a quick way to create tasks so you don’t need to touch crontab.
The at command works by following the run time of the task you want to run. Time is flexible because it supports many time formats. Including the following examples:
At 12:00 PM September 30 2017at now + 1 hourat 9:00 AM tomorrow
When you enter the at command with parameters, you will be prompted to run the command on your Linux system. This could be a backup script, a set of maintenance tasks, or even a normal bash command. If you want to end the task, you can press ctrl+d.
Alternatively, you can use the atq command to view all tasks for the current user, or use sudo atq to view all user tasks. It will show all scheduled tasks, and each task is accompanied by an ID. If you want to cancel a scheduled task, you can use the atrm command with the task ID as a parameter.
3. You can search for commands by function, not just by name
It is very difficult to remember the name of the command, especially for beginners. Fortunately, Linux comes with a tool for searching man pages by name and description.
Next time, if you don’t remember the name of the tool you want to use, you can try using the apropos command plus a description of what you want to do. For example, apropos build filesystem will return a list of names and descriptions of tools that include the words “build” and “filesystem”.
The apropos command accepts one or more strings as arguments, but it also has other parameters. For example, you can use the -r argument to search through regular expressions.
4. An alternative system that allows you to manage the system version
If you have worked on software development, you will understand the importance of supporting the support of different versions of the language across projects. Many Linux distributions have tools to handle different built-in versions.
Executable files such as java are often symbolically linked to the directory /etc/alternatives. In turn, the directory stores symbolic links as binary files and provides an interface to manage these links.
Java may be the most commonly managed language for alternative systems, but with some configuration it can also be used as a replacement for other applications, such as NVM and RVM (NVM and RVM are version managers for NodeJS and Ruby, respectively).
On Debian-based systems, you can create and manage these links using the update-alternatives command. In CentOS, this tool is called alternatives. By changing the links in your alternatives file, you can install multiple versions of a language and use different binaries in different situations.
This alternative system also provides support for any program you might run on the command line.
5. The shred command is a safer way to delete files.
Most of the time we always use the rm command to delete files. But where did the file go? The truth is that the rm command does not do what you think, it just removes hard links to the file system and the data on the hard disk.
The data on the hard disk still exists until it is overwritten by another application. For very sensitive data, this poses a big security risk.
The shred command is an upgraded version of the rm command. When you delete a file using the shred command, the data in the file is overwritten multiple times. There is even an option to clear all data after random overwriting.
If you want to safely delete a file and overwrite it with zero, you can use the following command:
Shred -u -z [file name]
At the same time, you can also use the -n option and a number as arguments to specify how many iterations to randomly overwrite the data.
6. Avoid automatic typing of long invalid file paths with AutoCorrect
How many times have you entered the absolute path of a file, but you see the message “There is no such file or directory”. Anyone will understand the pain of typing a very long string. Fortunately, there is a very simple solution.
The built-in shopt command allows you to set different options to change the behavior of the shell. Setting the cdspell option is a simple way to avoid the headache of typing a letter when typing a file path.
You can enable this option by running the shopt -s cdspell command. When this option is enabled, when you want to switch directories, it will automatically correct to the most matching directory.
The Shell option is a great way to save time (not to mention less trouble), and there are many other options. If you want to see a complete list of all the options in your system, you can run the shopt command with no parameters.
It’s important to note that this is a bash feature that may not work if you run zsh or another shell of choice.
7. Return to the current directory via the sub-shell
If you have ever configured a more complex system, you may find that you need to change directories frequently, making it difficult to track where you are. Wouldn’t it be nice if I automatically returned to the current position after running a command?
The Linux system actually provides a way to solve this problem and it’s very simple. If you want to go through another directory with the cd command to complete some tasks and then return to the current working directory, you can put the commands in parentheses.
You can try the following command on your Linux system. Remember your current working directory and run:
(cd /etc && ls -a)
This command will output the contents of the /etc directory. Now check your current working directory. It is the same as the directory before the command was executed, not the /etc directory.
How does it work? Running a command in parentheses creates a subshell or a replica of the current shell process.
This subshell can access all of the parent variables, and vice versa. So remember that you are running a very complicated one-line command.
Subshells are often used in parallel processing, but on the command line, it gives you the same power, making it easier to browse the file system.