Boşluk içeren bir dizinin konumunu değiştirme

0

Soru

İki boru hattı yaptım; bu, x="$(ls -1p | grep "/$" | tr -d "/")" tüm alt dizinleri çalışma dizininden alın ve bu, y="$(ls -1p | grep "/$"| grep \ | tr -d "/")" çalışma dizinindeki boşlukları içeren alt dizinleri alır.

Şimdi yapmaya çalıştığım şey, boşluk içeren dizinin konumunu değiştirmek ve en üste koymaktır, yani. aşağıda benim alt dirseklerim olduğunu söyleyin:

Dir1
Dir2
Dir 3

Şimdi Dir 3 zirveye çıkıyor:

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

Yukarıda bu görevi yapmak için döngüm var. Boşluk içermeyen tüm alt dir'leri yazdırır, ancak şu şekilde yazdırır:

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

Birisi yardım edebilirse, bu çok takdir edilecektir.

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

En iyi cevabı

0

Eğer istersen for döngü için find komuta, ne dersin:

#!/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

Sağlanan örnekle çıktı:

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

GNU bul'u kullanma:

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

Bu özyinelemeli.

2021-11-24 01:57:20

Diğer dillerde

Bu sayfa diğer dillerde

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