I have a directory with over 1000 files, and a few of them are .zip files, and I want to turn them all into .tgz files.

I want to use grep to make a list of file names. I have tried "grep *.zip" to no avail. Reading the man pages, I thought that * was a universal selector. Why is this not working?

Thank you.

  • 1
    Use ls, not grep. – Maxime R. Jan 19 '12 at 17:38
  • Actually, since *.zip is expanded by the shell, you don't even need to use ls - you could use echo *.zip – dannysauer Nov 6 '15 at 15:32

You should really use find instead.

find <dir> -iname \*.zip

Example: to search for all .zip files in current directory and all sub-directories try this:

find . -iname \*.zip

This will list all files ending with .zip regarding of case. If you only want the ones with lower-case change -iname to -name

The command grep searches for stings in files, not for files. You can use it with find to list all your .zip files like this

find . |grep -e "\.zip$"
  • Added some info on how to use find with grep if you're interested. – Zoke Jan 19 '12 at 17:51
  • 2
    Given that .zip files might be coming from one of "those other platforms," it's conceivable that they'd potentially contain spaces. Assuming the conversion process is bundled into a script like, say, zip2tgz, it might be slightly more convenient to either use find -iname '*.zip' -print0 | xargs -0 zip2tgz or find -iname '*.zip' -exec zip2tgz '{}' \; – dannysauer Nov 6 '15 at 15:31

Grep uses regular expressions by default. The expression you'd want is:

grep .zip$

You probably want to use find though.

find . -name '*.zip'

Or you can pipe find into grep

find . | grep .zip$
  • 1
    No need to pipe the results of find to grep, see above. – Panther Jan 19 '12 at 18:05

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.