- The grep command:
- Finding a string in a single file:
grep "literal_string" fileName
- Example:
grep "this" demo_file
- Example:
- Find strings in multiple files:
grep "string" fileNamePattern
- Example:
grep "this" demo_*
- Example:
- Case insensitive:
grep -i "literal_string" filename
- Example:
grep -i "this" demo_file
- Example:
- Regular expression lookup:
grep "REGEX" filename
- Example:
grep "lines.*empty" demo_file
- Example:
- Find a whole word, not a substring:
grep -w "literal_string" demo_file
- Example:
grep -w "is" demo_file
- Example:
- Find all files under the current file:
grep -r "literal_string"
- Example:
grep -r "is"
- Example:
- Number of matches counted:
grep -c "pattern" filename
- Example:
grep -c "is" demo_file
- Example:
- Display only matching filenames:
grep -l "pattern" filename
- Example:
grep -l "is" demo_file
- Example:
- Display the matching line number:
grep -n "pattern" filename
- Example:
grep -n "is" demo_file
- Example:
- Finding a string in a single file:
- The find command:
- Find at the specified path:
find pathName -name fileName
- The sample
find / -name mysqld.log
- The sample
- Find in the specified path, case insensitive:
find pathName -iname fileName
- The sample
find / -iname mysqld.log
- The sample
- Specify the depth of the search depth:
find pathName -mindepth mindepth -maxdepth maxdepth -name fileName
- The sample
find / -mindepth 2 -maxdepth 3 -name mysqld.log
- The sample
- Specify the file type:
find pathName -name fileName -type typeName
- The sample
find / -name test.txt -type -f
- – the type:
- B: Block device file
- C: character device file
- D: Directory file
- F: Ordinary files
- P: pipe file
- L: Symbolic links
- The sample
- Specify file size:
find pathName -size fileSize
- Find files larger than 100 M:
find / -size +100M
- Find files smaller than 100 M:
find / -size -100M
- Find files larger than 100 M:
- Select * from inode;
find -inum inodeNumber
- Example:
find -inum 2165021
- Example:
- Files used or modified within the specified date range:
find / -name fileName -mtime n
- Search for files whose modification time is more than 100 days from the current time:
find / -mtime +100
- -mtime indicates the modification time, and -atime indicates the access time
- +n indicates that the number is exceeded. -n indicates that the number is not exceeded
- Search for files whose modification time is more than 100 days from the current time:
- After finding the file, execute the corresponding command:
find / -name fileName -exec command {} \;
- File information is displayed after the file is found:
find / -name test.txt -exec ls -l {} \;
- File information is displayed after the file is found:
- Find the largest or smallest files:
- Find the largest 5 files:
find . -type f -exec ls -s {} \; | sort -n -r | head -5
- Find the smallest 5 files:
find . -type f -exec ls -s {} \; | sort -n | head -5
- Find the largest 5 files:
- Find at the specified path:
- The locate command:
- Find a file:
locate fileName
- Example:
locate *.doc
Locate finds files in a database that has been created. Sometimes a file has changed, for example by deleting it, but locate still finds the file because the file database has not been updated. This parameter is required if the file database needs to be updatedupdatedb
Command.
- Example:
- Find a file:
- [1] : The Ultimate Tar Command Tutorial with 10 Practical Examples [2] : 15 Practical Linux Find Command Examples [3] : Linux from Beginner to Master (2nd edition)