Search code examples
linuxfindtarlinux-mint

Making a tar file by date


Hi I'm with a simple question but didn't find why it's happenig,

I want to make a tar only with the files that downloaded yesterday. I'm using find to take all the file names, but when I try to use tar with find to make a tar, it make a file tar with all the items in the directory. Let me give an exemple:

When I run a find -mtime 0 it gives me some files that I downloaded yesterday

./linuxmint-19-xfce-32bit.iso
./utserver.tar.gz
./nf_9866.pdf
./-@ut c4d 22.torrent

But when i use find -mtime 0 | xargs tar -zcvf teste.tar.gz -T - It makes the tar with all the files

./Modelo_KCS_-_Devolucao_de_Compra2.doc
./Modelo_KCS_-_Devolucao_de_Compra2(2).doc
./Pref.25-04-20231-3.pdf
./Centro_Diag.14-04-20231.pdf
./OC 2290.pdf
./Configuracao_Cliente.docx
./Twine.html
./empresa.ods
./file22.xlsx
[...]

What I'm doing wrong?

I tried different ways to get files by date like `

find  -newermt 2023-06-30 ! -newermt 2023-07-01 | xargs tar -cvzf teste.tar.gz
find /home/user/Downloads/ -type f -newermt 2023-06-29 ! -newermt 2023-06-30 | tar -czvf teste.tar.gz`

but neither ways worked.


Solution

  • No need for xargs when tar already reads the filenames from stdin. This should work to create a tarball with your downloads from yesterday:

    find . -type f -mtime 1 | tar czvf test.tar.gz -T -
    

    If you still experience issues it's most likely an incorrect find result. What happens if you try the same but with a filename wildcard:

    find . -name '*.docx' | tar czvf alldocs.tar.gz -T -
    

    You can also do this in two steps where you can verify your list:

    find . -mtime 1 > /tmp/list.txt
    tar czvf test.tar.gz -T /tmp/list.txt
    

    If there is anything in your tarball that wasn't in /tmp/list.txt then use the contents of the list as arguments:

    cat /tmp/list.txt|xargs tar czvf test.tar.gz