bash - How can I rename of old files? -
i have directory this:
-rw-r--r-- 1 root root 0 jan 7 15:04 tmp_file2015_123_1_3123.log -rw-r--r-- 1 root root 0 jan 7 15:04 tmp_file2015_133_1_3123.log -rw-r--r-- 1 root root 0 jan 7 16:04 tmp_file2015_133_1_3125.log -rw-r--r-- 1 root root 0 jan 7 16:04 tmp_file2015_133_1__3223125.log -rw-r--r-- 1 root root 0 jan 7 16:04 tmp_file2015_133_1_3223125.log
i need remove tmp_ , can this:
for in *; s=$(sed -r 's/^(tmp_)(.*.log)/\2/' <<< $i); if [[ "$i" != "$s" ]]; mv "$i" "$s"; fi; done;
but need older 1 hour (modified time) files:
for example ( now: jan 7 16:10 ):
-rw-r--r-- 1 root root 0 jan 7 13:00 file2015_123_1_3123.log -rw-r--r-- 1 root root 0 jan 7 15:04 file2015_133_1_3123.log -rw-r--r-- 1 root root 0 jan 7 15:01 file2015_133_1_3125.log -rw-r--r-- 1 root root 0 jan 7 16:04 tmp_file2015_133_1__3223125.log -rw-r--r-- 1 root root 0 jan 7 16:10 tmp_file2015_133_1_3223125.log
how can that?
this operate on files modified in last hour:
for orig_file in $(find . -type f -depth 1 -mtime -60m); new_file="${orig_file#./tmp_}" if [[ "$new_file" != "$orig_file" ]]; mv "$orig_file" "$new_file"; fi; done
if want operate on files older 1 hour use +60m
instead
i've changed use of sed use built in bash functionality.
Comments
Post a Comment