Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

In-depth understanding of Unix / Linux commands

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Analysis of commands

The command of Unix consists of two parts, the command itself and additional parameters. For example, the ls command, if you execute the ls command directly without additional parameters, the default execution target is the current directory, as follows

[root@localhost /] $ls

Bin dev etc lib media opt root sbin sys usr

Boot docs home lib64 mnt proc run srv tmp var

We can add command parameters to make it more flexible, and the-l parameter can make the result set display more information in long results, and the subsequent path can specify the target path for the command execution.

[root@localhost /] $ls-l / root

Total 181328

-rw-. 1 root root 1264 Aug 15 19:36 anaconda-ks.cfg

Drwxr-xr-x. 3 root root 18 Sep 1 22:12 backups

-rw-r--r--. 1 root root 2381 Sep 1 21:35 baidu.html

-rwxr-xr-x. 1 root root 10 Sep 1 05:57 cat.txt

Drwxr-xr-x. 2 root root 25 Sep 1 21:43 docs

-Rafael, Rafe, Rafael. 2 root root 17 Aug 18 12:18 file

-Rafael, Rafe, Rafael. 2 root root 17 Aug 18 12:18 ha_link

-rw-r--r--. 1 root root 49 Aug 18 12:14 hard_link

Drwxr-xr-x. 7 10 143 245 Aug 26 11:44 jdk1.8

-rw-r--r--. 1 root root 185646832 Aug 26 11:30 jdk-8u181-linux-x64.tar.gz

-rw-r--r--. 1 root root 0 Sep 1 22:18 log.file

-rw-r--r--. 1 root root 0 Sep 1 23:36 log.fileat

-rwxr--r--. 1 root root 114 Sep 2 06:53 purple.sh

Lrwxrwxrwx. 1 root root 4 Aug 18 12:08 soft_link-> file

-rw-r--r--. 1 root root 1326 Aug 22 12:01 test.txt

two。 Find information related to the command

If you have never used the ls command, how do you learn how to use it? In addition to learning through search engines, you can also use man to learn all the instructions you want to learn. The following shows the output after the execution of the man ls command. Because the length is too long, the latter part is omitted. Interested friends will try it on their own.

[root@localhost /] $man ls

LS (1) User Commands LS (1)

NAME

Ls-list directory contents

SYNOPSIS

Ls [OPTION]... [FILE]...

DESCRIPTION

List information about the FILEs (the current directory by default).

Sort entries alphabetically if none of-cftuvSUX nor-- sort is speci-

Fied.

Mandatory arguments to long options are mandatory for short options

Too.

-a,-- all

GNU coreutils 8.22 November 2016 LS (1)

Manual page ls (1) line 219 Universe 251 (END) (press h for help or q to quit)

From the output, we can know the main functions of the command and all the parameters supported. Instead of using man to learn commands, we can also use info to learn. For example, enter info ls on the command line:

[root@localhost /] $info ls

File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directory listin\

G

10.1 'ls': List directory contents

= =

The 'ls' program lists information about files (of any type, including

Directories). Options and file arguments can be intermixed arbitrarily

As usual.

For non-option command-line arguments that are directories, by

Default 'ls' lists the contents of directories, not recursively, and

Omitting files with names beginning with'. For other non-option

Arguments, by default 'ls' lists just the file name. If no non-option

Argument is specified, 'ls' operates on the current directory, acting as

If it had been invoked with a single argument of'.

...

This will also tell you how to use ls to make your work more efficient.

3. Modify command

We can use some tools to enhance the functions of commands, such as metacharacters, input and output redirection, piping, command replacement.

Metacharacter

The deferred ls command above outputs a lot of files, but what if we just need to list the files whose names end with link? We can use parameters + wildcards to do this.

[root@localhost ~] $ls * link

Ha_link hard_link soft_link

* stands for matching one or more characters in the file name.

? Matches any character in the file name.

[] matches a character contained in the [] symbol.

[root@localhost ~] $ls * [link]

Baidu.html ha_link hard_link soft_link

As above, it matches all files except that the last character at the end is any one of the characters.

Output redirection

The command execution results of the above operations are all output directly to the console, but what if our output is very long, or if we have something else to do temporarily, and the results need to be analyzed later? At this point, we can redirect the result output to a file, save it, and check it later.

[root@localhost ~] $ls * [link] > links.txt

[root@localhost ~] $cat links.txt

Baidu.html

Ha_link

Hard_link

Soft_link

As we did above, we output the results of the file list to the links.txt file so that we can investigate the problem later.

Pipeline

What we just looked at is the file under the user's working directory, the output is very little, you can view it directly. However, when we query that there are many files under a folder, the output is very long, so it is not easy to view the results on a single screen. Is there a way for us to look at some of the results first, and then press a button to move on to the next page? Maybe if you're smart, you'll think of more or less commands. But these two commands are to view the file, and the pipe can help you at this time. Pipes can redirect input and output, using the output of one command as the input of another.

[root@localhost ~] $ls / etc | more

Adjtime

Aliases

Aliases.db

Alternatives

Anacrontab

Asound.conf

At.deny

Audisp

Audit

Bash_completion.d

Bashrc

Binfmt.d

Centos-release

Centos-release-upstream

-- more--

As above, the output of ls is used as the input of more, so we can take a leisurely look at the results of ls. Of course, this is just an example of ls and more. Friends can explore other commands to use in conjunction with the pipeline, and you will love it.

Command replacement

Similarly, command replacement can operate like a pipe. It also inputs the output of the command as another command.

[root@localhost ~] $ls ${pwd}

Anaconda-ks.cfg cat.txt ha_link jdk-8u181-linux-x64.tar.gz log.fileat test.txt

Backups docs hard_link links.txt purple.sh

Baidu.html file jdk1.8 log.file soft_link

As above, we use the output of the pwd command (the current working directory) as the input to the ls command. Of course, this command can also be implemented in a pipeline: pwd | ls to achieve the same effect:

[root@localhost ~] $pwd | ls

Anaconda-ks.cfg cat.txt ha_link jdk-8u181-linux-x64.tar.gz log.fileat test.txt

Backups docs hard_link links.txt purple.sh

Baidu.html file jdk1.8 log.file soft_link

Achieve the same effect, but the implementation principle is different. Command substitution is done by executing the result in the child shell and then returning the result to the main shell. Pipes, on the other hand, are always executed in the main shell.

-END-

For those of you who like this article, please click on the picture below to subscribe and watch more wonderful content.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report