linux 删除含特殊字符文件

http://blog.csdn.net/hittata/article/details/8020156

rm 语法

  1. [root@andes.com ~]#rm --help  
  2. Usage: rm [OPTION]... FILE...  
  3. Remove (unlink) the FILE(s).  
  4.   
  5.   -f, --force           ignore nonexistent files, never prompt  
  6.   -i                    prompt before every removal  
  7.   -I                    prompt once before removing more than three files, or  
  8.                           when removing recursively.  Less intrusive than -i,  
  9.                           while still giving protection against most mistakes  
  10.       --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or  
  11.                           always (-i).  Without WHEN, prompt always  
  12.       --one-file-system  when removing a hierarchy recursively, skip any  
  13.                           directory that is on a file system different from  
  14.                           that of the corresponding command line argument  
  15.       --no-preserve-root  do not treat `/' specially  
  16.       --preserve-root   do not remove `/' (default)  
  17.   -r, -R, --recursive   remove directories and their contents recursively  
  18.   -v, --verbose         explain what is being done  
  19.       --help     display this help and exit  
  20.       --version  output version information and exit  

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

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

直接    rm -filename   报错误选项。

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

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

  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
    评论
要在Linux系统中删除大量文件,可以使用命令行工具来完成这个任务。以下是一些常用的命令: 1. rm命令:可以用来删除文件和目录。使用rm命令时需要小心,因为删除文件将不可恢复。 例如,要删除一个名为file.txt的文件,可以输入以下命令: ``` rm file.txt ``` 如果要删除一个名为directory的目录及其所有内容,可以使用以下命令: ``` rm -r directory ``` 在使用rm命令时要注意,该命令没有撤销选项,因此请确保您要删除文件和目录是正确的。 2. find命令:可以用来搜索并删除满足特定条件的文件。例如,以下命令可以删除所有扩展名为.txt的文件: ``` find /path/to/directory -name "*.txt" -type f -delete ``` 这个命令将在指定的目录中查找所有扩展名为.txt的文件,并将它们删除。 请注意,该命令也没有撤销选项,因此请谨慎使用。 3. xargs命令:可以将其他命令的输出作为输入,并将其作为参数传递给另一个命令。这对于删除大量文件非常有用。 例如,以下命令将列出所有扩展名为.txt的文件,并将它们传递给rm命令进行删除: ``` find /path/to/directory -name "*.txt" -type f | xargs rm ``` 这个命令将列出所有扩展名为.txt的文件,并将它们作为参数传递给rm命令进行删除。 注意,在使用xargs命令时,应该小心空格和特殊字符,以确保命令的正确执行。 以上是一些常用的Linux命令,用于删除大量文件。请在使用这些命令时小心,确保您要删除文件和目录是正确的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值