Command line tool retrieval commands find and grep

grep command to retrieve what a file contains

Use the grep command to retrieve what a file contains, for example:

```

grep "search term" file.txt

```

This command will look for lines containing "search term" in file.txt and output them to the terminal. The search can be further customized using different options and parameters.

If you search for all the files in a directory and check whether each file contains a certain content, what command do you use?

You can use the following command to find all files in a directory and retrieve whether each file contains a certain content:

```

grep -r "what to look for" /path/to/directory

```

Among them, `-r` means to recursively search for all files in the directory, `"what to find"` is the content to be found, and `/path/to/directory` is the directory path to be found.

This command will find all files containing the specified content in the directory, and output the file path and line number containing the content. If the content you want to find is a regular expression, you can use the `-E` parameter to expand it.


find find directory name command

You can use the following command to find the directory name:

1. Find the directory name under the current directory:

```

find . -type d -name "directory name"

```

2. Find the directory name under the specified directory:

```

find /path/to/directory -type d -name "directory name"

```

Among them, "directory name" is the name of the directory you want to find, and "/path/to/directory" is the path of the directory you want to search.

This command will recursively search all directories under the specified directory and return all directories matching the specified name.


sed file manipulation

sed is a stream editor that can be used to transform and replace text. Its usage scenarios include:

1. Batch text replacement: You can use the sed command to find and replace specified text strings in a file.

2. Extract text: You can use the sed command to extract a specified line or block of text from a file.

3. Format text: You can use the sed command to format the text, such as adding line numbers, spaces, etc.

4. Delete text: You can use the sed command to delete a specified line or block of text in a file.

5. Other operations: You can also use the sed command to perform operations such as sorting, deduplication, and grouping.

The basic syntax of the sed command is:

sed [options] 'action' filename

Wherein, an action is an operation on the text, which may be operations such as replacement, deletion, and insertion. filename is the filename or list of files to process.

Commonly used sed command options include:

-i: directly modify the file content.

-n: Only print the content processed by sed.

-e: Allow multiple sed commands to be used in series.

For example, to replace all occurrences of hello in a file with world, the following command can be used:

sed -i 's/hello/world/g' filename

Among them, s represents the replacement operation, and g represents the global replacement.


Pipeline | Connection Operations

How do the find grep and sed mentioned above use pipeline | operations to connect and combine into more complex file operations?

The pipe | operation can take the output of one command as the input of another, so you can use the pipe operation to combine find, grep, and sed for more complex file operations, such as:

1. Use find to find files, then use grep to filter out files containing a specific string, and finally use sed to replace:

```

find . -type f | grep "pattern" | sed 's/old/new/g'

```

2. Use find to find files, then use sed to perform replacement operations, and finally use grep to filter out files that contain specific strings after replacement:

```

find . -type f -exec sed -i 's/old/new/g' {} \; | grep "pattern"

```

3. Use find to find files, then use sed to perform replacement operations, and finally use grep to filter out files that do not contain a specific string after replacement:

```

find . -type f -exec sed -i 's/old/new/g' {} \; | grep -v "pattern"

```

The above are just some simple examples. In fact, find, grep, and sed can be combined to perform more complex file operations according to specific needs.


Commonly used file operation commands

In addition to find grep sed, are there other commonly used file operation commands, and are they listed and used with examples?

1. awk: used for text processing, you can segment, filter, calculate and other operations on the text according to the specified rules.

Example: Count the number of words in each line in a file

```bash

awk '{print NF}' file.txt

```

2. cut: used to extract the content of the specified column from the text.

Example: Extract the contents of the first and third columns in the file

```bash

cut -f1,3 file.txt

```

3. sort: used to sort the text.

Example: Sort by the first column

```bash

sort -k1 file.txt

```

4. uniq: Used to remove duplicate rows.

Example: remove duplicate lines in a file

```bash

uniq file.txt

```

5. wc: Used to count the number of lines, words, bytes and other information of the file.

Example: Count the number of lines in a file

```bash

wc -l file.txt

```

6. head: Used to display the first few lines of the file.

Example: Display the first 10 lines of a file

```bash

head -n 10 file.txt

```

7. tail: Used to display the last few lines of the file.

Example: display the last 10 lines of the file

```bash

tail -n 10 file.txt

```

8. tee: Used to output to the screen and file at the same time in the command line.

Example: Outputting command line output to the screen and to a file at the same time

```bash

ls | tee file.txt

```


Comparison table for more basic commands

command name

effect

Example of use

Precautions

cd

switch current working directory

cd documents

path is case sensitive

ls

Display files and folders in the current directory

ls -a

Different parameters can be used to display different information

pwd

Display the path of the current working directory

pwd

none

mkdir

create new folder

mkdir new_folder

Folder names cannot contain special characters

touch

create new file

touch new_file.txt

Filenames cannot contain special characters

cat

view file content

cat file.txt

Can view the contents of multiple files at the same time

cp

Copy a file or folder

cp file.txt new_file.txt

The file or folder to be copied must exist

mv

Move a file or folder

mv file.txt documents/

The destination folder must exist

rm

delete file or folder

rm file.txt

Files or folders cannot be recovered after deletion

is rm

delete empty folder

rmdir empty_folder

Only empty folders can be deleted

chmod

Modify file or folder permissions

chmod 755 file.txt

Need to understand the concept of permissions and the meaning of different permissions

grep

Find the specified string in the file

grep "hello" file.txt

You can find a specified string in multiple files

Guess you like

Origin blog.csdn.net/u011024243/article/details/129617385