Usage
search for files in a directory hierarchy
1
|
find [path...] [options] [expression]
|
An expression is composed of a sequence of things:
- Tests
- Actions
- Global options
- Positional options
- Operators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Wildcard
find . -name "*.txt"
find . -name "2020*.csv"
find . -name "json_*"
# regex
# TODO
# directory
find . -type d
# file
find . -type f
## Size
# Find all bigger than 10MB files, 查找大于10M的文件
find . -size +10M
# Find all smaller than 10MB files, 查找小于10M的文件
find . -size -10M
# Find Size between 100MB and 1GB, 查找大于100M且小于1G的文件
find . -size +100M -size -1G
|
multiple dirs & filenames
1
2
3
4
|
# Find files with multiple dirs
find /opt /usr /var -name foo.scala -type f
# Find files with .sh and .txt extensions
find . -type f \( -name "*.sh" -o -name "*.txt" \)
|
Find and …
find and replace
1
2
3
4
5
6
7
|
# Find all files and modify the content const to let
# s表示substitute, 替换操作
# g表示global, 全局替换
find ./ -type f -exec sed -i 's/const/let/g' {} \;
# Find readable and writable files and modify the content old to new
find ./ -type f -readable -writable -exec sed -i "s/old/new/g" {} \;
|
find and move
1
2
3
|
# Find and move it to a specific directory (/tmp/music)
# {} 表示find找到的每一个文件名(相当于PowerShell中的 $_)
find . -name '*.mp3' -exec mv {} /tmp/music \;
|
Find and concatenate
1
2
3
4
|
# Merge all csv files in the download directory into merged.csv
find download -type f -iname '*.csv' | xargs cat > merged.csv
# Merge all sorted csv files in the download directory into merged.csv
find download -type f -iname '*.csv' | sort | xargs cat > merged.csv
|
find and delete
1
2
3
4
5
6
|
# Find all file names ending with .pdf, then remove them.
find -name \*.pdf | xargs rm
# The above, however, is better-written without xargs:
find -name \*.pdf -exec rm {} \+
# Although it's best to use find's own functionality, in this situation.
find -name \*.pdf -delete
|
find
Find Command Cheat Sheet & Quick Reference