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

Advanced detailed explanation of Linux/UNIX Shell command

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Splicing with cat

The 1 cat command is a simple command used every day, and cat itself stands for concatenate (splicing).

2 the general format for reading files with cat is

Cat file1 file2 file3.... / / this command splices the file contents of the command line arguments together to output.

The 3 cat command not only reads files and splices data, it can also read from standard input. To read from standard input, use pipes

Stand_ouput | cat

The 4 cat command can be used to compress multiple blank lines so that they are compressed into a single

Cat-s file

The 5 tr command can also compress multiple'\ n' characters into a single'\ n'.

The-n option of the 6 cat command precedes each line of output with a line number, but this does not modify the contents of the file

Recording and playback terminal session

1 script and scriptreplay commands can be found on most GNU/Linux distributions

The 2 scritp command can also be used to establish a broadcast video session between multiple users

Step 1: open two terminals T1 and T2

Step 2: enter mkfifo scriptfifo in T1

Step 3: enter the following command cat scriptfifo in T2

Step 4: enter the following command script-f scriptfifo in T1

Step 5: all commands entered in T1 can be seen in T2.

File lookup and file list

1 find is one of the best tools in the UNIX/Linux command line toolbox

2 the find command works as follows: traverse down the file hierarchy, match the files that meet the criteria, and perform the corresponding operation

3 to list all files and folders in the current directory and subdirectories: find base_bash

4 two important parameters of the find command

-print indicates the file name of the print matching file, and when using-print,'\ n' is used as the delimiter of the split file.

-print0 indicates that'\ 0' is used as the delimiter to print each matching file name

5 search based on the file name or regular expression match, and the parameter of-name specifies the string that the file name matches.

For example, find. -name "* .txt"-print / / command to find all txt files in the current directory and print them out

The 6 find command has an option-iname, which is similar to-name, but ignores case when matching

7 find command can also be used! To negate the meaning of parameters.

Find. ! -name "* .txt"-the function of the print / / command is to find all non-txt files in the current directory

The 8 find command traverses all subdirectories when it is used, and we can use the depth parameter to limit the traversal depth of the find command.

-maxdepth and-mindepth specify the maximum recursion depth and the least recursion depth (from which layer to search down)

9-maxdepth and-mindepth should appear as the third parameter of find, so that the search conditions can be limited at the first time, and the efficiency will be greatly improved.

Type 10 UNIX systems treat everything as files, which have different types, such as normal files, directories, character devices, block devices, symbolic links, hard links, sockets and FIFO, etc.

The-type option of the 11 find command filters the file search

12 type parameters to match the required file type

File type parameter

Ordinary file f

Match link l

Catalog d

Character device c

Block device b

Socket s

Fifo p

13 delete matching files,-delete can be used to delete matching files found by find

Find. The function of the-type f-name "* .txt"-delete / / command is to find to find all the txt files in the face directory and then delete them.

14 search based on file permissions and ownership

Find. The function of the-type f-name "* .txt"-perm 644-print / / command is to find all txt files with permissions of 644 in the current directory.

Use tr for conversion

1 tr can be used to replace, delete and compress standard input characters. It can change one set of characters into another, so it is often called a conversion command.

2 tr can only accept input through stdin, not through command line arguments.

Tr [options] set1 set2 / / maps input characters from stdin from set1 to set2 and inputs them into stdout.

Set1 and set2 are character classes or character sets. If the length of the two character sets is not equal, set2 will repeat its last character until it is equal to the length of set1. If the length of set2 is greater than set1, then the part that exceeds the length of set1 in set2 will be ignored.

3 to convert input characters from uppercase to lowercase, you can use the following command

Echo "HELLO WHO IS THIS" | tr 'Amurz' 'aMuz' / / 'Amurz' and 'Amurz' are collections

4 tr has an option-d to delete specific characters in the stdin that appear by specifying the set of characters to be deleted.

Echo "Hello 13 world 345" | tr-d'0-9'/ / command can delete the digits in stdin and output them.

5 We can use the complement of set1 with the option-c.-c set1 is equivalent to defining a collection whose characters are not included in the set1.

6 tr can use a variety of different character classes like collections, and these character types are as follows

Alnum: letters and numbers

Alpha: letter

Cntrl: control character

Digit: numeric

Graph: graphic character

Lower: lowercase letter

Print: printable character

Punct: punctuation

Space: White space character

Upper: uppercase letters

Xdigit: hexadecimal character

7 you can choose the character set to use as follows: tr [: class1:] [: class2:]

Sort, single and duplicate

The 1 sort command can take input from either a specific file or stdin, and write input to stdout,uniq in the same working mode as sort

We can easily sort a set of files in the following way

The sort file1 file2 file3... > sorted / / command sorts multiple files and redirects them to sorted.

3 sort by number: sort-n file

Sort in reverse order: sort-r file

Sort by month: sort-M file

4 if you need to merge two ordered files, and you do not need to sort the merged files, you can use the

Sort-m sorted1 sorted2

The 5 uniq command finds a single line from a given input by eliminating duplicates. It can also be used to find duplicate lines in the input.

6 uniq can only be used for sorted data input, so uniq either uses pipes or uses sorted files as input

Show only the only line: uniq-u sorted

Count the number of occurrences in a row: uniq-c sorted

Find the duplicate lines in the file: uniq-d sorted

Split files and data

1 generate a test file with the size of 100KB

Dd if=/dev/zero bs=100K count=1 of=data.file

The above command creates a file with uppercase 100KB that contains all zeros.

We can specify the split size to split the file into several smaller files

Split-b 10k data.file / / this command splits data.file into multiple files, each with a file size of 10KB

Split the file name based on the extension

With the help of%, we can easily extract the name from the file name in the format of "name. Extension"

2 ${var%.*} means

First: delete the string matched by the wildcard to the right of% from $var, which matches from right to left

Second: assign a value to var, var=sample.jpg, so the wildcard matches from right to left to .jpg, so delete it

3% is a non-greedy operation, which matches the shortest result of wildcards from right to left. The other is that% is greedy and matches the longest result.

4 similar # is the same as%, that is, its matching is from left to right, which belongs to non-greedy operation.

So # # is similar to%%

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