当前位置: 首页 > Linux

Linux进阶-查找文件

时间:2023-04-06 02:15:41 Linux

3.17查找文件locate:快速搜索查找指定文件#查看名为restart.sh的文件locaterestart.sh#/usr/local/qcloud/stargate/admin/restart.sh查找指定目录的以下文件以特定字符串开头#例如,在`/root/.ssh`目录中查找所有以“id_rsa”开头的文件locate/root/.ssh/id_rsa#/root/.ssh/id_rsa#/root/。ssh/id_rsa.pub使用正则表达式查找具有指定模式的文件#使用-r选项查找以“sh”结尾的文件或目录locate-rsh$find命令按文件名搜索#我们使用`-name`参数指定文件名find-name"new_file"根据文件大小查找#在/var/log目录下查找大于10m的文件,-10m表示小于10m。find/var/log-size+10M根据文件最近访问时间查找#查找最近7天内访问过的文件find-name"*.txt"-atime-7根据文件类型查找d:dirf:file#查找目录find.-name"new_file"-typedadvancedsearch#搜索结果按照固定格式打印出来#%p:文件名#%u:文件所有者find.-name"*.txt"-printf"%p-%u\n"execcallcommand后面可以跟一个使用exec参数的命令,对找到的每个文件进行操作exec是execute的缩写,意思是执行#如果你想更改一个目录下所有找到的txt文件的访问权限For600findone-n??ame"*.txt"-execchmod600{}\4.1正则表达式和数据操作grep命令:过滤数据grep的功能很简单在文件中查找关键字并显示关键字所在的行。greppath/etc/profile-i:ignoresizeignore-n:显示行号-r:在所有子目录和子文件中搜索,递归递归#在文件夹目录的所有子目录和子文件中搜索文本HelloWorldgrep-r"HelloWorld”文件夹/grep高级用法,与正则表达式一起使用-E:使用正则表达式grep-EPath/etc/profile#查找路径开头的字符grep-E^path/etc/profile#查找路径或路径字符grep-E[Pp]ath/etc/profile#搜索0-4之间的任意数字grep-E[0-4]/etc/profile#搜索a-z或A-Z之间的任意字符grep-E[a-zA-Z]/etc/profilesortforfilesorting#排序后,原文件顺序不变sortname.txt#将排序后的内容写入新文件sort-oname_sorted.txtname.txt#闪回排序sort-rname.txt#randomsortsort-Rname.txt#sortnumberssort-nnumber.txtwc命令:文件统计wc命令貌似是用来统计字数的,其实不止于此。wc命令还可以用来统计行数、字符数、字节数等wcname.txtuniqdeleteduplicatecontentinthefile#去掉重复内容后,将内容写入unique.txt文件uniqrepeat.txtunique。TXT