I'm trying to make a tar.tgz file using command substitution There are backups of 17 Aug 2012-rw-r----- 1 ossec  502 804M Aug 17  2012 BKPMDISINT_i6nir20j.F_bkp-rw-r----- 1 ossec  502 2.7G Aug 17  2012 BKPSYFINT_i5nir20j.F_bkp-rw-r----- 1 ossec  502 3.9G Aug 17  2012 BKPMSFFINT_i4nir20j.F_bkp-rw-r----- 1 ossec  502  28M Aug 17  2012 c-651746692-20120817-00-rw-r----- 1 ossec  502 2.0G Aug 17  2012 ALG_KLPFINT_ianir576.alg-rw-r----- 1 ossec  502 1.5G Aug 17  2012 ALG_KLPFINT_i8nir576.alg-rw-r----- 1 ossec  502  37M Aug 17  2012 ALG_KLPINT_ibnir64h.alg-rw-r----- 1 ossec  502 2.2G Aug 17  2012 ALG_KLPFINT_i9nir576.alg-rw-r----- 1 ossec  502  28M Aug 17  2012 c-651746692-20120817-01-rw-r----- 1 ossec  502  27M Aug 17  2012 ctrl_170803So i need to tar them. I used this command tar -zcvf DB-Backup-17082012.tgz $(ls -lrth|grep Aug 17)But it gave me this error tar: You may not specify more than one `-Acdtrux' optionTry `tar --help' or `tar --usage' for more information.
Command substitution grepping ls output causes error
shell;files;backup;tar;command substitution
Your wayYou can surely do what you want but it is probably dangerous given some files might have spaces in their names. Here's one way to do it though.$ tar -zcvf DB-Backup-17082012.tgz $(ls -lrth|grep Aug 17|awk '{print $9}')This will get rid of all the other column data that you're getting from the ls -lrth and just give you the file names. Using find & tar: method #1You can use the Unix find command to get a list of all the files that were last modified on a given date and pipe this list to tar as follows:$ find . -type f -newermt 2012-08-17 ! -newermt 2012-08-18 -print0 | \   xargs -0 tar zcvf DB-Backup-17082012.tgzThe above will only find files that were last modified on 2012-08-17 but nothing newer. The -print0 tells find to split the files that it outputs using null characters (\0) and the xargs -0 takes each of these files and builds up invocations of tar adding the files in batches at a time.This is probably the least performant way to do this.NOTE: This method also runs the risk of losing files if your have more than can be passed as command line arguments to the tar command.Using find & tar: method #2The -print0 and the xargs -0 are redundant and you can use just find to generate the list and call tar directly:$ find . -type f -newermt 2012-08-17 ! -newermt 2012-08-18 \   -exec tar zcvf DB-Backup-17082012.tgz {} \+The {} \+ arguments to find above will do 2 things. The {} will substitute the name of each file as find finds it in this position when calling tar and the \+ tells find to keep putting as many files as it can on the command line before calling tar. This saves you on having to invoke tar 100 times if you have 100 files to add to it.Referencesfind man pagetar man page