Delete all files from within a directory that contain a specific word

Delete all files from within a directory that contain a specific word

ファイルシステム上の特定のディレクトリに移動し、そのディレクトリ内でファイル名に「sofa_」が含まれるすべてのファイルを削除したいと考えています。

誰かこれをどうやって実行できるか提案してもらえませんか?

答え1

すべてのファイルが同じディレクトリ(サブディレクトリなし)にある場合は、次のコマンドを実行します。

rm *sofa_*

これをサブディレクトリに降ろす必要がある場合は、次のいずれかを使用しますfind

find . -name "*sofa_*" -type f -delete

Or, if you are using bash, enable the globstar option which makes ** match all files and 0 or more subdirectories (making it recursive):

shopt -s globstar

Then:

rm **/*sofa_*

答え2

find . -name "*sofa_*" -type f | xargs rm

Or as correctly noted in the comments:

find . -name "*sofa_*" -type f -delete

関連情報