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

Brief introduction of if case statement find locate File search and Compression and decompression tool

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

Share

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

1 condition selection of shell script programming if condition judgment case 2 file search find locate and compression and decompression tools

Procedural programming language:

Sequential execution

Select execution

Circular execution

If statement for conditional selection

Select execute:

Note: if statements can be nested

Single branch if judgment condition: end of branch code fi if the then condition is true

Two-branch if judgment condition; branch code whose then condition is true else end if the branch code condition is false fi

Multiple branches

If CONDITION1; then

If-true

Elif CONDITION2; then

If-ture

Elif CONDITION3; then

If-ture

...

Else

All-false

Fi

Judge by condition, execute its branch when the first encounter is "true" condition, and then end the entire if statement

For example

Execute the command according to its exit status

If ping-C1-W2 station1 & > / dev/null; then

Echo 'Station1 is UP'

Elif grep "station1" ~ / maintenance.txt & > / dev/null then

Echo 'Station1 is undergoing maintenance'

Else

Echo 'Station1 is unexpectedly downs'

Exit 1

Fi

Conditional judgment: case statement

The case variable refers to in

PAT1)

Branch 1

PAT2)

Branch 2

. *)

Default branch

Esac

Case supports glob-style wildcards:

*: any length, any character

?: any single character

[]: any single character in the specified range

A | b: an or b

Locate and find for file lookup

File lookup: locate, find

Locate: non-real-time search (database search if you want to accurately find an updatable database, but it takes up system resources during the update process)

Find: real-time search (accurate search but slow search)

Locate

Query the pre-built file index database on the system

Path to the file index database: / var/lib/mlocate/mlocate.db

Rely on pre-built indexes

Index construction is carried out automatically when the system is idle (periodic tasks); the administrator updates the database manually (updatedb); the index construction process needs to traverse the entire root file system, which consumes a lot of resources

Job characteristics:

Search speed is fast.

Fuzzy search

Non-real-time search searches the full path of the file, not just the file name, which may only search for directories where the user has read and execute permissions.

Locate command

Locate KEYWORD

Useful options

-I perform case-sensitive search

-n X enumerates only the first X matching items

Locate foo searches for files with "foo" in their name or path

Locate-r'\ .foo $'uses Regex to search for files ending in ".foo"

Find

Real-time search tool to complete file search by traversing the specified path

Job characteristics:

The speed of searching is a little slower.

Precise search

Real-time search may only search directories for which the user has read and execute permissions

Syntax:

Find [OPTION]... [search path] [search condition] [processing action]

Find path: specify a specific target path; default is the current directory

Search criteria: specified search criteria, such as file name, size, type, permissions, etc.; default is to find out all file processing actions under the specified path: operate on files that meet the criteria, and output to the screen by default

Search condition

1 File name and inode lookup

2 genera main group search

3 File type lookup

4 File size lookup

5 timestamp lookup

6 permission lookup

Look up by file name and inode:

-name "file name": glob *,?, [], [^] is supported.

-iname "file name": case-insensitive

-inum n search by inode number

-samefile name files with the same inode number

-links n files with n links-regex "PATTERN": matches the entire file path string with PATTERN, not just the file name

Look up according to the owner and group:

-user USERNAME: find files whose owner is the specified user (UID)

-group GRPNAME: find files that belong to the specified group (GID)

-uid UserID: find the file whose owner is the specified UID number

-gid GroupID: find files with the specified GID number in the group

-nouser: find files without owners

-nogroup: find files that do not belong to a group

Look up by file type:

-type TYPE:

F: ordinary files

D: catalog file

L: symbolic link file

S: socket file

B: block device file

C: character device file

P: pipe fil

Look up according to the file size:

-size [+ | -] # UNIT

Common units: K, M, G

# UNIT: (#-1, #], such as 6 5-6

-# UNIT: [0jurist muri 1], for example:-60-5

+ # UNIT: (#, ∞), for example: + 66-∞

According to the timestamp:

In terms of "days"

-atime [+ | -] #

#: [#, # + 1) such as: 6 6-7

+ #: [# + 1, ∞] such as: + 66-∞

-#: [0BZ #) such as:-60-6

-mtime

-ctime

In minutes:

-amin

-mmin

-cmin

Find based on permissions:

-perm [/ | -] MODE

MODE: exact permission matching

/ MODE: as long as one of the permissions of any kind of object can match, or relationship, + be eliminated from centos7.

-MODE: each type of object must have the specified permissions at the same time, and relationship 0 means no concern

Find-perm 755 will match files whose permission mode happens to be 755

Find-perm + 222will match as long as anyone has write permission

Find-perm-222will match only if everyone has write permission.

Find-perm-002will match only if someone else (other) has write permission.

Processing action

-print: default processing action, displayed to the screen

-ls: similar to the "ls-l" command on the found file

-delete: delete the found file

-fls file: the long format information of all found files is saved to the specified file

-ok COMMAND {}\; execute the command specified by COMMAND for each file found; before executing the command for each file, the user is interactively asked for confirmation

-exec COMMAND {}\; execute the command specified by COMMAND for each file found

{}: used to reference the found file name itself

When find passes the found file to the command specified later, it finds all the qualified files and passes them to the following command at one time. Some commands cannot accept too many parameters, and the command execution may fail. The following method can avoid this problem find | xargs COMMAND

Combination conditions:

And:-a

Or:-o

Non:-not!

De Morgan's law: non (P and Q) = (non-P) or (non-Q) non (P or Q) = (non-P) and (non-Q)

! a-a! B =! (A-o-B)! a-o! B =! (A-a-B)

Add parentheses to take the reverse.

Compression and decompression tool

1 compress [- dfvcVr] [- b maxbits] [file...]

-d: decompress, equivalent to

-c: the result is output to standard output without deleting the original file

-v: display details

Uncompress unzips zcat file.Z > file

2 gzip [OPTION]... FILE...

-d: decompress, which is equivalent to gunzip

-c: output the result of compression or decompression to standard output

-#: 1-9, specify the compression ratio. The higher the value, the greater the compression ratio.

Zcat: view the contents of a text file without explicit decompression

Example: gzip-c messages > messages.gz gzip-c-d messages.gz > messages zcat messages.gz > messages

3 bzip2 [OPTION]... FILE...

-k: keep, keep the original file

-d: decompress

-#: 1-9, compression ratio. Default is 6.

Bzcat: view the contents of a text file without explicit decompression

4 xz [OPTION]... FILE...

-k: keep, keep the original file

-d: decompress

-#: 1-9, compression ratio. Default is 6.

Xzcat: view the contents of a text file without explicit decompression

5 packing and compressing

Zip-r sysconfig sysconfig/

Unpack and decompress

Unzip sysconfig.zip

Cat / var/log/messages | zip message

Unzip-p message > message

Practice

1. Find all the files under the / var directory whose master is root and whose group is mail.

Find / var-user root-group mail-ls

Computer demonstration

[root@localhost /] # find / var-user root-group mail-ls

1443089 4 drwxrwxr-x 2 root mail 4096 Aug 13 10:34 / var/spool/mail

1444185 4-rw- 1 root mail 2688 Aug 13 09:38 / var/spool/mail/root

[root@localhost /] #

2. Find all the files in the / var directory that do not belong to root, lp or gdm

Find / var-not\ (- user root-o-user lp-o-user gdm\)-ls

Computer demonstration

Root@localhost /] # find / var-user root-group mail-ls

1443089 4 drwxrwxr-x 2 root mail 4096 Aug 13 10:34 / var/spool/mail

1444185 4-rw- 1 root mail 2688 Aug 13 09:38 / var/spool/mail/root

[root@localhost /] # find / var-not\ (- user root-o-user lp-o-user gdm\)-ls

1443544 4 drwx--x--- 2 postfix postdrop 4096 Aug 12 13:57 / var/spool/postfix/public

1444012 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/public/showq

1444007 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/public/flush

1444000 0 prw--w--w- 1 postfix postfix 0 Aug 13 11:34 / var/spool/postfix/public/qmgr

1443999 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/public/cleanup

1443998 0 prw--w--w- 1 postfix postfix 0 Aug 13 11:38 / var/spool/postfix/public/pickup

1443543 4 drwx- 2 postfix root 4096 Aug 12 13:57 / var/spool/postfix/private

1444014 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/private/retry

1444018 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/private/lmtp

1444017 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/private/virtual

1444016 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/private/local

1444019 0 srw-rw-rw- 1 postfix postfix 0 Aug 12 13:57 / var/spool/postfix/private/anvil

3. Find files whose contents have been modified in the last week in the / var directory, and the owner is neither root nor postfix.

Find / var-mtime-7-not\ (- user root-o-user postfix\)-ls

Computer demonstration

[root@localhost /] # find / var-mtime-7-not\ (- user root-o-user postfix\)-ls

1444198 0-rw-rw---- 1 dadi mail 0 Aug 13 10:18 / var/spool/mail/dadi

1444120 0-rw-rw---- 1 tiantian mail 0 Aug 13 10:17 / var/spool/mail/tiantian

1444075 0-rw-rw---- 1 tian mail 0 Aug 12 14:53 / var/spool/mail/tian

1444174 0-rw-rw---- 1 hh mail 0 Aug 13 10:34 / var/spool/mail/hh

1443974 4-rw-r--r-- 1 rpcuser rpcuser 5 Aug 12 13:56 / var/run/rpc.statd.pid

1443210 4 dr-x--x--x 2 lp sys 4096 Aug 12 13:56 / var/run/cups/certs

1444136 4 drwx--x--x 2 gdm gdm 4096 Aug 12 13:57 / var/run/gdm/auth-for-gdm-Z9Ky4G

1444186 4-rw- 1 gdm gdm 66 Aug 12 13:57 / var/run/gdm/auth-for-gdm-Z9Ky4G/database

[root@localhost /] #

4. Find files that have no owners or groups on the current system and have been accessed in the most recent week

Find /\ (- atime-7-nouser-o-nogroup\)-ls

Computer demonstration

[root@localhost /] # find /\ (- atime-7-nouser-o-nogroup\)-ls

Find: `/ proc/5584/task/5584/fd/5': No such file or directory

Find: `/ proc/5584/task/5584/fd/5': No such file or directory

Find: `/ proc/5584/task/5584/fdinfo/5': No such file or directory

Find: `/ proc/5584/task/5584/fdinfo/5': No such file or directory

Find: `/ proc/5584/fd/5': No such file or directory

Find: `/ proc/5584/fd/5': No such file or directory

Find: `/ proc/5584/fdinfo/5': No such file or directory

Find: `/ proc/5584/fdinfo/5': No such file or directory

[root@localhost /] #

5. Find all files in the / etc directory that are larger than 1m and whose types are ordinary files

Find / etc-size + 1m-type f

Computer demonstration

[root@localhost /] # find / etc-size + 1m-type f

/ etc/selinux/targeted/modules/active/policy.kern

/ etc/selinux/targeted/policy/policy.24

/ etc/gconf/gconf.xml.defaults/%gconf-tree.xml

[root@localhost /] #

6. Find files in the / etc directory that all users do not have write permission to

Find / etc/-not-perm / 222-ls

Find / etc/-not-perm + 222-ls

Computer demonstration

[root@localhost /] # find / etc/-not-perm / 222-ls

787420 4-root root 460 May 11 13:00 / etc/dbus-1/system.d/cups.conf

788058 4-root root May 11 01:32 / etc/ld.so.conf.d/kernel-2.6.32-642.el6.x86_64.conf

786821 4-r-1 root root 45 Jul 19 20:17 / etc/openldap/certs/password

786733 192-root root 194984 Jul 19 20:17 / etc/pki/ca-trust/extracted/java/cacerts

786729 340-root root 346654 Jul 19 20:17 / etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt

786730 256-root root 262042 Jul 19 20:17 / etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

786731 204-r etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem-1 root root 208874 Jul 19 20:17 / etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem

786732 208-root root 208976 Jul 19 20:17 / etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem

788589 4-root root Feb 22 07:18 / etc/lvm/profile/cache-smq.profile

788591 4-root root May 11 18:18 / etc/lvm/profile/metadata_profile_template.profile

788593 4-root root 80 Feb 22 07:18 / etc/lvm/profile/thin-performance.profile

788590 4-root root 2391 May 11 18:18 / etc/lvm/profile/

7. Find at least one class of files in the / etc directory that the user does not have the right to execute.

Find / etc/-not-perm-111111-ls

There are too many files to display

8. Find files under the / etc/init.d directory where all users have execute permission and other users have write permission

Find / etc/init.d/-perm-111a-perm / 002

Find / etc/init.d/-perm-113

Computer demonstration

[root@localhost /] # find / etc/init.d/-perm-111a-perm / 002

/ etc/init.d/

/ etc/init.d/f1

[root@localhost /] # find / etc/init.d/-perm-113

/ etc/init.d/

/ etc/init.d/f1

[root@localhost /] #

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

Database

Wechat

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

12
Report