10 Interesting Linux command-line tips you should know

Source: Internet
Author: User

Summary: I like to use commands very much, because they provide more control over the Linux system than GUI (graphical user interface) applications, so I've been looking for some interesting ways to make Linux easier and more fun, mostly based on terminal operations. When we find new techniques for using Linux, especially command-line geeks like me, we always feel very excited.

I like to use commands very much, because they provide more control over the Linux system than GUI (graphical user interface) applications, so I've been looking for some interesting ways to make Linux easier and more fun, mostly based on terminal operations.

When we find new techniques for using Linux, especially command-line geeks like me, we always feel very excited.

Suggested reading: 5 interesting Linux command-line tricks-Part I

And we would love to share new practices or commands with millions Linux users, especially those who are still working on this exciting operating system in their own way.

Suggested reading: 10 useful Linux command-line tricks for beginners-Part Two

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

1. Lock or hide files or directories in Linux

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

    1. $ chmod 700 tecmint.info
    2. $ chmod go-rwx tecmint.info

To learn more about Linux file permissions, read this article on managing users and Groups, file permissions, and properties in Linux.

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

    1. $ mv filename .tecmint.info
2. Convert rwx permissions to octal format in Linux

By default, when you run the LS command, it uses the rwx format to display file permissions, and in order to understand the RWX format and octal format equivalence, you can learn how to convert rwx permissions to octal format in Linux.

3. When sudoHow to use when command execution fails suCommand

Although the sudo command is used to execute commands with superuser privileges, in some cases it will also fail, as shown below.

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

    1. $ cat  /dev/null >/var/log/uptime log
    2. $ sudocat /dev/ null >/var/ Log/uptime.

Empty the contents of large files in Linux

In this case, you need to su switch to the user with a command root and then perform the purge operation like this:

    1. $ su
    2. $ sudo cat /dev/null >/var/log/uptime.log
    3. $ cat /var/log/uptime.log

Switch to Super User

Try to understand the difference between Su and sudo, in addition, by reading their hand albums to learn more about the usage guide:

    1. $ man sudo
    2. $ man su
4. End a process in Linux

Sometimes, when you want to end a process with the kill, Killall, pkill commands, they might not work, and you might see that the process is still running on the system.

If you want to force the end of a process, you can send a -KILL signal to the process.

Get the specified process ID first, and then end the process as follows:

    1. $ pidof vlc
    2. $ sudo kill -KILL 10279

Find and end processes in Linux

View the KILL command for more usage options and information.

5. Permanently delete files in Linux

In general, we rm remove files from the Linux system by using commands. However, these files are not really 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 shred commands to overwrite the contents of the file and choose to delete the file after the overwrite is complete.

    1. $ shred -zvu tecmint.pdf

Description of the options used in the above command:

    1. -z– Last use 0 to overwrite to hide overwrite action.
    2. -u– Truncate and remove files after overwrite.
    3. -v– Show detailed procedures.

Permanently delete files in Linux

Read shred the manual for more information about usage.

    1. $ man shred
6. Renaming multiple files in Linux

You can rename rename multiple files in Linux at any time by using commands.

renameThe command renames the specified file according to the rules in the first parameter.

The following command renames all .pdf files to a .doc file, using a rule that ‘s/\.pdf$/\.doc/‘ :

    1. $ rename -v ‘s/\.pdf$/\.doc/‘ *.pdf

Renaming multiple files in Linux

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

    1. $ rename -v ‘s/\e.bak$//‘ *.bak
7. Check the spelling of the word in Linux

lookThe command is used to display any row in the file prefixed with the specified string, and it can also help you check the spelling of a given word in the command line. Although it is not so effective and reliable, it is still a useful alternative to other powerful spell checker tools.

    1. $ look linu
    2. $ look docum

Check the spelling of a word in Linux

8. Search the manual page by keyword

manThe command is used to display the man page of the command, and when the option is used, it uses the keyword -k printf (or keywords in the following command, adjust apache ,, php ) as a regular expression to search all matching pages of that name and to display its introduction.

    1. $ man -k adjust
    2. $ man -k apache
    3. $ man -k php

Search by Keyword page

9. Real-time monitoring of logs in Linux

watchcommand to execute another Linux command on a regular basis and display the result of the command in full screen. When the watch command is used in conjunction with the Tail command (the Linux command used to view the end of the file), log file logging 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:

    1. $ sudo watch tail /var/log/auth.log

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

    1. $ sudo tail -f /var/log/auth.log

Next, run the following command in the Second terminal window, after which you can observe the contents of the log file in the first terminal window:

    1. $ sudo mkdir -p /etc/test
    2. $ sudo rm -rf /etc/test
10. List all shell built-in commands

The shell built-in command is a command or function that is invoked from within and executed directly in the shell, rather than from an external executable program loaded from the hard disk.

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

    1. $ help

As a closing point, command-line tricks can be useful and make learning and using Linux easier and more fun, especially for beginners.

Original Date: 2017-01-15

This article comes from the cloud community partner "Linux China"

Original link

10 Interesting Linux command-line tips you should know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.