
何千ものファイルを含む大容量フォルダがたくさんありますが、touch
修正時間を「元の時間」+ 3時間に設定したいと思います。
スーパーユーザーの同様のスレッドからこのスクリプトを取得しました。
#!/bin/sh
for i in all/*; do
touch -r "$i" -d '+3 hour' "$i"
done
だから私に必要なのは、固定ディレクトリではなく任意のディレクトリで動作するようにすることです(他の場所で実行したいときはいつでもスクリプトを編集する必要はありません)。再帰ファイルを編集します。
私はLinuxの経験がほとんどなく、プログラミング(主にC)について1つまたは2つ知っていますが、bashスクリプトを設定するのは今回が初めてです。
助けてくれた皆さんに心から感謝します:)
答え1
find -exec
再帰の場合は、touch
コマンドライン引数を使用してディレクトリを処理します。
#!/bin/sh
for i in "$@"; do
find "$i" -type f -exec touch -r {} -d '+3 hour' {} \;
done
次のように実行できます。
./script.sh /path/to/dir1 /path/to/dir2
答え2
正確で完全な答えは次のとおりです。
改訂する利用時間のみそして"触れる「必ず使用する必要があるコマンド」-ㅏパラメータを使用しないと、コマンドは変更時間も変更します。たとえば、3時間を追加します。
touch -a -r test_file -d '+3 hour' test_file
男の手から:
Update the access and modification times of each FILE to the current time. -a change only the access time -r, --reference=FILE use this files times instead of current time. -d, --date=STRING parse STRING and use it instead of current time
したがって、ファイルのアクセス時間は、前のアクセス時間に3時間を加えた時間と同じです。そして修正時間は変わりません。これでこれを確認できます。
stat test_file
ついに、ディレクトリ全体のアクセス時間のみを修正そしてそのファイルとサブディレクトリについて探す「ディレクトリを巡回して使用するコマンド」-実現するパラメータを使用して、すべてのファイルとディレクトリに対して「タッチ」を実行します(ただし、「- は使用しないでください」)。F型「パラメータはディレクトリには影響しません。)
find dir_test -exec touch -a -r '{}' -d '+3 hours' '{}' \;
人々から発見されました:
-type c File is of type c: d directory f regular file
-execの場合:
-exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encoun- tered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
中かっこを一重引用符で囲み、シェルスクリプトの句読点として解釈されるのを防ぎます。セミコロンもバックスラッシュを使用して保護されていますが、この場合は単一引用符も使用できます。
最後に「yaegashi」などのシェルスクリプトで使用します。
#!/bin/sh
for i in "$@"; do
find "$i" -exec touch -a -r '{}' -d '+3 hours' '{}' \;
done
「yaegashi」が言ったように実行してください。
./script.sh /path/to/dir1 /path/to/dir2
dir1とdir2のすべてのディレクトリを検索し、各ファイルとサブディレクトリのアクセス時間のみを変更します。