UNIX系OS ( Mac / Linux / FreeBSD )のシェルコマンドに関する各種メモ書き

UNIXのシェルである日付以降のすべてのファイルを検索する

● 起点となるファイルを指定して、そのファイルの更新日以降に更新されたファイルを表示させる

1. 起点となるファイル(ファイル名はなんでもOK。例:datefile)を 作成する

・現在の日付で 起点となるファイル「datefile」を作成する

touch datefile

・特定の日付(2018/2/14)で起点となるファイル「datefile」を作成する

touch -m -d '2018/2/14 00:00:00' datefile

・FreeBSDの場合は次のようにして作成してから日付を変更する

touch datefile
touch -t 201802140000  datefile 

・Macの場合は -t オプションを使用して 日付を指定して中身が空のファイルを作成する

touch -t 201802140000 datefile

2. findコマンドで起点となるファイルを指定して検索

find . -type f -newer datefile

3. 更新があったファイルをコピーする

*(1. Linuxの場合)updated_files に更新ファイルをコピーします。

mkdir updated_files
find . -type f -newer datefile -print0 |xargs -0 cp --parents -t ./updated_files

cp コマンドオプションの説明( Linux )

--parents  ディレクトリ 階層もコピーする
-t ターゲットディレクトリ 

*(2. FreeBSDの場合)FreeBSDの場合は cp --parents が使用できないので次のように記述します

mkdir updated_files
find . -type f -newer datefile -exec cp {} updated_files \;

find -exec コマンドオプションの説明( FreeBSD )

-exec  コマンドを実行する
{}  全てのファイルが対象
\;  コマンドの区切り文字(必ずつける)

 引用:https://blog.hello-world.jp.net/linux/1255/

No.1193
02/14 11:31

edit