[英]How to remove leading or trailing spaces in files, directories, and subdirectories using bash script?
On mac OS X, how to get a bash script working to change over a 1,000 files and sub-directories and many sub-sub-directories in the current working directory. 在Mac OS X上,如何使bash脚本工作以更改当前工作目录中的1,000个文件和子目录以及许多子目录。
Tried using a script from Find and replace filename recursively in a directory answer, however that doesn't take into account filenames or directories that have leading or trailing spaces, as the spaces need to be escaped. 在目录答案中使用“ 查找并递归替换文件名”中的脚本进行了尝试,但是该文件未考虑文件名或具有前导或尾随空格的目录,因为需要转义空格。 However as you can see the below line the path is also escaped and the script doesn't work.
但是,您可以看到下面的行,路径也被转义,脚本不起作用。
find . -name '* ' -type d -exec bash -c 'echo mv "$1"/ \\""${1/ /}\\""' -- {} \\;
(Note: Yes I'm using echo to see the commands.) (注意:是的,我正在使用echo来查看命令。)
The bash script needs to trim leading and trailing spaces of both files and directories. bash脚本需要修剪文件和目录的前导和尾随空格。 By "trim" I mean remove leading and trailing spaces and leaving the spaces in between words alone.
“修饰”是指删除前导和尾随空格,并保留单词之间的空格。
Example Structure: 示例结构:
./
./ Leading Space/
./ Leading Space/ Text File With Leading Space.txt
./Trailing Space /
./Trailing Space / Multiple Trailing and Leading Spaces /
./Trailing Space / Multiple Trailing and Leading Spaces / Trailing and Leading Spaces Text File .txt
Note: The answer will need to work on any default mac OS X. I will not accept an answer that uses third party applications or plugins, including Mac Automator and perl rename
. 注意:答案将需要在任何默认的Mac OS X上运行。我不接受使用第三方应用程序或插件(包括Mac Automator和perl
rename
的答案。
To remove trailing blanks from both directories and files: 要从目录和文件中删除尾随空白:
find . -depth -name '* ' -execdir bash -c 'mv "$1" "${1%"${1##*[^[:space:]]}"}"' Move {} \;
To remove leading whitespace: 删除前导空格:
find . -depth -name ' *' -execdir bash -c 'f=${1#./}; mv "./$f" "./${f#"${f%%[![:space:]]*}"}"' Move {} \;
Notes: 笔记:
So that directories are not renamed while find is searching inside them, we need to specify -depth
. 为了使目录在内部搜索时不会重命名,我们需要指定
-depth
。
The shell expansion ${1%"${1##*[^[:space:]]}"}
removes trailing white space while ${f#"${f%%[![:space:]]*}"}
removes leading white space. 外壳扩展
${1%"${1##*[^[:space:]]}"}
删除尾随空白,而${f#"${f%%[![:space:]]*}"}
删除前导空格。
The first non-option argument to bash -c
is assigned to $0
and is used as the program name in error messages should any errors occur. bash -c
的第一个非选项参数分配给$0
并在发生任何错误时在错误消息中用作程序名。 It is good practice to use a descriptive name here, like Move
as shown above. 在此处使用描述性名称是一个好习惯,例如上图所示的“
Move
。
The above was tested under Linux. 以上已在Linux下进行了测试。
You can use a simple for loop and replace the space by an underline with something like this: 您可以使用简单的for循环,并用下划线将空格替换为以下内容:
for FILE in *\ *
do
mv "$FILE" "${FILE// /_}"
done
For multiple sub-directories, you can use something like this: 对于多个子目录,您可以使用类似以下的内容:
for FILE in ./* */* */* *; do mv "$FILE" "${FILE// /_}"; done 2>/dev/null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.