スキップしてメイン コンテンツに移動

投稿

ラベル(command tips)が付いた投稿を表示しています

前日の日付が入ったファイルをLinuxコマンドで処理

ログファイルのバックアップなどで、日付の入ったファイルを扱うことも多いと思います。 dateコマンドを使って、前日の日付がファイル名に入ったファイルだけを処理する簡単なLinuxコマンドを紹介します。 下記のコマンドでは、dateコマンドにyesterdayと日付フォーマットを引数として渡し、当日が2021年2月25日の時に20210224.logのような書式のログファイルを、dir1からdir2へコピーすることができます。 #!/bin/bash cp -p /dir/*'date -d 'yesterday' +%Y%m%d'.log /dir2/

Linuxコマンドの組み合わせだけで、前日の日付がが入ったファイルのみgzip圧縮する方法

ログファイルなどでは日ごとにファイルを生成し、日付がファイル名に入っている場合があると思います。 前日までの日付のファイルをすべてgzipで圧縮するLinuxコマンドを紹介します。 注: コマンドの組み合わせは、もっと少なくできる方法もあるかもしれませんが、今回紹介したコマンドの組み合わせでも、たたき台にはなるかと思いますので、参考にしてみてください。 # /dir/ディレクトリはいかにある、拡張子がlogのファイルでかたファイル名に含まれる日付がコマンド実行時の前日のものをすべてgzip圧縮するコマンドの組み合わせ。 find /dir/ -type f -name "*.log" | sed 's/.*_\([0-9]*\)\.log/\1/` | awk -v date="$(date +%Y%m%d)" '{if ($0 < date) print $0}' | xargs -I {} find /dir/ -name "*{}.log" | xargs --no-run-if-empty gzip 簡単に解説です。 find /dir/ -type f -name "*.log" :dirディレクトリ配下で拡張子がlogのファイルをすべてリストアップ sed 's/.*_\([0-9]*\)\.log/\1/` :ファイル名から日付部分のみを抜き出し awk -v date="$(date +%Y%m%d)" '{if ($0 < date) print $0}' :コマンド実行時の前日の日付のみ抜き出し。ちなみにawkのvオプションで変数dateを定義しています。 xargs -I {} find /dir/ -name "*{}.log" :前日以前の日付が含まれる拡張子がlogのファイルをすべて取得。ちなみにxargsの -Iオプションの{}の部分で日付部分をfindコマンドに渡しています。 xargs --no-run-if-empty gzip :ファイルがなかったら実行しないというxargsのオプション

File Clean Up and Archiving Linux Command Tips

If you would like to delete files whose name ends with "batch.log" older than 30 days, Use the following command. find /var/log/ -name "*batch.log" -mtime +30 -type -f -delete If you would like to archive files whose name ends with "batch.log" older than 30 days, Use the following command. The archived file will have timestamp at the end of the filename. find /var/log/ -name "*batch.log" -mtime +30 -type -f -pront0 | tar -czvf /var/log/batch.log.tar.gz.`date +\%Y%m%d%H%M%S` -T - --null --remove-files

Set Specific Revision Number for SVN External

Sometimes, You might want to set revision number for svn-external repository explicitly. In that case, you should just set svn-external with "-rXXXX" on your svn property (XXXX is the revision number you want to set)! I confimred it worked perfectly on windows, linux svn client. lib -rXXXX [svn-external target url]