Find files and tar them (with spaces)

后端 未结 10 1838
北海茫月
北海茫月 2020-11-29 15:35

Alright, so simple problem here. I\'m working on a simple back up code. It works fine except if the files have spaces in them. This is how I\'m finding files and adding th

10条回答
  •  执笔经年
    2020-11-29 16:24

    Would add a comment to @Steve Kehlet post but need 50 rep (RIP).

    For anyone that has found this post through numerous googling, I found a way to not only find specific files given a time range, but also NOT include the relative paths OR whitespaces that would cause tarring errors. (THANK YOU SO MUCH STEVE.)

    find . -name "*.pdf" -type f -mtime 0 -printf "%f\0" | tar -czvf /dir/zip.tar.gz --null -T -
    
    1. . relative directory

    2. -name "*.pdf" look for pdfs (or any file type)

    3. -type f type to look for is a file

    4. -mtime 0 look for files created in last 24 hours

    5. -printf "%f\0" Regular -print0 OR -printf "%f" did NOT work for me. From man pages:

    This quoting is performed in the same way as for GNU ls. This is not the same quoting mechanism as the one used for -ls and -fls. If you are able to decide what format to use for the output of find then it is normally better to use '\0' as a terminator than to use newline, as file names can contain white space and newline characters.

    1. -czvf create archive, filter the archive through gzip , verbosely list files processed, archive name

    Edit 2019-08-14: I would like to add, that I was also able to use essentially use the same command in my comment, just using tar itself:

    tar -czvf /archiveDir/test.tar.gz --newer-mtime=0 --ignore-failed-read *.pdf
    

    Needed --ignore-failed-read in-case there were no new PDFs for today.

提交回复
热议问题