使用带有条件的ls列出文件,仅使用空格的process/grep文件 [英] List file using ls with a condition and process/grep files that only whitespaces
问题描述
我在一个文件夹中有一个文件列表,其中一些文件的文件名中有空格.
我需要用_替换空格,但首先,我需要列出条件为ls *_[1-4]*[A-c]*
的文件.过滤文件后,某些文件具有空白且没有固定位置(前,中间,结束位置).
ls命令后如何替换空白?
I have a list of files in a folder which some of the files have spaces in the filename.
I need to replace the whitespace with _ but first, i need to list the file with condition ls *_[1-4]*[A-c]*
. After filter the files, some of the files have whitespace with no fixed position(front, middle, end position).
How can i replace the whitespace after ls command?
推荐答案
您不想处理来自ls
.只需循环匹配的文件即可.
You don't want to process the output from ls
. Simply loop over the matching files.
for file in *_[1-4]*[A-c]*; do
# Skip files which do not contain any whitespace
case $file in *\ *) ;; *) continue;; esac
echo mv -n "$file" "${file// /_}"
done
echo
可以作为保障措施;如果输出看起来正确,则将其取出.
The echo
is there as a safeguard; take it out if the output looks correct.
case
和替换查找空格(ASCII 32);如果您还想匹配选项卡,换页等,请进行相应的调整. bash
允许使用$[\t ]
之类的内容来匹配制表符或空格,但这不能移植到其他Bourne Shell实现中
The case
and the substitution looks for a space (ASCII 32); if you also want to match tabs, form feeds, etc, adapt accordingly. bash
allows for something like $[\t ]
to match a tab or space, but this is not portable to other Bourne shell implementations
这篇关于使用带有条件的ls列出文件,仅使用空格的process/grep文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!