内容:
1、建立带空格文件
2、删除文件名中空格
一、
建立带空格文件:
- [admin@localhost empty]$ mkdir "aaa bbb . ccc __ ddd " "mm nn ll oo"
- [admin@localhost empty]$ touch "cc .t x t .b a k" "uu.l _ .c um "
- [admin@localhost empty]$ ll
- total 12
- drwxrwxr-x 2 admin admin 4096 2012-02-22 16:30 aaa bbb . ccc __ ddd
- -rw-rw-r-- 1 admin admin 0 2012-02-22 16:30 cc .t x t .b a k
- drwxrwxr-x 2 admin admin 4096 2012-02-22 16:30 mm nn ll oo
- -rw-rw-r-- 1 admin admin 0 2012-02-22 16:30 uu.l _ .c um
- [admin@localhost empty]$
二、
脚本文件:
[admin@localhost empty]$ touch delblank.sh
- -rw-rw-r-- 1 admin admin 0 2012-02-22 16:30 uu.l _ .c um
- [admin@localhost empty]$ cat delblank.sh
- #for loop in `ls -Al`
- #echo `ls -Al | awk '{print $0}'`
- #`ls -A` | while read loop ;do tmp=`echo $loop | sed 's/ //g'`;mv "$loop" "$tmp";done
- #for loop in `ls -A`
- for loop in *
- do
- #tmp=`echo $loop | sed 's/ //g'`
- #tmp=`echo $loop | tr -d " "`
- tmp=`echo ${loop// /}`
- if [ "$loop" != "$tmp" ]
- then
- mv "$loop" "$tmp"
- else
- :
- #echo "$loop has no blank"
- fi
- done
- [admin@localhost empty]$
更改权限:
- [admin@localhost zushell]$ chmod u+x
delblank.sh
[admin@localhost empty]$ ll delblank.sh
-rwxrwxr-x 1 admin admin 417 2012-02-22 16:28 delblank.sh
[admin@localhost empty]$
执行删除空格操作:
- [admin@localhost empty]$ ./delblank.sh
- [admin@localhost empty]$ ll
- total 12
- drwxrwxr-x 2 admin admin 4096 2012-02-22 16:30 aaabbb.ccc__ddd
- -rw-rw-r-- 1 admin admin 0 2012-02-22 16:30 cc.txt.bak
- -rwxrwxr-x 1 admin admin 417 2012-02-22 16:28 delblank.sh
- drwxrwxr-x 2 admin admin 4096 2012-02-22 16:30 mmnnlloo
- -rw-rw-r-- 1 admin admin 0 2012-02-22 16:30 uu.l_.cum
- [admin@localhost empty]$
另外,本来在shell下通过ls | grep " " |while read line ;do tmp=`echo $line | sed 's/ //g'`;mv "$line" "$tmp";done进行删除,但总是出现如下错误:
[admin@localhost aa]$ mkdir " aa ss"
[admin@localhost aa]$ ll
total 4
drwxrwxr-x 2 admin admin 4096 2012-02-22 16:39 aa ss
[admin@localhost aa]$ ls | grep " " |while read line ;do tmp=`echo $line | sed 's/ //g'`;mv "$line" "$tmp";done
mv: cannot stat `\033[00m\033[01;34m aa ss\033[00m': No such file or directory
[admin@localhost aa]$
刚刚接触shell几天时间,现在还不知道如何解决。各位高手知道的话请提供下。
阅读(1563) | 评论(0) | 转发(0) |