【Linux】搜索文件,将路径写入文件中
来自实验楼的一道题
大意是搜索/etc目录下,所有文件内容包含shiyanlou字段的文件。
将文件名按行存入/home/shiyanlou/output, 要求没有重复项
最开始看错了,看成搜索文件名称包含指定字段的文件名,于是:
sudo find /etc -name shiyanlou > /home/shiyanlou/output
再看清题后:
sudo grep -r shiyanlou /etc | awk '{FS=":"}{print $1}' > /home/shiyanlou/output
然而这样会有重复的路径,去重:
sort /home/shiyanlou/output | uniq >> /home/shiyanlou/output
还可以直接:
sudo grep -r shiyanlou /etc | awk '{FS=":"}!a[$1]++{print $1}' > /home/shiyanlou/output
更直接:
sudo grep -rl shiyanlou /etc >> /home/shiyanlou/output
Linux真有趣....