[英]Is it possible to find files by the exact date then execute tar command on the results files?
I want to search for files by the exact date and then execute tar command on the results files. 我想按确切日期搜索文件,然后对结果文件执行tar命令。
Example: 例:
-rw-rw-r-- 1 0 Dec 4 08:07 file-1
-rw-rw-r-- 1 0 Dec 4 08:07 file-2
-rw-rw-r-- 1 0 Dec 5 08:07 file-3
I want to get only files with Dec 4 date and passing them to the tar command. 我想只获取Dec 4日期的文件并将它们传递给tar命令。
The archive should contain: 档案应包含:
tar -ztvf dec4.tar.gz
Output: 输出:
-rw-rw-r-- 1 0 Dec 4 08:07 file-1
-rw-rw-r-- 1 0 Dec 4 08:07 file-2
This is my script it is still copying those Dec 5. 这是我的脚本,它仍在复制那些12月5日。
ls -ltr ./ | ls -ltr ./ | grep "Dec 4" |
grep“12月4日”| tar -zcvf /home/backupfiles_$(date '+%Y_%m_%d' --date='1 days ago').tar.gz
tar -zcvf / home / backupfiles _ $(date'+%Y_%m_%d' - date ='1天前')。tar.gz
ls -l ./ | grep "Dec 4" | tar -cvf archive.tar --null -T /dev/stdin
编辑 :使用临时文件来存储文件名:
ls -l | grep "Dec 4" | tr -s " " "#" | cut -d "#" -f 9 >tempfile ; tar -T tempfile -cvf arch.tar ; rm -r tempfile
find . -type f -name "*" -newermt 2013-12-04 ! -newermt 2013-12-05 | xargs -I {} tar -czvf files.tar.gz {}
Note: This will tar only the files not the directory hierarchy. 注意:这将仅tar文件而不是目录层次结构。 You can specify your required date in find command.
您可以在find命令中指定所需的日期。
stat -c"%z;%n" * | grep '^2012-12-04' | awk -F';' '{ print $2 }' | xargs tar -zcvf dec4.tar.gz
So you want to tar files that are modified on a certain date? 所以你想要tar某个日期修改过的文件? If so, that's a specific instance of the general problem " taring files modified between two dates ".
如果是这样,那就是“ 在两个日期之间修改文件 ”的一般问题的特定实例。 So like:
所以喜欢:
find /path/to/files/ \
-newermt 20131204 -not -newermt 20131205 -type f -print0 \
| cpio --create --null --format=ustar \
| gzip > /tmp/dec-4.tar.gz
This handles the case of many files, files that have spaces in the names, and avoids issues of grep including files that have a date in the name. 这处理许多文件的情况,名称中包含空格的文件,并避免grep问题,包括名称中包含日期的文件。
Thanks to overloop! 感谢overloop! :) this is my script!
:)这是我的剧本!
stat -c"%z;%n" * | grep '^2013-12-04' | grep "file-*" | awk -F';' '{ print $2 }' | xargs tar -zcvf pjpj.tar.gz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.