10 Useful Linux command-line tricks for beginners

Source: Internet
Author: User

I remember the first time I used Linux, I was also accustomed to the Windows graphical interface, I really hate the Linux terminal. At that time I felt the commands were hard to remember and could not be used correctly. Over time, I realized the beauty, flexibility and usability of the Linux terminal, and honestly, I didn't use it for a day. Today, I'm happy to share some useful tips and hints with people who are just beginning to get in touch with Linux, hoping to help them better transition to Linux and help them learn something new (hopefully).

    • 5 Interesting Linux command-line tricks

This article wants to show you some useful tips for using Linux endpoints like a master without the need for high technology. You only need a Linux terminal and some free time to experience these commands.

1. Find the right command

It is important for your system to execute the correct command. However, there are so many different command lines in Linux that are hard to remember. So how do you find the right command you need? The answer is apropos. You only need to run:

# Apropos <description>

Where you want to replace "description" with a statement that really describes the command you're looking for. Here's an example:

# apropos "List Directory" dir (1)-List directory Contentsls (1)-List directory Contentsntfsls (8)-List directory con Tents on an NTFS filesystemvdir (1)-List Directory contents

What you see on the left is the command, and the right is their description.

2. Execution of PREVIOUS commands

There are times when you need to perform the same command over and over again. Although you can repeatedly press the UP cursor key on your keyboard, you can also replace it with the history command. This command lists all commands that have been entered since the last time you started the terminal:

# History    1  fdisk-l    2  apt-get install gnome-paint    3  hostname tecmint.com    4  Hostnamectl tecmint.com    5 man  hostnamectl     6  hostnamectl--set-hostname tecmint.com    7  Hostnamectl-set-hostname tecmint.com    8  hostnamectl set-hostname tecmint.com    9  mount-t "NTFS"-O   Ten  fdisk-l  mount-t ntfs-3g/dev/sda5/mnt  mount-t rw ntfs-3g/dev/sda5/mnt  mount-t-rw ntfs-3g/dev/sda5/mnt  mount-t ntfs-3g/dev/sda5/mnt  Mount man< C30/>16 man  Mount  mount-t-o ntfs-3g/dev/sda5/mnt  mount-o ntfs-3g/dev/sda5/mnt  mount-ro ntfs-3g/dev/sda5/mnt  cd/mnt   ...

As you can see above, you'll get a list of commands you've run. A number in each row indicates that you entered the command in the first few lines. You can call the command again in the following ways:


The actual number of the command is used instead of #. For a better understanding, take a look at the following example:


Equivalent to:

# History
3. Using the Midnight Command

If you're not accustomed to using commands like CD, CP, MV, RM, etc., you can use the Midnight command. It is a simple visual shell that you can use with the mouse:

Midnight Command

With F1 to F12 keys, you can easily perform different tasks. You only need to select the corresponding command at the bottom. To select a file or directory, press the "Insert" key.

In short, Midnight is called "MC". To install the MC, you only need to run:

$ sudo apt-get install MC        [on Debian based Systems]
# yum Install MC                 [on Fedora based Systems]

Here is a simple example of using the Midnight command. Open the MC by entering the following command:

# MC

Now use the TAB key to select a different window – left and right. I have a LibreOffice file that I want to move to the "Software" directory:

Midnight command to move files

Press the F6 button to move the file to the new directory. MC will ask you to confirm:

Move files to a new directory

Once confirmed, the file will be moved to the new destination directory.

4. Turn off the computer at a specified time

Sometimes you need to shut down your computer a few hours after work. You can turn off your computer at a specified time by using the following command:

$ sudo shutdown 21:00

This will tell you to turn off the computer at the time you specify. You can also tell the system to close after a specified minute:

$ sudo shutdown +15

This means that the computer will shut down after 15 minutes.

5. Display information about a known user

You can use a simple command to list the users of your Linux system and some basic information about them.

# Lslogins

This will output the following result:

UID USER pwd-lock pwd-deny last-login GECOS0 root 0 0 apr29/11:35 root1 bin 0 1 bin2 daemon 0 1 daemon3 ADM 0 1 adm4 LP 0 1 lp5 Sync 0 1 sync6 shutdown 0 1 jul19/10:04 shutdown7 halt 0 1 halt8 Mail 0 1 mail10 UUCP 0 1 uucp11 operator 0 1 Operat Or12 Games 0 1 games13 Gopher 0 1 gopher14 FTP 0 1 ftp User23 squid 0 named 0 1 Named27 mysql 0 1 mysql Server47 mailn ull 0 148 Apache 0 1 Apache ...
6. Find Files

Finding files is sometimes not as simple as you might think. A good example of a search file is:

# Find/home/user-type F

This command searches all files in the/home/user directory. The Find command is really powerful and you can pass more options to it to make your search more detailed. If you want to search for files over a specific size, you can use:

# Find. -type f-size 10M

The above command searches all files larger than 10M in the current directory. Make sure that you do not run the command at the root of your Linux system, as this can cause your machine I/O bottlenecks.

One of the options I use most often with the Find command is "exec", which allows you to run some action on the results of the Find command.

For example, suppose we want to find all the files in a directory and change the permissions. This can be done with the following simple commands:

# find/home/user/files/-type f-exec chmod 644 {}/;

The above command recursively searches all files in the specified directory and executes the chmod command on the found file. It is recommended that you read the use of the 35 Linux ' find ' commands, and I am sure you will find more ways to use this command.

7. Create a directory tree with one command

You probably know that you can use the mkdir command to create a new directory. So if you want to create a new directory, you might run:

# mkdir New_folder

But what if you want to create 5 subdirectories under that directory? Running 5 times the mkdir command is not a good choice. Instead, you can use the-p option like this:

# Mkdir-p New_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}

In the end you will have 5 directories in New_folder:

# ls new_folder/folder_1 folder_2 folder_3 folder_4 folder_5
8. Copying files to multiple directories

File copying is usually done using the CP command. Copying files usually looks similar:

# cp/path-to-file/my_file.txt/path-to-new-directory/

Now suppose you need to copy the file to multiple directories:

# cp/home/user/my_file.txt/home/user/1# cp/home/user/my_file.txt/home/user/2# cp/home/user/my_file.txt/home/user/ 3

It's kind of ridiculous. Instead, you can solve the problem with a simple line of commands:

# echo/home/user/1//home/user/2//home/user/3/| Xargs-n 1  cp/home/user/my_file.txt
9. Delete large files

Sometimes files can become very large. I have seen an example of a log file exceeding 250G due to lack of managerial skills. The RM command may not be sufficient to delete the file because there is a large amount of data that needs to be removed. This "cumbersome" operation should be avoided. Instead, you can use an easy way to solve this problem:

# >/path-to-file/huge_file.log

Of course you need to replace the path and file name according to your actual situation. The above command writes a null output to the file. In simpler terms it will empty the file without causing your system to incur large I/O consumption.

10. Run the same command on multiple Linux servers

One of our readers recently asked in the Linuxsay forum how to execute a command on multiple Linux servers via SSH. The IP address of his machine is:

Here's a simple workaround. Write the IP address of the server to the file List.txt, one line above the other. Then run:

# for in $i (cat list.txt); do ssh [email protected] $i ' Bash command '; Done

In the above command you need to replace "user" with the actual logged-in user, replacing the "Bash command" with the actual command you wish to perform. This method is ideal for password-free authentication by using SSH keys, because you do not need to enter a password for the user every time.

Note Depending on the setup of your Linux system, you may also need to pass some extra parameters to the SSH command.


The above examples are simple, and I hope they will help you discover the beauty of Linux and how you can easily implement different operations that require more time on other operating systems.

10 Useful Linux command-line tricks for beginners

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.