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

The usage of find command and sort command

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

Share

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

Find command

The Linux find command is used to find files in the specified directory. Any string that precedes a parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command looks for subdirectories and files under the current directory. And all the subdirectories and files found are displayed.

Parameter description:

Find judges path and expression according to the following rules, the first-(), on the command line! The first part is path, followed by expression. The current path is used if the path is an empty string, and-print is used as the default expression if the expression is an empty string.

There are as many as 20 or 30 options available in expression, and only the most commonly used ones are introduced here.

-type searches by file type

B: block device file

C: character device file

D: catalog file

P: named pipe

F: ordinary files

L: symbolic link file

-exec is equivalent to shell executing commands such as rm,mkdir,touch, etc.

-Files before the mtime modification time, such as those 3 days ago, are generally used for log cleaning to match

-mount,-xdev: only check and specify files in the same file system to avoid listing files in other file systems

-amin n: read in the past n minutes

-anewer file: a file that was read later than the file file

-atime n: files that have been read in the past n days

-cmin n: modified in the past n minutes

-cnewer file: files that are newer than the file file

-ctime n: files that have been modified in the past n days

-empty: empty file-gid n or-group name: is gid n or group name name

-ipath p,-path p: files whose path names match p. Ipath ignores case.

-name name,-iname name: a file whose name matches name. Iname ignores case

-size n: the file size is n units, b represents a block of 512 bytes, c represents the number of characters, and k indicates that kilo bytes,w is two bytes.

-ok is usually used when using rm.-ok rm {}\

Select y for deletion and n for no deletion

View files in git-eureka or current directory that end with jar

[root@zhaocheng] # find git-eureka/-name'*. Jar'git-eureka/eureka-service/target/eureka-service.jar [root@zhaocheng] # find. -name "* .jar". / pp/eureka-service/target/eureka-service.jar./git-eureka/eureka-service/target/eureka-service.jar

Find out the directory under the 00 directory-d is the directory and-f is the general file

[root@zhaocheng ~] # find 00-type d0000/.git00/.git/objects00/.git/objects/pack00/.git/objects/52 [root@zhaocheng ~] # find 00-type f00/eureka-service-target.zip00/eureka.yaml00/.git/config00/.git/description00/.git/objects/52/c6e76bc253b21e1e59074e5730edb74b0af399

1 > access time (access time is abbreviated as atime)

2 > modification time (modify time is abbreviated as mtime)

3 > status modification time (change time is abbreviated as ctime)

Find out which directories are accessed under the current directory

[root@zhaocheng] # find. -atime-10./number.txt./.viminfo./filetest./fileteste./.cache/abrt/lastnotification./vim./harbor-offline-installer-v1.10.1.tgz./filetest2

Go to the logs directory of the micro service to find the log of 40 days ago and delete it. Exec is equivalent to shell command, and you can perform ls touch rm and other related operations.

[root@localhost] # find / data/deploy/rebuild2/restful-workorder/ROOT/logs/-mtime + 40-name "* .log"-exec ls {}\ / data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-28.log/data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-27.log/data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-22.log/data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-23.log/data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-25.log / data/deploy/jinre/restful-workorder/ROOT/logs/restful-2019-11-26.log [root@sitWeb ROOT] # find / data/deploy/jinre/restful-workorder/ROOT/logs/-mtime + 40-name "* .log"-exec rm-rf {}\

Or use find and | xargs to delete the log of log

[root@zhaocheng log] # find / var/log/anaconda//var/log/anaconda//var/log/anaconda/storage.log/var/log/anaconda/ifcfg.log/var/log/anaconda/syslog/var/log/anaconda/a/var/log/anaconda/ks-script-A2uRCN.log/var/log/anaconda/program.log/var/log/anaconda/anaconda.log/var/log/anaconda/packaging.log/var/log/anaconda/ks-script-LPeMYl.log/var/log / anaconda/journal.log [root@zhaocheng log] # find / var/log/anaconda/-name'* log' | xargs rm

Or use-ok rm {}\ when deleting; here you will be prompted whether to delete, y to confirm deletion, n to do not delete

[root@localhost ~] # find / var/log/-name "* .gz"-size + 2m-ok rm {}\

< rm ... /var/log/secure-201912161576437482.gz >

? N

< rm ... /var/log/secure-202001111578684841.gz >

? N

< rm ... /var/log/secure-201912301577646721.gz >

? N

< rm ... /var/log/secure-201911221574363161.gz >

? N

< rm ... /var/log/secure-202001271580065561.gz >

? N

< rm ... /var/log/secure-201912241577130301.gz >

? N

< rm ... /var/log/secure-202001051578164821.gz >

? N

< rm ... /var/log/secure-202002091581191341.gz >

? N

< rm ... /var/log/secure-201912041575402301.gz >

? N

< rm ... /var/log/secure-201912111576006622.gz >

? N

For example, if you want to find out where the configuration file of nginx.conf is placed, and if you want to see if there is any configuration of php,-name, find the file that matches-name.

[root@localhost scripts] # find /-name nginx.conf/usr/local/nginx/conf/nginx.conf/root/nginx/nginx-1.16.1/conf/nginx.conf [root@localhost scripts] # find / root/nginx/nginx-1.16.1/conf/-name "* .conf"-exec grep php {}\; # location ~\ .php$ {# location ~\ .php$ {# fastcgi_index index.php

Move the repo source under yum.repos.d to bak. {} is the content, followed by a directory.

[root@localhost ~] # find / etc/yum.repos.d/-name "* .repo"-exec mv {} / etc/yum.repos.d/bak\; [root@localhost ~] # ls / etc/yum.repos.d/bak

The find command is used with the grep command to find the file with filetest under / root and the line with halt

[root@localhost ~] # find / root/-name "filetest"-exec grep "halt" {}\; # halt:x:7:0:halt:/sbin:/sbin/halt

The find command is used in conjunction with the sed command to find the file with filetest under / root, find the line with halt, and delete its comments that begin with #

[root@localhost ~] # find / root/-name "filetest"-exec grep "halt" {}\; | sed's / ^ # / halt&/' halt#halt:x:7:0:halt:/sbin:/sbin/halt

The find command is used in conjunction with the awk command to find the file filetest under / root, filter out the line with halt, and print its second column with: as the delimiter

[root@localhost ~] # find / root/-name "filetest"-exec grep "halt" {}\; | awk-F ":"'{print $2}'x

Find files larger than 30m under the current file, xargs filter, and generally use find filtering together with pipes

[root@zhaocheng git-eureka] # find. -type f-size + 30m | xargs du-sh46M. / eureka-service/target/eureka-service.jar40M. / eureka-service.zip40M. / .git / objects/c8/aeecf5dbe33c9e24a049738fcccc2aa8b4ff10

Find files larger than 50m under / directory and check the file size, sort backwards, type type general users specify f as file, or d as directory,-size size, xargs for pipe statistics, sort-n is the specified number, r is sorted backwards

[root@zhaocheng ~] # find /-type f-size + 50m | xargs du-sh | sort-nrfind:'/ proc/8705/task/8705/fdinfo/6': No such file or directoryfind:'/ proc/8705/fdinfo/5': No such file or directory500M / opt/binary_pkg.gz268M / opt/binary_pkg/image.tar.gz157M / data/soft/logstash-7.5.0.tar.gz136M / var/lib/rpm/Packages136M / usr/local/qcloud/YunJing/VulnerDetect/yhvs/Packages

For docker, each container that runs generates logs, that is, its working directory / var/lib/docker/containers/xxx/xxx-json.log

If we usually clean up, we also need to clean up this directory. Now find out the log in this directory that is greater than 2G. To delete logs, you generally need to use cat / dev/null > xxx-json.log. Using rm-rf to delete logs that are being written will not reduce disk space.

[root@k8s-node1 ~] # find / var/lib/docker/containers/-name *-json.log-size + 2G | xargs du-sh | sort-nr

Find out the larger directories in the first 20 lines and sort them backwards.

-h,-human-readable outputs file size information in a comparative reading manner (for example, 1K 234m 2G). Note: this option is also valid in many other commands (df, ls).

-m is calculated in megabytes (M)

-max-depth=N only lists the information of directories and files whose depth is less than max-depth-the effect of max-depth=0 is the same as-s

[root@zhaocheng ~] # du-hm-- max-depth=2 | sort-nr | head-20642. 126. / git-eureka86. / pp81. / 99/.git81. / 9981. / 88/.git

The uniq command removes duplicate lines from the file. The uniq command reads the standard input or file specified by the InFile parameter. This command first compares adjacent lines, and then removes the second line and subsequent copies of that line.

Usage: uniq [option]... (input [output]]

Filter adjacent matching rows from input (or standard input)

Write to output (or standard output).

Take this file as an example.

[root@zhaocheng ~] # cat filetestroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinhalt:x:7:0:halt:/sbin:/sbin/halt

Parameter interpretation

-c calculate the number of prefix lines according to the number of occurrences

-d prints only duplicate lines

-D prints all duplicate lines

If there is no option, the matching rows will be merged into the first row that appears.

[root@zhaocheng ~] # uniq filetestroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinhalt:x:7:0:halt:/sbin:/sbin/halt

-c count all the fields in the text, and one of them will be counted.

[root@zhaocheng ~] # uniq-c filetest 3 root:x:0:0:root:/root:/bin/bash 1 bin:x:1:1:bin:/bin:/sbin/nologin 1 daemon:x:2:2:daemon:/sbin:/sbin/nologin 1 adm:x:3:4:adm:/var/adm:/sbin/nologin 1 halt:x:7:0:halt:/sbin:/sbin/halt

-d prints only duplicate lines

[root@zhaocheng] # uniq-d filetestroot:x:0:0:root:/root:/bin/bash

-D prints all duplicate lines,-d prints duplicate lines only once

[root@zhaocheng] # uniq-D filetestroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bash

Sort

Usage: sort [options]. [document]...

Or: [options]... -- files0-from = F

Writes the sort connection of all files to standard output.

-b ignores space characters that begin at the beginning of each line.

-c check whether the files have been sorted in order.

When sorting, uppercase and lowercase letters are ignored.

-M sorts the first three letters according to the abbreviation of the month.

-n is sorted by the size of the number.

-o saves the sorted results to the specified file.

-r is sorted in reverse order.

-t specifies the field delimiter used for sorting.

-k chooses which interval to sort.

[root@zhaocheng ~] # sort filetestadm:x:3:4:adm:/var/adm:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinhalt:x:7:0:halt:/sbin:/sbin/haltroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bash

If you change it to a number here, you need to use sort-n to sort it.

[root@zhaocheng] # sort-n number.txt 2357566580

-r means to arrange in reverse order.

[root@zhaocheng] # sort-n-r number.txt 8065567532

For example, there are a lot of log files under our linux/var/log, such as systematically scheduled tasks, and so on, but there are also many files, which can be sorted through sort for easy viewing.

[root@k8s-master1 log] # du-sh * | sort-n-r755m messages236K wtmp132K cron-20200216132K cron-20200209132K cron-20200202132K cron-2020012668K cloud-init.log52K cron36K dmesg34M audit25M sa8.0K tuned8.0K boot.log-20191021

Sort-u can also sort and remove duplicates.

[root@zhaocheng] # sort-u filetestadm:x:3:4:adm:/var/adm:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinhalt:x:7:0:halt:/sbin:/sbin/haltroot:x:0:0:root:/root:/bin/bash

To save the contents of our output, we need-o to save it to the specified file.

[root@zhaocheng] # sort-n-r number.txt-o number.txt [root@zhaocheng] # cat number.txt 986589087656553464

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