Saturday, May 4, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 13829  / 3 Years ago, wed, may 19, 2021, 6:53:59

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.


More From » command-line

 Answers
2

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


[#27381] Friday, May 21, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
egantfis

Total Points: 406
Total Questions: 108
Total Answers: 108

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
egantfis questions
Mon, Nov 1, 21, 03:37, 3 Years ago
Fri, Mar 18, 22, 23:26, 2 Years ago
Mon, Mar 6, 23, 05:03, 1 Year ago
Sun, Oct 3, 21, 23:30, 3 Years ago
Thu, Mar 10, 22, 12:28, 2 Years ago
;