按日期将日志文件移动到存档文件夹,但不是每个进程的最后一个日志文件
Move log files to archive folder by date, but not the last log file for each process
我有这些文件,[processName.DateTime.out]
/stev/log: ls -al
drwxrwxr-x 1 user.stev user 4096 Feb 5 23:59 .
drwxrwxr-x 1 user.stev user 4096 Feb 5 23:59 ..
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 BBB01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 BBB01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 BBB01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 DDD01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 DDD01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 DDD01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 TEST01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 TEST01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 TEST01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 TEST02.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 TEST02.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 TEST02.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
我的脚本将一些文件移动到存档文件夹,如果它在 7 天之前被修改。
sfind . -type f -mtime +7 -name "/stev/log" | while read FNAME
do
echo " moving file: " ${FNAME} >> /stev/archive/archiveLog.log
PCMD="mv ${FNAME} /stev/archive}"
echo "PCMD=${PCMD}" >> /stev/archive/archiveLog.log
eval "${PCMD}" >> /stev/archive/archiveLog.log
echo "" >> /stev/archive/archiveLog.log
done
此脚本将在下面列出文件,然后将它们移动到存档文件夹
[当前日期:2 月 5 日]
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
但问题是,我必须保留每个进程的最后一个文件。
例如)
我可以移动下面的文件
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
但我应该保留下面的文件
(不管多少天前,因为每个进程的最后一个文件都必须保留。)
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
这个问题有什么好办法吗??
bash 和 "streams" 一样思考 - 有进有出。
# find all files
find /stev/log -type f -mtime +7 |
# extract the part before dot
cut -d. -f1 |
# for each process
# could be transformed into `xargs sh -c '...' --` for speed
while IFS= read -r process; do
# all files with this process
find /stev/log -type f -mtime +7 -name "$process*" |
# sort numerically second field behind dot
sort -t. -n -k2 |
# remove last file, because we don't want to move it
# maybe you need to remove first line, then just sort -r, check it
head -n -1
done |
# mv each file into /stev/archive
xargs -d$'\n' -I{} mv -v {} "/stev/archive" >> /stev/archive/archiveLog.log
将 -t
选项传递给 xargs
进行调试。
我有这些文件,[processName.DateTime.out]
/stev/log: ls -al
drwxrwxr-x 1 user.stev user 4096 Feb 5 23:59 .
drwxrwxr-x 1 user.stev user 4096 Feb 5 23:59 ..
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 BBB01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 BBB01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 BBB01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 DDD01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 DDD01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 DDD01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 TEST01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 TEST01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 TEST01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 1 23:59 TEST02.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 2 23:59 TEST02.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Feb 3 23:59 TEST02.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
我的脚本将一些文件移动到存档文件夹,如果它在 7 天之前被修改。
sfind . -type f -mtime +7 -name "/stev/log" | while read FNAME
do
echo " moving file: " ${FNAME} >> /stev/archive/archiveLog.log
PCMD="mv ${FNAME} /stev/archive}"
echo "PCMD=${PCMD}" >> /stev/archive/archiveLog.log
eval "${PCMD}" >> /stev/archive/archiveLog.log
echo "" >> /stev/archive/archiveLog.log
done
此脚本将在下面列出文件,然后将它们移动到存档文件夹 [当前日期:2 月 5 日]
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
但问题是,我必须保留每个进程的最后一个文件。
例如) 我可以移动下面的文件
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 AAA01.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 AAA01.202002020000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 21 23:59 TEST03.202002010000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 22 23:59 TEST03.202002020000.out
但我应该保留下面的文件 (不管多少天前,因为每个进程的最后一个文件都必须保留。)
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 AAA01.202002030000.out
-rw-rw-r-- 1 user.stev user 4096 Jan 23 23:59 TEST03.202002030000.out
这个问题有什么好办法吗??
bash 和 "streams" 一样思考 - 有进有出。
# find all files
find /stev/log -type f -mtime +7 |
# extract the part before dot
cut -d. -f1 |
# for each process
# could be transformed into `xargs sh -c '...' --` for speed
while IFS= read -r process; do
# all files with this process
find /stev/log -type f -mtime +7 -name "$process*" |
# sort numerically second field behind dot
sort -t. -n -k2 |
# remove last file, because we don't want to move it
# maybe you need to remove first line, then just sort -r, check it
head -n -1
done |
# mv each file into /stev/archive
xargs -d$'\n' -I{} mv -v {} "/stev/archive" >> /stev/archive/archiveLog.log
将 -t
选项传递给 xargs
进行调试。