パスで*とタッチが利用できないのはなぜですか? [コピー]

パスで*とタッチが利用できないのはなぜですか? [コピー]

これは次の出力ですtree

[xyz@localhost Semester1]$ tree
.
├── Eng
├── IT
├── IT_workshop
├── LA
├── OS
├── OS_lab
├── Psy
├── Python
└── Python_lab

9 directories, 0 files

すべてのディレクトリで使用したいですtouch

私は次のコマンドを試しました。

[xyz@localhost Semester1]$ touch */{credits,links,notes}

出力は次のとおりです。

touch: cannot touch ‘*/credits’: No such file or directory
touch: cannot touch ‘*/links’: No such file or directory
touch: cannot touch ‘*/notes’: No such file or directory

このコマンドが期待どおりに機能しないのはなぜですか?

ところで、私はCentOS Linux 7を使用しています。

ベストアンサー1

問題は、*/シェルがコマンドを開始する前にglob(glob)を拡張することです。そして、中括弧拡張はグローバル拡張の前に発生します。これは、これらのglobがシェルによって拡張され、ファイルがまだ作成されていないため、globが独自に拡張されることを意味し*/{credits,links,notes}ます'*/credits' '*/links' '*/notes'

何も一致しないすべてのglobに対して同じ動作を見ることができます。たとえば、

$ echo a*j
a*j

一致する場合:

$ touch abj
$ echo a*j
abj

あなたのケースに戻ってファイルが実際には存在しないので、実行するコマンドは次のようになります。

touch '*/credits' '*/links' '*/notes'

次のいずれかを作成すると、状況が変わることがわかります。

$ touch Psy/credits
$ touch */{credits,links,notes}
touch: cannot touch '*/links': No such file or directory
touch: cannot touch '*/notes': No such file or directory

*/creditsglob 、 file と一致するファイルが1つあるので、Psy/creditsこのファイルは機能しますが、残りの2つのファイルはエラーになります。

あなたが試していることを行う正しい方法は次のとおりです。

for d in */; do touch "$d"/{credits,links,notes}; done

結果:

$ tree
.
├── abj
├── Eng
│   ├── credits
│   ├── links
│   └── notes
├── IT
│   ├── credits
│   ├── links
│   └── notes
├── IT_workshop
│   ├── credits
│   ├── links
│   └── notes
├── LA
│   ├── credits
│   ├── links
│   └── notes
├── OS
│   ├── credits
│   ├── links
│   └── notes
├── OS_lab
│   ├── credits
│   ├── links
│   └── notes
├── Psy
│   ├── credits
│   ├── links
│   └── notes
├── Python
│   ├── credits
│   ├── links
│   └── notes
└── Python_lab
    ├── credits
    ├── links
    └── notes

おすすめ記事