2

I'm new to Unix and am trying to extract specific pattern filename from the multiple zip files. I'm using find to find the zip files, and then trying to extract files having the filename CTL in the zip file, a*b*zip.

$ unzip -jn 'find $source_dir -mtime -$st_d -daystart -iname "a*zip"' \
    *CTL* -d $target_dir`

Other than this I tried using:

$ find -type f -newermt $start_date \! -newermt $end_date -name "a*zip" \
    | while read filename; do unzip -d $target_dir; done;`

but this is extracting all the files, but I'm looking for only those files, which have filename CTL.

1

You can simply do that using find -exec alone.

find . -type f -name '*.zip' -exec unzip {}  '*CTL*' -d "$target_dir" \;

Explanation: -exec will execute the following command on each file found where {} is substituted for the filename. The command must be terminated by ;. Use \; at the end (';' escaped with \) to avoid your shell from interpreting the ; for its own as a termination for find, instead it is passed to find as regular text.

Also, notice that it is a good practice to enclose file patterns in quotes when they are passed as arguments, i.e. '*CTL*' as opposed to just *CTL*. When doing so, you avoid letting your shell expand the filenames. For example, suppose in this case one of your zip files has CTL in its filename (say abcCTL.zip). If you use just *CTL* for the pattern, the shell will expand it and instead of passing a pattern to unzip, it will instead pass the filename abcCTL.zip, which is clearly not what you want.

  • This doesn't sound right. Notice that find . searches in the current directory. If you run it from / it is going to search everywhere, including the /target_dir. Try to give a specific directory to look into find /ddw_projs/ddw/tst/data/ for example. – Bichoy Apr 25 '15 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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