码迷,mamicode.com
首页 > 其他好文 > 详细

文件名中有空格处理

时间:2015-10-08 14:29:56      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

方法一:设置IFS(the Internal Field Separator),不要用空格做为IFS,选择其他的符号。先来看看man page:

IFS: The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read built-in command. The default value is “<space><tab><new-line>”.

[xx@WW testtttttt]$ for i in *;do echo $i;done
1 .txt
2 .txt

方法二:就是在对文件名进行处理之前,先将空格替换为特殊的自定义符号,然后在处理结束的时候,再替换回来

safename="$(echo name | sed ‘s/ /_-_/g‘)"

original="$(echo $safename | sed s‘/_-_/ /g‘)"

 

方法三:利用find命令。

[xx@WWW testtttttt]$ find . -type f -print0
./2 .txt./1 .txt[op@TIM testtttttt]$
[xx@WWW testtttttt]$ find . -type f -print0 | while read -d $‘\0‘ file;do mv "$file" "$(echo $file |sed ‘s/ /_/g‘)";done

方法四:

ls * | while read file; do
old_file_name=${file}
new_file_name=`echo ${old_file_name} | tr -d ‘ ‘`
mv "${old_file_name}" "${new_file_name}"
done

文件名中有空格处理

标签:

原文地址:http://www.cnblogs.com/timdes/p/4860709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!