Is it possible to find files by the exact date then execute tar command on the results files?

女生的网名这么多〃 提交于 2019-12-23 05:06:16

问题


I want to search for files by the exact date and then execute tar command on the results files.

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.

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.

ls -ltr ./ | grep "Dec 4" | tar -zcvf /home/backupfiles_$(date '+%Y_%m_%d' --date='1 days ago').tar.gz


回答1:


stat -c"%z;%n" * | grep '^2012-12-04' | awk -F';' '{ print $2 }' | xargs tar -zcvf dec4.tar.gz



回答2:


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. You can specify your required date in find command.




回答3:


  ls -l ./ | grep "Dec 4" | tar -cvf archive.tar --null -T /dev/stdin

EDIT : use a temp file to store file names:

ls -l | grep "Dec  4" | tr -s " " "#" | cut -d "#"  -f 9 >tempfile ; tar -T tempfile -cvf arch.tar ; rm -r tempfile 



回答4:


So you want to tar files that are modified on a certain date? 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.




回答5:


Thanks to 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


来源:https://stackoverflow.com/questions/20390573/is-it-possible-to-find-files-by-the-exact-date-then-execute-tar-command-on-the-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!