Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am trying to zip a certain number of files and am stuck at below point,

   ls -lrt /lag/cnn/*/*/*/adb.tar.gz | grep -nri ABC >> sample.txt

In sample.txt i got filesNames on which i have to do touch command to extract filesNames that i need to tar.

   touch -t '<time>' first & touch -t '<time>' last 

So my question is , how can i implement this touch in sample.txt to extract what i need based on time and tar those files. Whether touch command and tar command can be added to the above command using pipe or should i do it separately.

share|improve this question
Can you tell us what you are trying to achieve? What are you using touch for? What files are you trying to zip? – richard Dec 19 '13 at 12:43
@richard I have certain files in the path /lag/cnn/*/*/*/ i need to tar those files which are created between certain time.Am trying to do this from root directory. So far i have got all the files in the path after grep in sample.txt . from the file i have to extract files based on time and tar it. – Mano Dec 19 '13 at 13:05
find is your friend. I will write an answer. – richard Dec 19 '13 at 13:13

Ask find to find files within /lag/cnn to a maximum depth of 3 of at least so many minutes old. Then using xargs pass them to tar (to zip it).

This works with gnu tools:

find /lag/cnn -maxdepth 3 -newermt "2013-12-19 00:00" -o -type f -newermt "2013-12-16 00:00" -print0 | 
xargs -0 tar acf out.tar.gz

This should work with other tools.

touch -d 2013-12-19 00:00 later-timestamp.temporary
touch -d 2013-12-16 00:00 earlier-timestamp.temporary
find /lag/cnn -maxdepth 3 -newermm later-timestamp.temporary  -o -type f -newermm earlier-timestamp.temporary -print0 | 
xargs -0 tar zcf out.tar.gz
share|improve this answer
My start time is :201312160000.00 and my End time is:201312190000.00 in the format "yyyymmddhhmm.ss" and i need to extract files between this time. how to use this in the above command.The format can be changed for that time how to use the command. – Mano Dec 19 '13 at 13:29
Am getting, find: invalid predicate `-max-depth' and tar: invalid option -- a What is the problem? – Mano Dec 20 '13 at 4:46
I have changed it to -maxdepth but now am getting find: invalid predicate `-newermt' – Mano Dec 20 '13 at 4:50
Ah you are not using the Gnu versions. For gnu-tar the a is compress based on file extension (change it to z). For find I don't know, the manual says that not all finds support all of the -newerXY combinations. You could try -newermm reference file, you will have to use touch to create the two reference files somewhere out of the way. – richard Dec 21 '13 at 10:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.