ディレクトリ内の一部のファイルをコピーしようとしています。このディレクトリには、次のファイルが含まれています。
私の現在のディレクトリは〜/certificate/
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 16 11:58 apache
-rw-rw-r-- 1 ubuntu ubuntu 5812 Oct 16 11:20 apache.keystore
-rw-rw-r-- 1 ubuntu ubuntu 1079 Oct 16 08:31 csr.txt
-rwxr-xr-x 1 ubuntu ubuntu 36626564 Oct 16 10:08 my.war
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 16 09:39 tomcat
-rw-rw-r-- 1 ubuntu ubuntu 6164 Oct 16 09:31 tomcat.keystore
my.warを除くすべてのファイルを〜/certs/にコピーしたいと思います。コマンドに従おうとしましたが、成功しませんでした。一時的にもmy.warをフォルダから移動したくありません。
cp -r ~/certificate/(?!m)* ~/cert/.
適切な正規表現やその他のツールを使用して助けてください。
ベストアンサー1
移植可能なファイル名ワイルドカードパターンにはいくつかの制限があります。 「このファイルを除くすべてのファイル」を表現する方法はありません。
ここに表示されるファイルの場合は、最初の文字~/certificate/[!m]*
(「文字以外の文字で始まるすべてのファイル名m
」)または最後の文字を一致させることができます~/certificate/*[^r]
。
コピーするファイルのリストを微調整する必要がある場合は、サブディレクトリに繰り返されるのを防ぐfind
ことができます。-type d -prune
cd ~/certificates &&
find . -name . -o -type d -prune -o ! -name 'my.war' -name 'other.exception' -exec sh -c 'cp "$@" "$0"' ~/cert {} +
kshを使用している場合は、拡張globモードを使用できます。
cp ~/certificates/!(my.war|other.exception) ~/cert
bashを最初に実行すると、同じコマンドをbashで使用できますshopt -s extglob
。 zshを最初に実行すると、同じコマンドをzshで実行できますsetopt ksh_glob
。 zshには代替構文があります。runsetopt extended_glob
の後に次のいずれかが続きます。
cp ~/certificates/^(my.war|other.exception) ~/cert
cp ~/certificates/*~(my.war|other.exception) ~/cert
または、paxやrsyncなどの除外リストを含むレプリケーションツールを使用します。 Paxは基本的に再帰的です。このオプションを使用して-d
ディレクトリをコピーできますが、内容はコピーできません。
rsync --exclude='my.war' --exclude='other.exception' ~/certificates/ ~/cert/
pax -rw -s '!/my\.war$!!' -s '!/other\.exception$!!' ~/certificates/ ~/cert/