如何更换位置的一个目录,包含空间

0

的问题

我做了两个管道; x="$(ls -1p | grep "/$" | tr -d "/")" 得到所有子目录的工作目录,而这一点, y="$(ls -1p | grep "/$"| grep \ | tr -d "/")" 获取的子目录,包含空间中的工作目录。

所以现在我已经试图要做的就是换位置的目录,包含空间,并把它放在最高层,即, 说下是我的子目录:

Dir1
Dir2
Dir 3

现在 Dir 3 去顶:

Dir 3
Dir1
Dir1
for I in $x; do
    for X in $y; do
        if [[ $I == $X ]];then
            sed "/"$X"/d" "$I"
        fi
    done
    echo "$I"
done

以上是我的循环要做到这一任务。 它打印的所有子目录,含有没有空但是印它为:

Dir1
Dir2
sed: Dir: No such file or directory
Dir
sed: 3: No such file or directory
3

如果有人能帮帮忙,那将是极大的赞赏。

bash
2021-11-24 00:05:11
2

最好的答案

0

如果你喜欢 for 循环到 find 命令,如关于:

#!/bin/bash

# 1st loop to print the dirnames containing space character
for d in */; do                         # loops over subdirectories under current directory
    if [[ $d =~ [[:space:]] ]]; then    # if the dirname contains a space character
        echo "${d%/}"                   # then print the name removing the trailing slash
    fi
done

# 2nd loop to print the dirnames without space character
for d in */; do
    if [[ ! $d =~ [[:space:]] ]]; then  # if the dirname does not contain a space character
        echo "${d%/}"
    fi
done

输出与所提供的例子:

Dir 3
Dir1
Dir2
2021-11-24 01:45:53
0

使用的GNU发现:

find . -mindepth 1 -type d -name '*[[:space:]]*'       # spaces
find . -mindepth 1 -type d -regex '.*/[^/[:space:]]+$' # no spaces

这是递归的。

2021-11-24 01:57:20

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................