1.批量创建文件

mkdir playground
touch playground/{A..Z}cadcs{2..3}.txt

注: 要在bash下执行, zsh下会存在{A..Z}无法展开的情况

2.查找以.txt结尾文件,并统计此类文件的个数

find playground -name *.txt | wc -l
find ./playground -name *.txt | grep 3.txt | wc -l
等同于
find ./playground -name *3.txt | wc -l

区分
find ./playground -name *.txt | grep [^YO]cadcs3.txt$
find ./playground -name *.txt | grep -v [YO]cadcs3.txt$
find ./playground -name *.txt | grep -c [^YO]cadcs3.txt$

3.往找到的文件中,批量写入内容

find . -name \*.txt -exec sh -c 'echo "abcdef" >> "$1"' -- {} \;
find . -type f -name \*.txt -exec sh -c 'echo "abcdef" >> "$1"' -- {} \;

注: 确保find出来的都是文件,否则写入的时候会报错

4.查找文件中的内容

grep -h hhh *.txt # -h 不显示文件名,只显示查到的结果
grep hhh *.txt | grep -n abc  # -n 显示行号
grep -E h{4} *.txt  # -E 支持正则

5.生成随机的电话号码

for i in {1..10}; do echo "(${RANDOM:0:3}) ${RANDOM:0:3}-${RANDOM:0:4}" >> phonelist.txt; done
grep -E '^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$' phonelist.txt   #找到符合规则的号码
grep -Ev '^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$' phonelist.txt   #找到不符合规则的号码

6.查找文件,排序

find . -name \*.txt | sort | head -n 5
find . -name \*.txt | xargs du -h | sort -r | head -n 5  #按大小排序,找到最大的5个
find . -type f -name \*.txt | xargs ls -lt | head -n 5   #按时间排序,找到最晚创建的5个, -t 按时间排序
find . -type f -name \*.txt | xargs ls -ltr | head -n 5  #按时间排序,找到最早创建的5个, -r 倒序

注: sort默认是按从小到大排序

备注: