处理Linux文件的3个技巧
Linux 提供了许多用于查找、计数和重命名文件的命令。这有一些有用的选择。
Linux 提供了多种用于处理文件的命令,这些命令可以节省你的时间,并使你的工作不那么繁琐。
查找文件
当你查找文件时,find
可能会是第一个想到的命令,但是有时精心设计的 ls
命令会更好。想知道你昨天离开办公室回家前调用的脚本么?简单!使用 ls
命令并加上 -ltr
选项。最后一个列出的将是最近创建或更新的文件。
<span class="pln">$ </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">ltr </span><span class="pun">~/</span><span class="pln">bin </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">tail</span><span class="pln"> </span><span class="pun">-</span><span class="lit">3</span>
<span class="pun">-</span><span class="pln">rwx</span><span class="pun">------</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">229</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">19</span><span class="pun">:</span><span class="lit">37</span><span class="pln"> checkCPU</span>
<span class="pun">-</span><span class="pln">rwx</span><span class="pun">------</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">285</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">19</span><span class="pun">:</span><span class="lit">37</span><span class="pln"> ff</span>
<span class="pun">-</span><span class="pln">rwxrw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">1629</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">19</span><span class="pun">:</span><span class="lit">37</span><span class="pln"> test2</span>
像这样的命令将仅列出今天更新的文件:
<span class="pln">$ </span><span class="kwd">ls</span><span class="pln"> </span><span class="pun">-</span><span class="pln">al </span><span class="pun">--</span><span class="kwd">time</span><span class="pun">-</span><span class="pln">style</span><span class="pun">=+%</span><span class="pln">D </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="str">`date +%D`</span>
<span class="pln">drwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">60</span><span class="pln"> shs shs </span><span class="lit">69632</span><span class="pln"> </span><span class="lit">09</span><span class="pun">/</span><span class="lit">23</span><span class="pun">/</span><span class="lit">19</span><span class="pln"> </span><span class="pun">.</span>
<span class="pln">drwxrwxr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">2</span><span class="pln"> shs shs </span><span class="lit">8052736</span><span class="pln"> </span><span class="lit">09</span><span class="pun">/</span><span class="lit">23</span><span class="pun">/</span><span class="lit">19</span><span class="pln"> bin</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">506</span><span class="pln"> </span><span class="lit">09</span><span class="pun">/</span><span class="lit">23</span><span class="pun">/</span><span class="lit">19</span><span class="pln"> stats</span>
如果你要查找的文件可能不在当前目录中,那么 find
将比 ls
提供更好的选项,但它可能会输出比你想要的更多结果。在下面的命令中,我们不搜索以点开头的目录(它们很多一直在更新),指定我们要查找的是文件(即不是目录),并要求仅显示最近一天 (-mtime -1
)更新过的文件。
<span class="pln">$ </span><span class="kwd">find</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">not</span><span class="pln"> </span><span class="pun">-</span><span class="pln">path </span><span class="str">'*/\.*'</span><span class="pln"> </span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span class="pun">-</span><span class="kwd">ls</span>
<span class="pln"> </span><span class="lit">917517</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="pun">-</span><span class="pln">rwxrw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> shs shs </span><span class="lit">683</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">23</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> </span><span class="pun">./</span><span class="pln">newscript</span>
注意 -not
选项反转了 -path
的行为,因此我们不会搜索以点开头的子目录。
如果只想查找最大的文件和目录,那么可以使用类似 du
这样的命令,它会按大小列出当前目录的内容。将输出通过管道传输到 tail
,仅查看最大的几个。
<span class="pln">$ </span><span class="kwd">du</span><span class="pln"> </span><span class="pun">-</span><span class="pln">kx </span><span class="pun">|</span><span class="pln"> egrep </span><span class="pun">-</span><span class="pln">v </span><span class="str">"\./.+/"</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sort</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">tail</span><span class="pln"> </span><span class="pun">-</span><span class="lit">5</span>
<span class="lit">918984</span><span class="pln"> </span><span class="pun">./</span><span class="pln">reports</span>
<span class="lit">1053980</span><span class="pln"> </span><span class="pun">./</span><span class="pln">notes</span>
<span class="lit">1217932</span><span class="pln"> </span><span class="pun">./.</span><span class="pln">cache</span>
<span class="lit">31470204</span><span class="pln"> </span><span class="pun">./</span><span class="pln">photos</span>
<span class="lit">39771212</span><span class="pln"> </span><span class="pun">.</span>
-k
选项让 du
以块列出文件大小,而 x
可防止其遍历其他文件系统上的目录(例如,通过符号链接引用)。事实上,du
会先列出文件大小,这样可以按照大小排序(sort -n
)。
文件计数
使用 find
命令可以很容易地计数任何特定目录中的文件。你只需要记住,find
会递归到子目录中,并将这些子目录中的文件与当前目录中的文件一起计数。在此命令中,我们计数一个特定用户(username
)的家目录中的文件。根据家目录的权限,这可能需要使用 sudo
。请记住,第一个参数是搜索的起点。这里指定的是用户的家目录。
<span class="pln">$ </span><span class="kwd">find</span><span class="pln"> </span><span class="pun">~</span><span class="pln">username </span><span class="pun">-</span><span class="pln">type f </span><span class="lit">2</span><span class="pun">></span><span class="str">/dev/</span><span class="kwd">null</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">wc</span><span class="pln"> </span><span class="pun">-</span><span class="pln">l</span>
<span class="lit">35624</span>
请注意,我们正在将上面 find
命令的错误输出发送到 /dev/null
,以避免搜索类似 ~username/.cache
这类无法搜索并且对它的内容也不感兴趣的文件夹。
必要时,你可以使用 maxdepth 1
选项将 find
限制在单个目录中:
<span class="pln">$ </span><span class="kwd">find</span><span class="pln"> </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">shs </span><span class="pun">-</span><span class="pln">maxdepth </span><span class="lit">1</span><span class="pln"> </span><span class="pun">-</span><span class="pln">type f </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">wc</span><span class="pln"> </span><span class="pun">-</span><span class="pln">l</span>
<span class="lit">387</span>
重命名文件
使用 mv
命令可以很容易地重命名文件,但是有时你会想重命名大量文件,并且不想花费大量时间。例如,要将你在当前目录的文件名中找到的所有空格更改为下划线,你可以使用如下命令:
<span class="pln">$ </span><span class="kwd">rename</span><span class="pln"> </span><span class="str">'s/ /_/g'</span><span class="pln"> </span><span class="pun">*</span>
如你怀疑的那样,此命令中的 g
表示“全局”。这意味着该命令会将文件名中的所有空格更改为下划线,而不仅仅是第一个。
要从文本文件中删除 .txt 扩展名,可以使用如下命令: