linux 删除含特殊字符文件

rm 语法

[cpp]  view plain copy
  1. [root@TSMClt software]# rm --help
    Usage: rm [OPTION]... FILE...
    Remove (unlink) the FILE(s).


      -f, --force           ignore nonexistent files, never prompt
      -i                    prompt before every removal
      -I                    prompt once before removing more than three files, or
                              when removing recursively.  Less intrusive than -i,
                              while still giving protection against most mistakes
          --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                              always (-i).  Without WHEN, prompt always
          --one-file-system  when removing a hierarchy recursively, skip any
                              directory that is on a file system different from
                              that of the corresponding command line argument
          --no-preserve-root  do not treat `/' specially
          --preserve-root   do not remove `/' (default)
      -r, -R, --recursive   remove directories and their contents recursively
      -v, --verbose         explain what is being done
          --help     display this help and exit
          --version  output version information and exit


    By default, rm does not remove directories.  Use the --recursive (-r or -R)
    option to remove each listed directory, too, along with all of its contents.


    To remove a file whose name starts with a `-', for example `-foo',
    use one of these commands:
      rm -- -foo


      rm ./-foo



    Note that if you use rm to remove a file, it is usually possible to recover
    the contents of that file.  If you want more assurance that the contents are
    truly unrecoverable, consider using shred.


    Report rm bugs to bug-coreutils@gnu.org
    GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
    General help using GNU software: <http://www.gnu.org/gethelp/>
    For complete documentation, run: info coreutils 'rm invocation'
  2.   

特殊文件产生的原因 :误操作,以及一些编码问题导致文件名比较特殊,包含特殊字符,或无法打出的特殊字符,需要删除,可以参照如下几点:

1.文件名以 - 开头  , -filename

直接    rm -filename   报错误选项。

原因,-在rm 中是option 开头字段

解决办法: rm -- -filename     或者  rm ./-filename 

[cpp]  view plain copy
  1. [root@andes.com ~/tmp/dir]#> -ta  
  2. [root@andes.com ~/tmp/dir]#ls  
  3. -ta  
  4. [root@andes.com ~/tmp/dir]#rm -ta  
  5. rm: invalid option -- 't'  
  6. Try `rm ./-ta' to remove the file `-ta'.  
  7. Try `rm --help' for more information.  
  8. [root@andes.com ~/tmp/dir]#rm ./-ta  
  9. rm: remove regular empty file `./-ta'? y  
  10. [root@andes.com ~/tmp/dir]#  

2.文件名以其他特殊字符开头,可以考虑通配符删除

    可以使用通配符   rm *some*  之类进行删除


3.文件名是无法打印的乱码字符

   进入要删除文件目录, rm -i  *   逐个由系统提示是否删除,遇到想删除文件就Y,保留就N,较繁琐,

   如遇到普通正常文件特别多可以考虑将其移动到其他目录:

   find . -name "[a-z|A-Z]*" |xargs -I {} mv {} /somepath       


个人认为第三种方法无敌,没有删不掉的文件,只有不努力的程序员。


4.通过inode节点找到文件删除

ls -i
find -inum XXX  |xargs -I {} rm {}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值