知らなかったコマンド色々


忘れるのでメモ

特定のユーザでコマンドを実行

$ sudo /sbin/runuser -l {ユーザ名} -c "コマンド"

日時を指定してtouch

$ touch -t201301090000 01.txt
$ touch -t201301080000 02.txt
$ touch -t201301070000 03.txt
$ touch -t198001010000 04.txt

$ ll
total 0
-rw-r--r-- 1 kobayashi wheel 0 1 9 00:00 01.txt
-rw-r--r-- 1 kobayashi wheel 0 1 8 00:00 02.txt
-rw-r--r-- 1 kobayashi wheel 0 1 7 00:00 03.txt
-rw-r--r-- 1 kobayashi wheel 0 1 1 1980 04.txt

タイムスタンプを指定して検索(今日が2013/02/08として)

$ find . -name "*.txt" -mtime +30 # 過去31日以上
./02.txt
./03.txt
./04.txt

$ find . -name "*.txt" -mtime 31 # 31日前のみ
./02.txt

$ find . -name "*.txt" -mtime -32 # 過去31日以降
./01.txt
./02.txt

xargsでmvする

GNU Linux

$ find . -name "*.txt" -mtime +30 | xargs mv --target-directory=./test/


Macだと、

$ find . -name "*.txt" -mtime +30 | xargs -J % mv % ./test/

$ ll *
-rw-r--r-- 1 kobayashi wheel 0 1 9 00:00 01.txt

test:
total 0
-rw-r--r-- 1 kobayashi wheel 0 1 8 00:00 02.txt
-rw-r--r-- 1 kobayashi wheel 0 1 7 00:00 03.txt
-rw-r--r-- 1 kobayashi wheel 0 1 1 1980 04.txt