This is the fourth day of my participation in Gwen Challenge
File search command
find
- Command name: find
- Execute permission: all users
- Function: File search
- Grammar:
Find [search scope]
Example 1, find by filename:
-
Find the file init in the directory /etc:
find /etc -name init Copy the code
-
Unlike Windows, Linux searches for init and only returns files with the name init, but Windows searches for files with the name init.
-
You can use -etec/ -OK to perform operations on the search results, such as finding the inittab file under /etc and displaying its details
find /etc -name inittab -exec ls -l {} \; Copy the code
- {} indicates a substitution for the result
- \ indicates an escape character
- ; End of the said
-
-
If you want to find a file containing init like Windows, do as follows:
find /etc -name *init* Copy the code
- Use wildcards
-
Using -iname to search is case insensitive
find /etc -iname init??? Copy the code
- Both initabc and initabc files will be searched
- Use regular expressions to search
Example 2: Find by file size:
-
Find files larger than 100MB in the root directory
find / -size +204800 Copy the code
-
Add a few query criteria (not only for size) :
- -a: Both search conditions are met
- -o: Meet either of the two conditions
- For example, search for files between 80MB and 100MB in /etc
find /etc -size +163840 -a -size -204800 Copy the code
-
-type: searches by file type
-
F: file
-
D: directory
-
L: Soft link file
-
For example, find a file whose file name starts with init and whose type is directory in /etc
find /etc -name init* -a type -d Copy the code
-
-
-inum: Searches based on node I
-
Each file has an I node, which can be viewed through ls -i.
-
For example, there is a file whose I node is 31531
find . -inum 31531 -exec rm {} \; Copy the code
-
Example 3: Find by owner:
-
Find the file with the owner AAA under /home:
find /home -user AAA Copy the code
- -group: searches by group
Example 4: Find by modification time:
-
Look for files and directories whose properties have been modified in the last 5 minutes under /etc
find /etc -cmin -5 Copy the code
- about
- 5
: The minus sign indicates how long it will take, and the plus sign indicates how long it will take - -cmin: search by file attribute (change)
- Such as through
ls -ld /home/AAA
You can see the file properties
- Such as through
- Add additional find options:
- -amin: Search by access time
- -mmin: search by file content (modify)
- about
locate
- Command name: locate
- Execute permission: all users
- Function: Find files in the file database
- Grammar:
Locate the file name
Note 1: The Locate search is fast because it periodically collects files into a file repository. So it searches directly in the database, and therefore faster than find.
Note 2: Sometimes locate does not find a file because the file has not yet been searched into the file database. You can use updatedb to update the database.
Note 3: Locate cannot be found if the file is in/TMP, even if you use updatedb
Note 4: If you want to be case-insensitive in lookups, you can add the use of -i. For example, locate -i helloworld.txt
which
- Command name: which
- Execute permission: all users
- Run the following command to search for the directory where a command resides and the alias information
- Grammar:
Which command
Note 1: The commands stored in /bin/usr/bin are available to all users. The commands stored in /sbin /usr/sbin are available to the root user.
Example:
/usr/sbin/useradd /usr/sbin/useradd /usr/sbin/useraddCopy the code
whereis
- Command name: whereis
- Execute permission: all users
- Run the following command to search for the directory where a command resides and the help file path
- Grammar:
Whereis [command name]
grep
- Command name: grep
- Execute permission: all users
- Function: Search a file for a string matching line and output it
- -i: case insensitive
- -v: excludes the specified string
- Grammar:
Grep -iv [string] [file]
The sample
root@atomy-virtual-machine: /home/atomy/desktop # grep DisableMBIMGlobal /etc/usb_modeswitch.conf DisableMBIMGlobal=0Copy the code
Note 1: most of the time there are # comments in the file. The commented code does not take effect. We can use -v to exclude a specified character, for example
grep -v # /etc/usb_modeswitch.conf Copy the code
But you can’t seem to find the information. Because there are so many # s in the file, valid lines sandwiched between them are also excluded. You can use
grep -v ^# /etc/usb_modeswitch.conf Copy the code
^ is a type of regular expression that indicates what to start with. The real power of grep lies in this. Grep and regular expression can find effective information well.
The help command
man
- Command name: man
- Original meaning: manual
- Execute permission: all users
- Function: Get help information
- Grammar:
Man [command or configuration file]
- (space) or f turn the page
- B Turn the page down
- Line feed (Enter)
- Q or Q exits
Example: View the help information about the ls command
man ls
Copy the code
Example: View help information about configuration file services
man services
Copy the code
help
- Command name: help
- Execute permission: all users
- Function: Obtain help information about Shell built-in commands
- Grammar:
The help command
Example: View the help information of the umask command
help umask
Copy the code
Decompression command
gzip
- Command name: gzip
- GNU zip
- Execute permission: all users
- Function: Compress files
- Compressed file format:.gz
- Grammar:
Gzip [file]
gunzip
- Command name: gunzip
- GNU unzip
- Execute permission: all users
- Function: Decompress the. Gz compressed file
- Only files can be compressed, not directories
- Grammar:
Gunzip [compressed file]
tar
-
Command name: tar
-
Execute permission: all users
-
Syntax: tar option [-zcf] [compressed file name] [directory]
-
Function: package directory, compressed file format:.tar.gz
- – c: packaging
- -v: Displays detailed information
- -f: Specifies the file name
- -z: package and compress
- – x: unpack
Example: compression
I have a file tartext.txt in tartest folder in TMP. I’m now compressing the directory.
Package and compress the directory tartest as a.tar.gz file
tar -zcf tartest.tar.gz tartest
Copy the code
Example: Decompress
root@atomy-virtual-machine:/tmp# tar -zxvf tartest.tar.gz
tartest/
tartest/tartest.txt
Copy the code
zip
- Command name: zip
- Execute permission: all users
- Function: Compress files or directories
- The compressed file format is.zip
- Grammar:
Zip option [-r] [compressed file name] [file or directory]
- -r Indicates the compressed directory
Example: Compressed file
zip tartest.txt.zip tartest.txt
Copy the code
Example: Compressed directory
zip tartest.zip tartest
Copy the code
unzip
- Command name: unzip
- Execute permission: all users
- Function: Decompress the. Zip file
- Grammar:
Unzip [compressed file]
bzip2/bunzip2
-
Syntax: bzip2 option [-k] [file]
- -k Saves the compressed file after it is generated
- The compressed file format is.bz2
-
Syntax: bunzip2 option [-k] [zip file]
- -k Retain the original file after decompressing it