Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to write a shell script in order to write all files ending with .tif in a given directory and all its subdirectories to a csv file.

The directory contains various sub-directories and those can also contain .zip folders so the script should be able to extract the names from zip folders too.

I first thought about separating these steps though (unzipping and getting the filenames) but I'm not sure if this is really necessary.

Since I'm really new to working with the Shell, any help would be appreciated.

share|improve this question
How to loop through sub folders? askubuntu.com/questions/406462/… –  Sneetsher Jan 20 '14 at 17:48

2 Answers 2

up vote 5 down vote accepted

To search for .tif files inside a folder and its subfolders, then write the output in a .csv file you can use the following command:

find /path/to/folder -iname '*.tif' -type f >tif_filenames.csv

To search also inside .zip files and append the output to the previous tif_filenames.csv file you can use:

find /path/to/folder -iname '*.zip' -type f -exec unzip -l '{}' \; | process

where process is the following bash function:

function process() {
  while read line; do
    if [[ "$line" =~ ^Archive:\s*(.*) ]] ; then
    elif [[ "$line" =~ \s*([^ ]*\.tif)$ ]] ; then
      echo "${ar}: ${BASH_REMATCH[1]}"

Source: http://unix.stackexchange.com/a/12048/37944

share|improve this answer
I think it will be *.tif instead of *.sh. Isn't it? –  souravc Jan 20 '14 at 19:16
@souravc Yes, you have right. I tested those commands for .sh files. –  Radu Rădeanu Jan 20 '14 at 20:02
me too. That is why I noticed it. One more thing in the first case your output will consist the path with its name. –  souravc Jan 20 '14 at 20:08
@souravc Yep, this was my intention. The OP didn't didn't specified clear this and for me is better to see the full path for files. –  Radu Rădeanu Jan 20 '14 at 20:14
thanks for your help. it worked for me. another way to retrieve names inside zip files is: unzip -l mypath/myfile.zip | awk '{print $NF}' >> echo /home/myname/mylist.csv –  Joschi Jan 22 '14 at 13:39

You can use the code given below,


for f in $(find . -iname "*.tif" -type f)
    echo "$(basename $f)"

for f in $(find . -iname "*.zip" -type f)
    less $f | grep "tif" | awk '{print $NF}'

Save the file say as find_tif.sh in the directory where all the tif files and sub directories are located. give it executable permission.

chmod +x find_tif.sh


./find_tif.sh >> tif_name_file.csv
share|improve this answer

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.