bash globは期待どおりに動作しません(親ディレクトリを引数として使用します)。

bash globは期待どおりに動作しません(親ディレクトリを引数として使用します)。

私のプロジェクトのソースコードを別のディレクトリにコピーしたいのですが、.gitディレクトリをコピーしたくありません(サイズが大きく、私には必要ありません)。以下を試してみました。

cp -r ~/source/!(.git) ~/destination

ただし、ソースの親ディレクトリ(この場合は〜/ home)をターゲットにコピーします。次のコマンドを使用して複製しようとしましたが、ワイルドカードなしで動作しているようです。

user1@user1:~$ mkdir test
user1@user1:~$ mkdir test2
user1@user1:~$ mkdir test/orig
user1@user1:~$ touch test/file1
user1@user1:~$ touch test/orig/file2
user1@user1:~$ ls test
file1  orig
user1@user1:~$ ls test/orig
file2
user1@user1:~$ cp -r test/orig/. test2
user1@user1:~$ ls test2
file2

ただし、ワイルドカードを介して子ディレクトリを省略しようとすると、親ディレクトリもコピーされます。

user1@user1:~$ rm test2/*
user1@user1:~$ ls test2
user1@user1:~$ mkdir test/orig/.sub
user1@user1:~$ cp -r test/orig/!(.sub) test2
cp: will not create hard link 'test2/orig' to directory 'test2/.'
user1@user1:~$ ls test2
file1  file2

拡張式の内容を印刷すると、実際には3つの用語に翻訳され、そのうちの1つが親ディレクトリであることがわかります。

user1@user1:~$ echo test/orig/!(.sub)
test/orig/. test/orig/.. test/orig/file2

これはbashのバグですか、それともglobを正しく使用していませんか?

ベストアンサー1

これが予想される動作かどうかはわかりませんが、GLOBIGNORE変数を使用してこれらのモードを制限すると回避できるようです。

$ echo test/orig/!(.sub)
test/orig/. test/orig/.. test/orig/file2
$ GLOBIGNORE='?(*/)@(.|..)*(/)'; echo test/orig/!(.sub)
test/orig/file2

おすすめ記事