10 Interesting Linux Command Line Tricks You Worth Knowing

Summary: I really like using commands because they provide more control over a Linux system than GUI (Graphical User Interface) applications, so I'm always looking for some interesting ways to make Linux operations easy and fun, mainly based on Terminal operation. When we discover new tricks for using Linux, especially a command-line geek like me, we always get a kick out of it.

I really like using commands because they provide more control over a Linux system than GUI (Graphical User Interface) applications, so I'm always looking for some interesting ways to make Linux operations easy and fun, mostly terminal based operations .

When we discover new tricks for using Linux, especially a command-line geek like me, we always get a kick out of it.

Suggested Read: 5 Interesting Linux Command Line Tricks - Part 1

And we'd love to share newly learned practices or commands with millions of Linux users, especially those who are still doing this exciting thing their way Newbies to the system.

Suggested Read: 10 Useful Linux Command Line Tips for Newbies - Part II

In this article, we'll review a series of useful command line tips that can dramatically improve your Linux skills.

1. Lock or hide a file or directory in Linux The easiest way to
lock file or directory is to use Linux file permissions. If you are the owner of a file or directory, you can prevent other users and groups from accessing (deleting, reading, writing, executing) it as follows:

$ chmod 700 tecmint.info
or
$ chmod go-rwx tecmint.info
To learn more about file permissions in Linux, read this article Managing Users and Groups, File Permissions and Attributes in Linux.

In order to hide files or directories from other users in the system, you can rename them by adding . at the beginning of the file or directory:

$ mv filename .tecmint.info
2. Convert rwx permissions to octal format in Linux
By default , After you run the ls command, it will display file permissions in rwx format. To understand the equivalence of rwx format and octal format, you can learn how to convert rwx permissions to octal format in Linux.

3. How to use the su command when the sudo command fails
Although the sudo command is used to execute commands with superuser privileges, it also fails to execute in some cases, as shown below.

Here, I want to empty the contents of a large file named uptime.log, but it fails even if I use sudo command.

$ cat /dev/null >/var/log/uptime.log
$ sudo cat /dev/null >/var/log/uptime.log
Empty the contents of a large file
in Linux

Encountered this In this case, you need to use the su command to switch to the root user, and then perform the empty operation as follows:

$ su
$ sudo cat /dev/null >/var/log/uptime.log
$ cat /var/log/uptime.log
switch to superuser
switch to superuser

Try to understand the difference between su and sudo, also, read their man pages for more usage guidelines:

$ man sudo
$ man su
4. Ending a Process in Linux
There are times when you want to use kill, killall , pkill command to kill a process, they may not take effect, you may see the process is still running on the system.

To forcibly kill a process, send the -KILL signal to the process.

First get the specified process ID, then kill the process like this:

$ pidof vlc
$ sudo kill -KILL 10279 Find and kill
processes
in Linux Find and kill processes in Linux

See the kill command for more usage options and information.

5. Permanently delete files in Linux
Normally , we delete files from the Linux system by using the rm command. However, these files are not actually deleted, they are still stored there and hidden on your hard drive, and other users can still recover deleted files and view them in Linux.

To prevent this from happening, we can use the shred command to overwrite the contents of the file and choose to delete the file when the overwrite is complete.

$ shred -zvu tecmint.pdf
Description of the options used in the above command:

-z – Last overwrite with 0 to hide overwrite action.
-u – Truncate and remove files after overwriting.
-v – Display verbose procedure.
Deleting files permanently
in Linux Deleting files permanently in Linux

Read the shred manual for more usage information.

$ man shred
6. Renaming multiple files in Linux
You in Linux at any time by using the rename command.

The rename command renames the specified file according to the rules in the first argument.

The following command will rename all .pdf files to .doc files using the rule 's/\.pdf$/\.doc/':

$ rename -v 's/\.pdf$/\.doc/' * .pdf
Renaming Multiple Files
in Linux Renaming Multiple Files in Linux

In the following example, we will remove the extension by renaming all files matching "*.bak", using the rule 's/\e.bak$//':

$ rename -v 's/\e.bak$//' *.bak
7. Check spelling of words in Linux The
look command is used to display files prefixed with a specified string , and it can also help you check the spelling of a given word on the command line. Even though it's not as effective and reliable, it still counts as a useful alternative to other powerful spell checking tools.

$ look linu
$ look docum
Check spelling of words
in Linux

8. Search man pages by keyword
The man command is used to display the man page of the command. When the -k option is used, it will use the keyword printf (or the keywords adjust, apache, php in the following commands) as a regular expression to search for all man pages matching the name , and displays its profile.

$ man -k adjust $ man -k
apache $ man -k
php
Search man pages
by keyword Search man pages by keyword Results of the. When the watch command is used in conjunction with the tail command (a Linux command used to view the end of files), the logging of log files can be monitored. In the following example, you will monitor the system authentication log file in real time. Open two terminal windows and monitor the log file in real time in the first window, as follows: $ sudo watch tail /var/log/auth.log You can also use the -f option of the tail command (Linux command to show end of file) Monitor file changes in real time. In this way, we can see the log generation in the log file. $ sudo tail -f /var/log/auth.log Next, run the following command in the second terminal window, after which you can observe the log file contents in the first terminal window: $ sudo mkdir -p /etc /test $ sudo rm -rf /etc/test 10. List all built-in shell commands















A shell built-in command is a command or function that is called from within and executed directly in the shell, rather than loading an external executable program from the hard disk for execution.

To list all shell built-in commands and their syntax, execute the following command:

$ help
As a concluding remark, command-line tricks not only come in handy, but also make learning and using Linux easier and more fun, especially for newbies.

Original published on: 2017-01-15

This article is from Yunqi Community Partner "Linux China" Abstract: I really like using commands because they provide more control over Linux systems than GUI (Graphical User Interface) applications, so , I've been looking for some interesting ways to make Linux operations easy and fun, mainly terminal-based operations. When we discover new tricks for using Linux, especially a command-line geek like me, we always get a kick out of it.

I really like using commands because they provide more control over a Linux system than GUI (Graphical User Interface) applications, so I'm always looking for some interesting ways to make Linux operations easy and fun, mostly terminal based operations .

When we discover new tricks for using Linux, especially a command-line geek like me, we always get a kick out of it.

Suggested Read: 5 Interesting Linux Command Line Tricks - Part 1

And we'd love to share newly learned practices or commands with millions of Linux users, especially those who are still doing this exciting thing their way Newbies to the system.

Suggested Read: 10 Useful Linux Command Line Tips for Newbies - Part II

In this article, we'll review a series of useful command line tips that can dramatically improve your Linux skills.

1. Lock or hide a file or directory in Linux The easiest way to
lock file or directory is to use Linux file permissions. If you are the owner of a file or directory, you can prevent other users and groups from accessing (deleting, reading, writing, executing) it as follows:

$ chmod 700 tecmint.info
or
$ chmod go-rwx tecmint.info
To learn more about file permissions in Linux, read this article Managing Users and Groups, File Permissions and Attributes in Linux.

In order to hide files or directories from other users in the system, you can rename them by adding . at the beginning of the file or directory:

$ mv filename .tecmint.info
2. Convert rwx permissions to octal format in Linux
By default , After you run the ls command, it will display file permissions in rwx format. To understand the equivalence of rwx format and octal format, you can learn how to convert rwx permissions to octal format in Linux.

3. How to use the su command when the sudo command fails
Although the sudo command is used to execute commands with superuser privileges, it also fails to execute in some cases, as shown below.

Here, I want to empty the contents of a large file named uptime.log, but it fails even if I use sudo command.

$ cat /dev/null >/var/log/uptime.log
$ sudo cat /dev/null >/var/log/uptime.log
Empty Large File Contents in Linux
Empty the contents of

a this case, you need to switch to root user using the su command, and then perform the empty operation like this:

$ su
$ sudo cat /dev/null >/var/log/uptime. log
$ cat /var/log/uptime.log
Switch to superuser
Switch to superuser

Try to understand the difference between su and sudo, also read their man pages for more usage guidelines:

$ man sudo
$ man su
4. End a process in
Linux Sometimes, when you want to use kill, killall, pkill commands to end a process, they may not take effect, and you may see that the process is still running on the system.

To forcibly kill a process, send the -KILL signal to the process.

First get the specified process ID, then kill the process like this:

$ pidof vlc
$ sudo kill -KILL 10279 Find and kill
processes
in Linux Find and kill processes in Linux

See the kill command for more usage options and information.

5. Permanently delete files in Linux
Typically, we remove files from a Linux system by using the rm command. However, these files are not actually deleted, they are still stored there and hidden on your hard drive, and other users can still recover deleted files and view them in Linux.

To prevent this from happening, we can use the shred command to overwrite the contents of the file and choose to delete the file when the overwrite is complete.

$ shred -zvu tecmint.pdf
Description of the options used in the above command:

-z – Last overwrite with 0 to hide overwrite action.
-u – Truncate and remove files after overwriting.
-v – Display verbose procedure.
Deleting files permanently
in Linux Deleting files permanently in Linux

Read the shred manual for more usage information.

$ man shred
6. Renaming multiple files in Linux
You in Linux at any time by using the rename command.

The rename command renames the specified file according to the rules in the first argument.

The following command will rename all .pdf files to .doc files using the rule 's/\.pdf$/\.doc/':

$ rename -v 's/\.pdf$/\.doc/' * .pdf
Renaming Multiple Files
in Linux Renaming Multiple Files in Linux

In the following example, we will remove the extension by renaming all files matching "*.bak", using the rule 's/\e.bak$//':

$ rename -v 's/\e.bak$//' *.bak
7. Check word spelling in Linux The
look command is used to display any line in the file prefixed with a specified string, and it can also help you check The spelling of a given word on the command line. Even though it's not as effective and reliable, it still counts as a useful alternative to other powerful spell checking tools.

$ look linu
$ look docum
Check word spelling
in Linux

8. Search man pages by keyword
The man command is used to display the man page for a command, when the -k option is used, it prints the keyword printf ( Or use the keywords adjust, apache, php in the following command as a regular expression to search for all manual pages matching the name and display their introduction.

$ man -k adjust $ man -k
apache $ man -k
php
Search man pages
by keyword Search man pages by keyword Results of the. When the watch command is used in conjunction with the tail command (a Linux command used to view the end of files), the logging of log files can be monitored. In the following example, you will monitor the system authentication log file in real time. Open two terminal windows and monitor the log file in real time in the first window, as follows: $ sudo watch tail /var/log/auth.log







You can also monitor file changes in real time using the -f option of the tail command (the Linux command that displays the end of files). In this way, we can see the log generation in the log file.

$ sudo tail -f /var/log/auth.log
Next, run the following command in the second terminal window, after which you can observe the log file contents in the first terminal window:

$ sudo mkdir -p /etc /test
$ sudo rm -rf /etc/test
10. List all shell built-in commands.
A shell built-in command is a command or function that is called from within and executed directly in the shell, instead of loading an external executable program from the hard disk to execute. .

To list all shell built-in commands and their syntax, execute the following command:

$ help
As a concluding remark, command-line tricks not only come in handy, but also make learning and using Linux easier and more fun, especially for newbies.

The original release time is: 2017-01-15

This article is from Yunqi Community Partner "Linux China"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326197299&siteId=291194637