Linux如何查找处理文件名后包含空格的文件

 

当Linux下文件名中出现空格这类特殊情况话,如何查找或确认那些文件名后有空格呢? 又怎么批量替换处理掉这些空格呢?

方法1: 输入文件名后使用Tab键,如果使用Tab键后面出现\ \ \这样的可见字符,那么该文件名包含空格。当然,这个方法弊端很大,例如,效率低下,不能批量查找,只有当你怀疑某个文件名后有空格,这个方法才比较凑效。另外,不能查找文件中间包含空格的文件名。如下测试所示:

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. it is only for test!

  1.  

  1. [1]+  Stopped                 cat > "test.txt    "

  1. [root@DB-Server kerry]# cat >"tes t.txt"

  1. it is only for test too!

  1.  

  1. [2]+  Stopped                 cat > "tes t.txt"

  1. [root@DB-Server kerry]# ls test.txt

  1. ls: test.txt: No such file or directory

  1. [root@DB-Server kerry]# ls test

  1. test~         test1.py      test.py       test.sh       test.txt     

  1. [root@DB-Server kerry]# ls test.txt\ \ \ \ 

  1. test.txt   

  1. [root@DB-Server kerry]# ls tes

  1. test~         test1.py      test.py       test.sh       tes t.txt     test.txt  

方法2: 使用find命令查找文件名中包含空格的文件。

[root@DB-Server kerry]# find . -type f -name "* *" -print

./test.txt

./tes t.txt

那么如何将这些空格替换掉呢?  下面脚本可以替换文件中间的空格,用下划线替换空格,但是只能替换文件中间的空格,并不能替换文件名后面的空格。如下测试所示:

  1. find . -type f -name "* *" -print |

  1. while read name; do

  1. na=$(echo $name | tr ' ' '_')

  1. if [[ $name != $na ]]; then

  1. mv "$name" "$na"

  1. fi

  1. done

上面脚本只能将文件名中间有空格的替换为下划线。那么如何解决文件名后有空格的情况呢? 可以用其它shell脚本实现,如下所示:

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 12

  1. [root@DB-Server kerry]# cat >"tes t.txt"

  1. 12

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. ./test.txt   

  1. ./tes t.txt

  1. [root@DB-Server kerry]# for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. [root@DB-Server kerry]# ls -lrt

  1. total 8

  1. -rw-r--r-- 1 root root 0 Nov 13 10:04 test.txt

  1. -rw-r--r-- 1 root root 0 Nov 13 10:04 tes_t.txt

如上所示,虽然文件名中间的空格被替换为了下划线,但是后面的空格没有替换为下划线,而是将那些空格直接截断了。Why?下面使用sed命令也是如此

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 12

  1. [root@DB-Server kerry]# cat >"tes t.txt"

  1. 12

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. ./test.txt   

  1. ./tes t.txt

  1. [root@DB-Server kerry]# for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. [root@DB-Server kerry]# ls -lrt

  1. total 8

  1. -rw-r--r-- 1 root root 0 Nov 13 09:29 test.txt

  1. -rw-r--r-- 1 root root 0 Nov 13 09:29 tes_t.txt

  1. [root@DB-Server kerry]#

  1. [root@DB-Server kerry]#

其实,这个是因为读取文件名是$file 与"$file"是不同的,$file不会识别文件名后面的空格,而"$file"才会失败文件名后面的空格。所以上面脚本其实只是取巧而已。

  1. [root@DB-Server kerry]# rm -rf *;

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 123

  1. [root@DB-Server kerry]#  for file in *; do echo "$file"; echo "$file" | wc -m ;   done;

  1. test.txt   

  1. 13

  1. [root@DB-Server kerry]#  for file in *; do echo $file; echo $file | wc -m ;   done;

  1. test.txt

  1. 9

  1. [root@DB-Server kerry]#

所以,正确的替换空格的命令应该为如下:

方案1:

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 123456

  1.  

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. ./test.txt   

  1. [root@DB-Server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '\n'` ; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. [root@DB-Server kerry]# ls test.txt

  1. test.txt

  1. [root@DB-Server kerry]#

方案2:

  1. [root@DB-Server kerry]#

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 123456

  1.  

  1. [root@DB-Server kerry]# for file in *' '*; do   mv "$file" `echo "$file" | sed -e 's/ /n/g'`; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

但是对于文件名中间包含空格的情况,上面两个脚本都无法完美解决。如下所示:

  1. [root@DB-Server kerry]#

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"tes t.txt"

  1. 123456

  1.  

  1. [root@DB-Server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '_'` ; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. [root@DB-Server kerry]# ls -lrt

  1. total 8

  1. -rw-r--r-- 1 root root 7 Nov 13 16:00 tes_t.txt

  1. [root@DB-Server kerry]#

  1.  

  1.  

  1. [root@DB-Server kerry]# rm -rf *

  1. [root@DB-Server kerry]# cat >"tes t.txt"

  1. 123456

  1. [root@DB-Server kerry]# cat >"test.txt    "

  1. 654321

  1.  

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. ./test.txt   

  1. ./tes t.txt

  1. [root@DB-Server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '_'` ; done

  1. [root@DB-Server kerry]# find . -type f -name "* *" -print

  1. [root@DB-Server kerry]# ls -lrt

  1. total 12

  1. -rw-r--r-- 1 root root 0 Nov 13 15:59 tes_t.txt

  1. -rw-r--r-- 1 root root 7 Nov 13 15:59 test.txt____

当然对于这两种特殊情况,上面脚本都不能一起处理,如上所示,后面的空格会被替换成了下划线。这反而不是我们想要的,反而最上面的那两种脚本,可以误打误撞的解决这两种问题。当然让前提是你得知其然知其所以然!

 

参考资料:

http://www.eygle.com/digest/2006/11/linux_replace_blank.html

https://stackoverflow.com/questions/1806868/linux-replacing-spaces-in-the-file-names

https://www.keakon.net/2011/10/20/bash%E4%B8%8B%E5%A4%84%E7%90%86%E5%8C%85%E5%90%AB%E7%A9%BA%E6%A0%BC%E7%9A%84%E6%96%87%E4%BB%B6%E5%90%8D

Linux如何查找处理文件名后包含空格的文件的更多相关文章

  1. Shell 处理文件名中包含空格的文件

    最近在学Gradle, 使用git clone 命令下载了一些资料,但是文件名含有空格,看上去不是很舒服,因此想到用shell脚本对其进行批处理,去掉文件名中的空格,注意这里是把所有的空格全去掉 gi ...

  2. linux下rm命令删除文件名中包含特殊字符的文件【转】

    转自:http://blog.itpub.net/143526/viewspace-1060083/ 1. 删除带“-”的文件名的方法 2. 删除包含其它特殊字符的文件 3. 删除系统打不出的乱码文件 ...

  3. Linux下怎么创建和进入带有空格的文件夹

    有时候需要创建带有空格的文件夹,虽然这不是一个好的习惯,但是偶尔会遇到.用的最多的是很多时候需要进入带有空格的文件夹,如"a b"是一个文件夹名. 创建:mkdir "a ...

  4. linux下查找某个目录下包含某个字符串的文件

    有时候要找一些字符串,但是又不知道在哪个文件,只记得一些字符串 那么如何在linux下寻找包含某段文字的文件呢? 强大的find命令可以帮你完成不可能的任务. 比如我只记得我的程序里包含唯一的字符串“ ...

  5. 一次因为文件名开头包含空格而导致FTP文件一直无法下载的悲剧!

    最近负责公司研究新的多渠道打包方案,之前的打包方案太慢了,因此采用了美团的Android Signature V2 Scheme签名下的新一代渠道包打包神器 方案进行了多渠道打包.但是由于马虎,在配置 ...

  6. Linux查看当前目录下文件名中包含指定字符的文件

    find . -type f -name "edaijia* 结果:

  7. Linux如何查找某个时间点后生成的空文件

    今天遇到一个特殊需求,需要找到某天(例如2017-04-13)以及这之后生成的空文件.那么这个要怎么处理呢?这个当然是用find命令来解决.如下所示, -mtime -5 表示查找距现在 5*24H ...

  8. Linux 下如何处理包含空格和特殊字符的文件名

    Linux 下如何处理包含空格和特殊字符的文件名 作者: Avishek Kumar 译者: LCTT zpl1025 | 2015-07-08 07:47   评论: 12 收藏: 9 分享: 1 ...

  9. Linux find 查找 并删除文件 杀掉进程

    find 默认在当前 即 . 目录下查找 du 文件名 / 目录 # 查看文件占用内存大小 1. 按照文件名查找 find / -name qwe # qwe为文件名 find / -name *qw ...

随机推荐

  1. awk的sub函数和gsub函数的用法

    1. sub函数 [root@nhserver1 10]# echo "a b c 2011-11-22 a:d" | awk 'sub(/-/,"",$4)' ...

  2. 101490E Charles in Charge

    题目连接 http://codeforces.com/gym/101490 题目大意 你有一张图,每两点之间有一定距离,计算出比最短路大x%之内的路径中最长边的最小值 分析 先跑一遍最短路,然后二分答 ...

  3. Sql Server的艺术(二) SQL复杂条件搜索

    本次讲到where字句中经常用到的集中较为复杂的搜索条件,包括组合的查询条件.IN运算符.NOT运算符.LIKE运算符和相关通配符. 学习本节需要用到一下两张表: CREATE TABLE TEACH ...

  4. [ASP.NET][Session] 使用 SQLServer 会话管理解决 Session 丢失问题

    使用 SQLServer 会话管理解决 Session 丢失问题 步骤 1.通过命令行执行 aspnet_regsql.exe 程序(不要双击安装),先在 CMD 中输入命令 cd C:\Window ...

  5. zookeeper 实现分布式锁

    主要是依赖临时节点的特性.数据存储到内存中效率高:例如有web1 web2 两台应用服务器 db1 db2两台db服务器  db互为主备,web1 web2 分别去修改db1 .有限db2库里张三的年 ...

  6. JS前端验证代码

    手机号码正则表达式验证: function checkPhone(){ var phone = document.getElementById('phone').value; if(!(/^1[345 ...

  7. 使用guava变形数据结构

    在java日常开发中,经常需要使用各种数据结构,在涉及到数据结构之间如何优雅的转换时,我们可以借助google的guava提供的相关功能来优雅的实现.以下记录一些开发中经常需要使用数据结构的变形,以便 ...

  8. POJ 3182 The Grove [DP(spfa) 射线法]

    题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...

  9. 【转】如何解决plsql查询oracle数据库语句where条件带有中文无法匹配结果

    一.问题描述 之前使用PLSQL查询oracle数据库可以正常查询统计结果,由于换了个电脑,重新安装之后,同样的sql查询语句同一个数据库,无法正常查询结果,如下图所示 二.解决办法 1. 查询数据当 ...

  10. CSA单点登录环境配置

    本篇先写一些基础 今天看到一个cas单点登录的源码,搞环境就废了大半时间 <SSO CAS单点系列>http://www.imooc.com/article/3576 参考了这篇博客里的配 ...