Linuxの下でどのようにして指定された日付に変更されたすべてのファイルを一度に削除しますか?

7479 ワード

問:Linuxの下でどのようにして指定された日付に変更されたすべてのファイルを一度に削除しますか?
 
答:スクリプトdel_files_of_date.sh
使用法:./del_files_of_date.sh
例:./del_files_of_date.sh 2011-09-01 html/*.html
#!/bin/sh

: ${2?"usage: $0 <YYYY-mm-dd> <files...>"}

for f in "${@:2}"
do
        modify_time=$(stat --printf '%y' "$f")
        if [[ $modify_time == $1* ]]; then
                echo "$f" "$modify_time"
                rm -v -- "$f"
        fi
done

 
次に、このスクリプトの実行例を示します.
[root@jfht contents]# ./del_files_of_date.sh 2011-08-01 html/*.html html/0041bb66458751c989fe55d8dd837458.html 2011-08-01 08:55:58.00 00000000+0800「html/0041 bb 66458751 c 989 fe 55 d 8 dd 837458.html」html/0562 e 464 e 720 e 482 fe 0 a 33 ac 71198 ea.html 2011-08-01 12:55:3300000000+0800削除「html/05602 e 464 e 720 e 482 fe 0 a 33 ac 71198 ea.html」・・・htm l/f 4 dd 248 f 7 f 6 b 97 bc 07519 d 561 d 47111.html 2011-08-01 18:55:3700000000+0800「html l/f 4 dd 248 f 7 f 6 b 97 bc 07519 d 561 d 47111.html」が削除されました[root@jfht contents]#
 
このスクリプトについて詳しく説明します.
 
3行目:${2?「usage:$0 
組み込みコマンド:(コロン)パラメータ拡張に使用します.もしなかったら、後ろの${2...}をの結果をコマンドとして実行します.
man:書く
: [arguments]
     No effect; the command does nothing beyond expanding arguments and performing any specified redirec-
tions. A zero exit code is returned.
 
[root@jfht contents]# touch 1.txt [root@jfht contents]# ./del_files_of_date.sh 2011-09-01 1.txt ./del_files_of_date.sh: line 3: 1.txt: command not found
上の行は前に追加されていません:コマンドの実行効果.1.txt 2011-09-01 19:56:46.000000000 +0800 [root@jfht contents]#
 
(詳細:コマンドの詳細については、「私が使ったLinux命令之:(コロン)-何もしない(以外は...)」を参照)
 
3行目:${2?「usage:$0 
${VAR?STRING}の役割は、変数VARが空であるか否かを判断し、空であればSTRINGで指定された情報を印刷して終了することである.
これは、コマンドラインパラメータが十分に提供されているかどうか、または変数が設定されているかどうかを確認するのに適しています.
その中で、?前の2は、2番目のコマンドラインパラメータであることを示します.すなわち、2番目のコマンドラインパラメータが設定されていない場合、印刷ヘルプ情報が終了します.
 
[root@jfht contents]# ./del_files_of_date.sh ./del_files_of_date.sh: line 3: 2: usage: ./del_files_of_date.sh [root@jfht contents]# ./del_files_of_date.sh 2011-09-01 ./del_files_of_date.sh: line 3: 2: usage: ./del_files_of_date.sh [root@jfht contents]#
 
5行目:for f in${@:2}
 
Bashにおけるfor構造の書き方
for VAR in LIST
do
    DO_SOMETHING_OF_VAR
done
 
(for文の詳細については、「私が使ったLinuxコマンドのfor-BashでのForループ」を参照)
 
5行目:for f in"${@:2}"
 
${@:2}については、@はすべてのコマンドラインパラメータを表し、2は2番目のパラメータから始まるすべてのパラメータを表します.
より一般的な場合:${@:START:N}は、START番目のパラメータからN個のパラメータを切り取ることを示す.
 
注意したいのは${@:2}は二重引用符に書かなければなりません.
二重引用符を削除すると、次のようにスペースのあるファイル名を正しく処理できません.
 
[root@jfht contents]# ./del_files_of_date.sh 2011-09-03 'hello world.txt'stat:statできません「hello」:そのファイルまたはディレクトリがありませんstat:statできません「world.txt」:そのファイルまたはディレクトリがありません
ファイル名にスペースを区切って、明らかに所望の結果ではありません.[root@jfht contents]#
 
二重引用符を付ける処理:
[root@jfht contents]# ./del_files_of_date.sh 2011-09-03 'hello world.txt'stat:hello world.txtをstatできません:そのファイルやディレクトリがありません[root@jfht contents]#
 
7行目:modify_time=$(stat --printf '%y' "$f")
$()については,その内容をコマンドラインとして実行し,標準出力を文字列に保存する.
逆引用符のペアで実現することもできますmodify_と書くことができます.time=`stat --printf '%y' "$f"`
 
7行目:modify_time=$(stat --printf '%y' "$f")
 
$(stat--printf'%y'"$f")は、指定されたファイル$fの変更時間(2011-09-01 10:26:04.000000000+0800など)を出力するために使用されます.
statは、サイズ、変更時間、アクセス時間など、ファイルに関する情報を印刷するために使用されます.
最初に私が採用したパラメータは--formatで、後で--printfに変更したほうがいいです.
 
man statは
-c --format=FORMAT
    use the specified FORMAT instead of the default; output a newline after each use of FORMAT
--printf=FORMAT
    like --format, but interpret backslash escapes, and do not output a mandatory trailing newline. If you
want a newline, include in FORMAT.
The valid format sequences for files
       %y     Time of last modification
 
(statコマンドの詳細については、「私が使用したLinuxコマンドのstat-ファイルまたはファイルシステムのステータスを表示」を参照)
 
8行目:if[[$modify_time=$1*]];then
 
Bashにおけるif構造の書き方は以下の通りである.
if CONDITION; then
   DO_SOMETHING
fi
 
もしDO_SOMETHINGの部分の文は少なくて、書くことができます
if CONDITION; then DO_SOMETHING; fi
しかしfiの前にセミコロンを付けるのを忘れないでください.そうしないと、Bash解析の時にコマンドが終わったかどうか分かりません.
 
(if文の詳細については、「私が使ったLinuxコマンドのif-Bashの条件判断文」を参照)
 
8行目:if[[$modify_time=$1*]];then
 
[[]]については、Bashで[]は組み込みコマンドです.[]とは異なり、変数を二重引用符で参照せずにワイルドカードモードのマッチングをサポートできます.
[$modify_time=$1*]]はmodify_を表すtimeは1番目のコマンドラインパラメータで始まります.
 
help[[書き込み]
[[ ... ]]: [[ expression ]]
Returns a status of 0 or 1 depending on the evaluation of the conditional
expression EXPRESSION. Expressions are composed of the same primaries used
by the `test' builtin, and may be combined using the following operators
( EXPRESSION ) Returns the value of EXPRESSION
! EXPRESSION True if EXPRESSION is false; else false
EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false
EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false
When the `==' and `!=' operators are used, the string to the right of the
operator is used as a pattern and pattern matching is performed. The
&& and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
determine the expression's value.
 
10行目:rm-v--"$f"
rmコマンドは、ファイルを削除するために使用されます.-vパラメータを追加すると、削除したファイル情報を印刷できます.追加--パラメータは、$fが-で始まる場合を正しく処理するためです.
manrmは書いている
-v, --verbose
     explain what is being done
To remove a file whose name starts with a ‘-’, for example ‘-foo’, use one of these commands:
              rm -- -foo
              rm ./-foo
 
(rmコマンドの詳細については、「私が使用したLinuxコマンドのrm-ファイルまたはディレクトリを削除」を参照)
 
 
本文リンク:http://codingstandards.iteye.com/blog/1163913(転載は出典を明記してください)
 
PS:5行目から12行目は完全に1行に短縮できます.
スクリプトdel_files_at_date.sh
#!/bin/sh

: ${2?"usage: $0 <YYYY-mm-dd> <files...>"}

for f in "${@:2}"; do [[ $(stat --printf '%y' "$f") == $1* ]] && rm -v -- "$f"; done


 
[root@jfht contents]# ./del_files_at_date.sh ./del_files_at_date.sh: line 3: 2: usage: ./del_files_at_date.sh [root@jfht contents]# touch test1.txt 'test space.txt' [root@jfht contents]# ./del_files_at_date.sh 2011-09-02 test*.txtは「test 1.txt」を削除し「test space.txt」を削除した[root@jfht contents]#