我是Linux的新手(我正在使用Linux ),我需要您帮助我理解基本bash命令。
我将文件存储在我在不同操作系统中使用的外部硬盘驱动器(NTFS格式)上。我的文件被组织在许多目录(文件夹)中,在每个主目录中,我有更多的文件夹,而在这些文件夹中,我有其他文件夹,等等。我需要一个bash命令来查找所有的目录,在每个名称的末尾都有尾随空格的。如果可能的话,我还想使用bash命令删除空格。我试过寻找其他的答案,但我只找到命令,而没有明确解释它们的作用,所以我不确定这是否是我想要的,我不想冒险在不经意间改变一些东西。任何帮助解释使用哪些命令将是非常感谢的!
发布于 2017-10-13 01:34:49
编写以下内容是为了便于跟踪(作为次要目标),并在拐角情况下进行更正(作为主要目标):
# because "find"'s usage is dense, we're defining that command in an array, so each
# ...element of that array can have its usage described.
find_cmd=(
find # run the tool 'find'
. # searching from the current directory
-depth # depth-first traversal so we don't invalidate our own renames
-type d # including only directories in results
-name '*[[:space:]]' # and filtering *those* for ones that end in spaces
-print0 # ...delimiting output with NUL characters
)
shopt -s extglob # turn on extended glob syntax
while IFS= read -r -d '' source_name; do # read NUL-separated values to source_name
dest_name=${source_name%%+([[:space:]])} # trim trailing whitespace from name
mv -- "$source_name" "$dest_name" # rename source_name to dest_name
done < <("${find_cmd[@]}") # w/ input from the find command defined above
另请参阅:
while read
循环的语法,以及上面的特定修改(IFS=
、read -r
等)的目的。${var%suffix}
语法,称为“参数扩展”,用于从上面的值中修剪后缀)。find
的使用及其与bash的集成。发布于 2020-07-21 18:55:34
find . -name "* " -type d
.
搜索当前目录和子目录。
-name
搜索匹配的文件名(星号表示匹配多个字符,文字空间匹配文件名末尾的空格)
-type d
搜索目录类型的项。
https://stackoverflow.com/questions/46721082
复制相似问题