9q13nh 发表于 2024-8-22 15:28:11

教你一个骚命令防止你的文件被误删除


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span>好,我是良许。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">文件是<span style="color: black;">咱们</span>在电脑里最<span style="color: black;">宝贵</span>的财富,<span style="color: black;">咱们</span>经常工作了老半天,<span style="color: black;">而后</span>出来的成果就只是一个文件<span style="color: black;">罢了</span>。<span style="color: black;">尤其</span>是程序员,<span style="color: black;">咱们</span>写了半天的代码,结果<span style="color: black;">亦</span><span style="color: black;">便是</span>一个个代码文件<span style="color: black;">罢了</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,<span style="color: black;">咱们</span>都有过<span style="color: black;">这般</span>惨痛的经历——工作了老半天,结果不小心把文件删除了……</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此时</span>候,你的心情是<span style="color: black;">怎么样</span>的?痛苦?自责?无奈?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天良许就给<span style="color: black;">大众</span>介绍一个命令,它将给你的重要文件加一把锁,防止误删除或误修改,为你的宝贝文件保驾护航!</p>chattr命令简介<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>需要<span style="color: black;">运用</span>到一个命令是:chattr ,这个命令在大部分的 Linux 发行版里都有,<span style="color: black;">因此</span><span style="color: black;">针对</span>它的安装就不赘述了。它的基本用法如下:</p>$ chattr 操作符 属性 文件名<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>操作符,有以下三种:</p>+ :给文件<span style="color: black;">增多</span>属性- :去除文件属性= :设置文件的仅有属性<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>,<span style="color: black;">这儿</span>讲的属性不是文件对应的系统属性,而是 chattr 给文件赋予的属性。本文要讲的属性有以下两个:</p>a - <span style="color: black;">准许</span>给文件追加内容i - <span style="color: black;">守护</span>模式(不<span style="color: black;">准许</span>删除或修改)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,它的属性<span style="color: black;">能够</span>设置<span style="color: black;">非常多</span>,有兴趣的小伙伴<span style="color: black;">能够</span>去看下它的 man 手册。</p>$ man chattr防止文件被误删除或修改<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假如<span style="color: black;">咱们</span><span style="color: black;">此刻</span>有个国宝级重要文件 file.txt ,<span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">运用</span> chattr 对它进行<span style="color: black;">守护</span>。在<span style="color: black;">这儿</span>,<span style="color: black;">咱们</span>给文件<span style="color: black;">增多</span> +i 属性:</p>$ sudo chattr +i file.txt<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> lsattr 命令查看它的属性。类似的输出如下:</p>$ lsattr file.txt
    <span style="color: black;">----i---------e---- file.txt</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/2692274d34964ad2b369864f9ef2bafd~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1724866102&amp;x-signature=jznuLYkjC%2FBCyJlQgm9gB9zm79k%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,<span style="color: black;">咱们</span>来尝试一下,手贱去删除那个文件:</p>$ rm file.txt
    rm: cannot <span style="color: black;">remove</span> <span style="color: black;">file.txt</span>: Operation not permitted<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">咦?不<span style="color: black;">准许</span>删除?难道权限<span style="color: black;">不足</span>?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">那好,我 sudo 一下!</p>$ sudo rm file.txt
    rm: cannot <span style="color: black;">remove</span> <span style="color: black;">file.txt</span>: Operation not permitted<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我 X ,居然还是<span style="color: black;">不可</span>删除?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>再来试一下,修改文件的内容。</p><span style="color: black;">$</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">hello world!</span> &gt;&gt; file.txt</span>bash: file.txt: Operation not permitted<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看出来,依然<span style="color: black;">不可</span>对文件进行修改了。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/433f239cc41f4e9b8335ffed503ea170~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1724866102&amp;x-signature=%2FZ2%2FAKEjEynZtzxIIf8WFJwwJrA%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">况且</span>,即使你从 GUI 界面手动去删除这个文件,<span style="color: black;">亦</span>还是<span style="color: black;">不可</span>耐他几何。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/0ce005b39f3642babe9454b9c57f7f3f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1724866102&amp;x-signature=4Fn4POmY6XAFvBhEJimxEOfCJTE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>,<span style="color: black;">能够</span>看出来,<span style="color: black;">此刻</span>这个文件得到了很好的<span style="color: black;">守护</span>,既<span style="color: black;">不可</span>被删除(<span style="color: black;">各样</span><span style="color: black;">办法</span>都不行),<span style="color: black;">亦</span><span style="color: black;">不可</span>被修改。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">那<span style="color: black;">咱们</span>要怎么去除这个<span style="color: black;">守护</span>呢?很简单,只需加上 -i 这个选项。</p>$ sudo chattr -i file.txt<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,文件又恢复原样了,<span style="color: black;">咱们</span>想修改就修改,想删除就删除。</p><span style="color: black;">$</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">Hello World!</span> &gt;&gt; file.txt</span>
    <span style="color: black;">$</span><span style="color: black;"> cat file.txt</span>
    Hello World!
    <span style="color: black;">$</span><span style="color: black;"> rm file.txt</span>防止文件夹被误删除或修改<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面讲到的是<span style="color: black;">守护</span>文件,<span style="color: black;">那样</span>文件夹要<span style="color: black;">怎样</span><span style="color: black;">守护</span>呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实<span style="color: black;">亦</span>是<span style="color: black;">同样</span>,<span style="color: black;">运用</span> +i 这个选项。假如<span style="color: black;">咱们</span><span style="color: black;">此刻</span>有个 dir1 目录,里面有个 file.txt 文件。<span style="color: black;">咱们</span>来对这个文件夹进行<span style="color: black;">守护</span>。</p>$ sudo chattr -R +i dir1<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">这儿</span>,<span style="color: black;">咱们</span><span style="color: black;">运用</span> -R 选项<span style="color: black;">暗示</span><span style="color: black;">能够</span>递归<span style="color: black;">功效</span>到目录里所有的文件(<span style="color: black;">包含</span>子目录)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,我们<span style="color: black;">一样</span>测试一下<span style="color: black;">是不是</span><span style="color: black;">能够</span>被删除或修改。</p>$ rm -rf dir1
    $ sudo rm -rf dir1
    <span style="color: black;">rm:</span> cannot remove <span style="color: black;">dir1/file.txt</span>: Operation <span style="color: black;">not</span> permitted
    $ echo <span style="color: black;">hello world!</span> &gt;&gt; dir1/file.txt
    <span style="color: black;">bash:</span> file.<span style="color: black;">txt:</span> Operation <span style="color: black;">not</span> permitted<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>与文件<span style="color: black;">同样</span>,<span style="color: black;">咱们</span>成功地对文件夹进行了<span style="color: black;">守护</span>。</p>防止文件/目录被删除,但<span style="color: black;">准许</span>追加内容<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">晓得</span>怎么防止文件/目录被误删除或修改了,<span style="color: black;">然则</span>,假如<span style="color: black;">咱们</span>不想要文件已有内容被修改,但<span style="color: black;">准许</span>别人在文件末尾追加内容,要怎么操作?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此时</span>候<span style="color: black;">咱们</span>就需要<span style="color: black;">运用</span> +a 这个选项了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对文件:</p>$ sudo chattr +a file.txt<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对目录:</p>$ sudo chattr -R +a dir1<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,<span style="color: black;">咱们</span>来确认一下,文件<span style="color: black;">是不是</span><span style="color: black;">能够</span>被追加内容。</p><span style="color: black;">$</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">Hello World!</span> &gt;&gt; file.txt</span>
    <span style="color: black;">$</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">Hello World!</span> &gt;&gt; dir1/file.txt</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>再<span style="color: black;">运用</span> cat 命令去查看一下内容:</p><span style="color: black;">$</span> <span style="color: black;">cat file.txt</span>
    <span style="color: black;">Hello</span> <span style="color: black;">World!</span>
    <span style="color: black;">$</span> <span style="color: black;">cat dir1/file.txt</span>
    <span style="color: black;">Hello</span> <span style="color: black;">World!</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看出来,文件都是<span style="color: black;">能够</span>被追加的。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/3511a48ef9ec46e8a6799dc42cddad6b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1724866102&amp;x-signature=SLd2IVDYry%2FtmhQlKy2uskZ5q3k%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,file.txt 还有 dir1/file.txt 依然<span style="color: black;">不可</span>被删除。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你想去掉可追加的属性,<span style="color: black;">能够</span><span style="color: black;">运用</span> -a 这个选项。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对文件:</p>$ sudo chattr -R -a file.txt<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对目录:</p>$ sudo chattr -R -a dir1/<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">近期</span><span style="color: black;">非常多</span>小伙伴找我要<strong style="color: blue;">Linux学习路线图</strong>,于是我<span style="color: black;">按照</span>自己的经验,利用业余时间熬夜肝了<span style="color: black;">一月</span>,整理了一份电子书。无论你是面试还是自我<span style="color: black;">提高</span>,相信都会对你有<span style="color: black;">帮忙</span>!目录如下:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/68ec7544a5c54811b8c2b5b1e090a64a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1724866102&amp;x-signature=%2B7UaqDCA4rkcurdsE57Le2noAS4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">免费送给<span style="color: black;">大众</span>,只求<span style="color: black;">大众</span>金指给我点个赞!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://pan.baidu.com/s/1Wadt2My9gH1zdlkwWL3zTQ?from=timeline</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">期盼</span>有小伙伴能加入我,把这份电子书做得更完美!</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">有收获?<span style="color: black;">期盼</span>老铁们来个三连击,给<span style="color: black;">更加多</span>的人看到这篇<span style="color: black;">文案</span></h3>




7wu1wm0 发表于 2024-10-7 23:36:25

期待与你深入交流,共探知识的无穷魅力。

1fy07h 发表于 2024-10-9 04:14:26

你的见解真是独到,让我受益匪浅。

wrjc1hod 发表于 2024-11-1 23:05:30

我完全同意你的观点,说得太对了。

wrjc1hod 发表于 4 天前

“BS”(鄙视的缩写)‌
页: [1]
查看完整版本: 教你一个骚命令防止你的文件被误删除