1
0

More than 3 years have passed since last update.

ファイル名、更新日時など複数条件で検索する

Last updated at Posted at 2019-10-04

2019/11/08
複数のディレクトリを検索条件とした結果を追記

[root@testhost tmp]# find /system/log /system/info /system/alert \( -name "AAA.txt*" -or -name "BBB.txt*" -or -name "CCC.txt*" -or -name "DDD.txt*" -or -name "EEE.txt*" -or -name "FFF.txt*" \) -newermt "20191002 00:00" ! -newermt "20191002 22:59" | xargs ls -ltr
-rw-r--r--. 1 root root 2097152 10月  2 03:00 /system/log/BBB.txt.1
-rw-r--r--. 1 root root 2097152 10月  2 03:00 /system/log/AAA.txt.1
-rw-r--r--. 1 root root 1048576 10月  2 03:00 /system/info/DDD.txt
-rw-r--r--. 1 root root 1048576 10月  2 03:00 /system/info/CCC.txt
-rw-r--r--. 1 root root 1048576 10月  2 03:00 /system/alert/FFF.txt
-rw-r--r--. 1 root root 1048576 10月  2 03:00 /system/alert/EEE.txt
-rw-r--r--. 1 root root 2097152 10月  2 07:00 /system/log/BBB.txt.2
-rw-r--r--. 1 root root 2097152 10月  2 07:00 /system/log/AAA.txt.2
-rw-r--r--. 1 root root 2097152 10月  2 12:00 /system/log/BBB.txt.3
-rw-r--r--. 1 root root 2097152 10月  2 12:00 /system/log/AAA.txt.3
-rw-r--r--. 1 root root 2097152 10月  2 22:00 /system/log/BBB.txt.4
-rw-r--r--. 1 root root 2097152 10月  2 22:00 /system/log/AAA.txt.4

[root@testhost tmp]# tar cfpvz /tmp/`date +%Y%m%d`_`uname -n`_systemlog.tar.gz `find /system/log /system/info /system/alert \( -name "AAA.txt*" -or -name "BBB.txt*" -or -name "CCC.txt*" -or -name "DDD.txt*" -or -name "EEE.txt*" -or -name "FFF.txt*" \) -newermt "20191002 00:00" ! -newermt "20191002 22:59"`
tar: メンバ名から先頭の `/' を取り除きます
/system/log/AAA.txt.1
/system/log/AAA.txt.2
/system/log/AAA.txt.3
/system/log/AAA.txt.4
/system/log/BBB.txt.1
/system/log/BBB.txt.2
/system/log/BBB.txt.3
/system/log/BBB.txt.4
/system/info/CCC.txt
/system/info/DDD.txt
/system/alert/EEE.txt
/system/alert/FFF.txt

[root@testhost tmp]# ls -ltr /tmp/`date +%Y%m%d`_`uname -n`_systemlog.tar.gz
-rw-r--r--. 1 root root 21182 11月  9 10:44 /tmp/20191109_testhost_systemlog.tar.gz

; -or で複数条件を使用していずれかの条件に当てはまるものを検索する
; \(\)で括ることでその中の条件が優先される
; -newermt "日時" ! -newermt "日時"で期間指定が可能となる
find /system/log \( -name "AAA.txt*" -or -name "BBB.txt*" \) -newermt "20191001 00:00" ! -newermt "2091003 23:59" -ls

以下、実行結果

ls -ltr /system/log/
合計 49152
-rw-r--r--. 1 root root 2097152  9月  2 04:00 BBB.txt.8
-rw-r--r--. 1 root root 2097152  9月  2 04:00 AAA.txt.8
-rw-r--r--. 1 root root 2097152  9月  2 08:00 BBB.txt.9
-rw-r--r--. 1 root root 2097152  9月  2 08:00 AAA.txt.9
-rw-r--r--. 1 root root 2097152  9月  3 12:00 BBB.txt.10
-rw-r--r--. 1 root root 2097152  9月  3 12:00 AAA.txt.10
-rw-r--r--. 1 root root 2097152  9月  3 22:00 BBB.txt.11
-rw-r--r--. 1 root root 2097152  9月  3 22:00 AAA.txt.11
-rw-r--r--. 1 root root 2097152 10月  2 03:00 BBB.txt.1
-rw-r--r--. 1 root root 2097152 10月  2 03:00 AAA.txt.1
-rw-r--r--. 1 root root 2097152 10月  2 07:00 BBB.txt.2
-rw-r--r--. 1 root root 2097152 10月  2 07:00 AAA.txt.2
-rw-r--r--. 1 root root 2097152 10月  2 12:00 BBB.txt.3
-rw-r--r--. 1 root root 2097152 10月  2 12:00 AAA.txt.3
-rw-r--r--. 1 root root 2097152 10月  2 22:00 BBB.txt.4
-rw-r--r--. 1 root root 2097152 10月  2 22:00 AAA.txt.4
-rw-r--r--. 1 root root 2097152 10月  3 04:00 BBB.txt.5
-rw-r--r--. 1 root root 2097152 10月  3 04:00 AAA.txt.5
-rw-r--r--. 1 root root 2097152 10月  3 07:00 BBB.txt.6
-rw-r--r--. 1 root root 2097152 10月  3 07:00 AAA.txt.6
-rw-r--r--. 1 root root 2097152 10月  3 10:00 BBB.txt.7
-rw-r--r--. 1 root root 2097152 10月  3 10:00 AAA.txt.7
-rw-r--r--. 1 root root 2097152 10月  4 22:58 AAA.txt
-rw-r--r--. 1 root root 2097152 10月  4 22:58 BBB.txt
drwxr-xr-x. 2 root root      32 10月  5 01:57 test
[root@testhost ~]# find /system/log \( -name "AAA.txt*" -or -name "BBB.txt*" \) -newermt "20191001 00:00" ! -newermt "20191003 23:59" -ls
  1789 2048 -rw-r--r--   1 root     root      2097152 10月  2 03:00 /system/log/AAA.txt.1
  1790 2048 -rw-r--r--   1 root     root      2097152 10月  2 07:00 /system/log/AAA.txt.2
  1791 2048 -rw-r--r--   1 root     root      2097152 10月  2 12:00 /system/log/AAA.txt.3
  1792 2048 -rw-r--r--   1 root     root      2097152 10月  2 22:00 /system/log/AAA.txt.4
  1793 2048 -rw-r--r--   1 root     root      2097152 10月  3 04:00 /system/log/AAA.txt.5
  1794 2048 -rw-r--r--   1 root     root      2097152 10月  3 07:00 /system/log/AAA.txt.6
  1795 2048 -rw-r--r--   1 root     root      2097152 10月  3 10:00 /system/log/AAA.txt.7
  1801 2048 -rw-r--r--   1 root     root      2097152 10月  2 03:00 /system/log/BBB.txt.1
  1802 2048 -rw-r--r--   1 root     root      2097152 10月  2 07:00 /system/log/BBB.txt.2
  1803 2048 -rw-r--r--   1 root     root      2097152 10月  2 12:00 /system/log/BBB.txt.3
  1804 2048 -rw-r--r--   1 root     root      2097152 10月  2 22:00 /system/log/BBB.txt.4
  1805 2048 -rw-r--r--   1 root     root      2097152 10月  3 04:00 /system/log/BBB.txt.5
  1806 2048 -rw-r--r--   1 root     root      2097152 10月  3 07:00 /system/log/BBB.txt.6
  1807 2048 -rw-r--r--   1 root     root      2097152 10月  3 10:00 /system/log/BBB.txt.7

検索結果をそのままアーカイブしてgzip形式で圧縮することも可能

[root@testhost ~]# tar cfpvz /tmp/`date +%Y%m%d`_`uname -n`_systemlog.tar.gz `find /system/log \( -name "AAA.tx
t*" -or -name "BBB.txt*" \) -newermt "20191001 00:00" ! -newermt "20191003 23:59"`
tar: メンバ名から先頭の `/' を取り除きます
/system/log/AAA.txt.1
/system/log/AAA.txt.2
/system/log/AAA.txt.3
/system/log/AAA.txt.4
/system/log/AAA.txt.5
/system/log/AAA.txt.6
/system/log/AAA.txt.7
/system/log/BBB.txt.1
/system/log/BBB.txt.2
/system/log/BBB.txt.3
/system/log/BBB.txt.4
/system/log/BBB.txt.5
/system/log/BBB.txt.6
/system/log/BBB.txt.7
[root@testhost ~]# ls -l /tmp/`date +%Y%m%d`_`uname -n`_systemlog.tar.gz
-rw-r--r--. 1 root root 29464 10月  5 02:30 /tmp/20191005_testhost_systemlog.tar.gz
[root@testhost ~]#
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0