8

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.

Braiam
  • 69,112
Joschi
  • 361

2 Answers2

7

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
      ar="${BASH_REMATCH[1]}"
    elif [[ "$line" =~ \s*([^ ]*\.tif)$ ]] ; then
      echo "${ar}: ${BASH_REMATCH[1]}"
    fi
  done
}

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

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
0

You can use the code given below,

#!/bin/bash

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

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

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

Useage

./find_tif.sh >> tif_name_file.csv
sourav c.
  • 46,120