私のディレクトリには、数ヶ月に生成されたファイルがたくさんあります。特定の月に生成されたファイルをフィルタリングし、tar機能を使用してこれらのファイルをアーカイブしたいと思います。これまで試したコードは次のとおりです。
months="Dec-2021"
decfiles=find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"
echo $decfiles
上記のコードでは、次のエラーが発生します。
files.sh: 3: .: Illegal option -n
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
私はこれを見つけることができないようで、私も初めてbashに触れました。この問題の解決にご協力いただきありがとうございます。
ベストアンサー1
あなたはこれに慣れていないので、私はあなたに答えを提供するだけでなく、この種の問題を解決し始める方法を説明しようとします。
まず、これらの問題が見つかった場合は、それをより小さなコマンドに分割して分離してみてください。
たとえば、次のように結果をファイルに直接保存しようとしないでください。
decfiles=find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"
コマンドを単独で実行してみてくださいfind
。
find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"
まもなく次の内容をご確認いただけます。
ミス#1
find: I cannot figure out how to interpret ‘01-Dec-2021 +1 month-1 sec | tar cz decemberfiles.tar.gz’ as a date or time
実際に読むと、引用符の中でパイプを実行していることがわかります。
"01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"
試み #2
これで、findコマンドが次のようになることを確認する必要があります。
find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec"
これで、起動したファイルとディレクトリのリストが表示されます。ただ印刷することをお勧めします。文書、ディレクトリの代わりに-type f
。
find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f
今、私たちはどこかに行きます。
次に、次のようなことを試したいとします。
find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |tar cz decemberfiles.tar.gz
これにより、次のようになります。
ミス#2
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
これはオプションが欠落していることを意味-f
するので、マニュアルページを見て、アーカイブを提供するために使用した名前をtar
確認してください。-f ARCHIVE
ファイル名を指定しましたが、アーカイブラとは言っていません。それでは、次のことを試してみてください。
試み #3
find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |tar czf decemberfiles.tar.gz
間違い #3
これで新しいエラーが発生します。
tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.
空のアーカイブを作成しようとしていると言うのはなぜですか?マニュアルを見直すと、tar
以下のようにファイルのリストを提供する必要があることがわかります。議論:
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
tarコマンドのSTDINにファイルをパイプしようとしています。したがって、次のようにする必要があります。
tar czf decemberfiles.tar.gz <file1> <file2> <file3> ...
このコマンドはファイルリストを作成していますfind
。tar
このファイルをコマンドにどのように提供できますか?いくつかのオプションは説明せずに直接読んでみましょう。
オプション1
tar czf decemberfiles.tar.gz $(find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f)
オプション#2
tar czf decemberfiles.tar.gz `find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f`
ヒント:実行していることをよりよく理解するには、コマンドの前にechoを追加してどのように見えるかを確認してください(ここでは出力を書かないので、自分で探してみましょう)。
echo tar czf decemberfiles.tar.gz `find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f`
オプション#3
コマンドに慣れるための最後のオプションは次のとおりですxargs
。
find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |xargs tar czf decemberfiles.tar.gz
これで、少なくとも保存した内容が必要です。今頃ならまだ学ぶことが多いことがわかります。
部品のように克服すべきいくつかの問題がまだ残っていますdecfiles=...
。小さなコマンドを再実行して内容を確認し、コマンド出力を bash の変数に割り当てる方法を検索してみてください。 Googleで検索できるので、ここでは説明しません。しかし、今ではbash学習を始め、障害物を段階的に克服する方法についてより良いアイデアを持っていたことを願っています。