【问题标题】:List all directories with spaces in their name列出名称中带有空格的所有目录
【发布时间】:2012-03-07 06:00:42
【问题描述】:

我想使用 shell 脚本列出所有目录。我正在使用以下代码:

DIR="$1"

if [ $# -ne 1 ]
then
    echo "Usage: $0 {dir-name}"
exit 1
fi

cd "$DIR"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for user in $( ls -d */)
do
  for dirs in $( ls -d $PWD/$user*)
  do
    echo $PWD/$user/$dirs;
  done
done
IFS=$SAVEIFS

如果该目录上没有任何空格,它对我有用,否则它将为每个空格拆分输出。我得到以下输出:

abhinaba@abhinaba-desktop:~/software$ sh test.sh /media/2C44138344134F48/RB1
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3001/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3002/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3003/*: No such file or directory

【问题讨论】:

  • 你能否更准确地回答这个问题:你只需要上面有空格的目录吗?或者您是否需要在所有目录上“做你的事情”,包括名称中带有空格的目录?
  • 您需要使用 BASH "bash test.sh /media/2C44138344134F48/RB1" 运行脚本
  • 我需要列出所有目录,无论是否有空格。
  • 我明白,但标题中的问题不清楚。我已经更新了我的答案。通过“sh test.sh ...”运行我遇到了同样的错误

标签: linux shell unix sh


【解决方案1】:

已编辑:删除 ls 以优化脚本(来自 tripleee

如果你使用 BASH shell:

#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
DIR="$1"

if [ $# -ne 1 ]
 then
 echo "Usage: $0 {dir-name}"
 exit 1
fi

cd "$DIR"

for user in $(ls -d */)
 do
 for dirs in $(ls -d $user*)
  do
  echo $dirs
 done
done

IFS=$SAVEIFS

像这样运行上面的“test.sh”脚本:

bash test.sh /media/2C44138344134F48/RB1

或者简单地说(如果你已经在 BASH 中并且你已经设置了 eXecutable 标志)

./test.sh /media/2C44138344134F48/RB1

Reference

【讨论】:

  • 奇怪。它适用于我的 Ubuntu,使用 BASH shell。几分钟前我错过了脚本的最后一行(设置回原始 IFS 的行。也许您需要在新的 shell 中尝试(只需输入“bash”)
  • 我也在使用 ubuntu 11.10 并在 bash shell 上工作,但我仍然收到错误。
  • ls 在反引号中驱动for 循环基本上总是错误的。 partmaps.org/era/unix/award.html#ls
  • 通配符修复后,我认为您无需再与IFS 混淆。
  • 请注意上述代码中的其他字符。诸如包含“../”类型文本的文件之类的东西仍然会抛出一个循环。
【解决方案2】:

实际上使用 find 是最简单的:

find PATH -type d -name '* *'

或者,如果您需要对每个结果进行处理,请考虑将其通过管道传输到 xargs

find PATH -type d -name '* *' -print0 | xargs -0 run-some-command

或者,如果您只想将所有目录安全地转义为参数,那么:

find PATH -type d -print0 | xargs -0 run-some-command

run-some-command 中,脚本的每个参数都将正确设置为每个目录名称,而不管它包含什么字符。

【讨论】:

  • 同意,可能OP问题与帖子内容不符。我认为他想处理一个目录中的所有目录,但他的脚本失败了,因为名称中有空格。
  • 是的,我需要处理其中的所有目录。
【解决方案3】:

下面的简单脚本将满足您的需求。

#!/bin/bash

function usage() {
        echo "Usage: `basename $0` <dir-name>"
}


if [ "x$1" = "x" ]
then
        usage
        exit 0
fi

find "$1" -type 'd'

【讨论】:

    猜你喜欢
    • 2019-03-19
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2019-04-10
    • 1970-01-01
    • 2016-02-27
    • 2013-08-21
    相关资源
    最近更新 更多