首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bash:查找每个名称末尾有尾随空格的目录

Bash:查找每个名称末尾有尾随空格的目录
EN

Stack Overflow用户
提问于 2017-10-13 01:10:05
回答 2查看 1.7K关注 0票数 2

我是Linux的新手(我正在使用Linux ),我需要您帮助我理解基本bash命令。

我将文件存储在我在不同操作系统中使用的外部硬盘驱动器(NTFS格式)上。我的文件被组织在许多目录(文件夹)中,在每个主目录中,我有更多的文件夹,而在这些文件夹中,我有其他文件夹,等等。我需要一个bash命令来查找所有的目录,在每个名称的末尾都有尾随空格的。如果可能的话,我还想使用bash命令删除空格。我试过寻找其他的答案,但我只找到命令,而没有明确解释它们的作用,所以我不确定这是否是我想要的,我不想冒险在不经意间改变一些东西。任何帮助解释使用哪些命令将是非常感谢的!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-13 01:34:49

编写以下内容是为了便于跟踪(作为次要目标),并在拐角情况下进行更正(作为主要目标):

代码语言:javascript
运行
复制
# 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

另请参阅:

  • BashFAQ #1,一般描述while read循环的语法,以及上面的特定修改(IFS=read -r等)的目的。
  • BashFAQ #100,描述如何在bash中执行字符串操作(特别包括${var%suffix}语法,称为“参数扩展”,用于从上面的值中修剪后缀)。
  • 使用查找,介绍了find的使用及其与bash的集成。
票数 5
EN

Stack Overflow用户

发布于 2020-07-21 18:55:34

find . -name "* " -type d

.搜索当前目录和子目录。

-name搜索匹配的文件名(星号表示匹配多个字符,文字空间匹配文件名末尾的空格)

-type d搜索目录类型的项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46721082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档