Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively look for files with a specific extension

I'm trying to find all files with a specific extension in a directory and its subdirectories with my bash (Latest Ubuntu LTS Release).

This is what's written in a script file:

#!/bin/bash  directory="/home/flip/Desktop" suffix="in"  browsefolders ()   for i in "$1"/*;    do     echo "dir :$directory"     echo "filename: $i"     #   echo ${i#*.}     extension=`echo "$i" | cut -d'.' -f2`     echo "Erweiterung $extension"     if     [ -f "$i" ]; then                  if [ $extension == $suffix ]; then             echo "$i ends with $in"          else             echo "$i does NOT end with $in"         fi     elif [ -d "$i" ]; then       browsefolders "$i"     fi   done } browsefolders  "$directory" 

Unfortunately, when I start this script in terminal, it says:

[: 29: in: unexpected operator 

(with $extension instead of 'in')

What's going on here, where's the error? But this curly brace

like image 281
flip Avatar asked May 08 '11 12:05

flip


People also ask

How do I search for all files with specific extensions?

For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.

What command can we use to recursively find all files with an INI extension?

Using Glob() function to find files recursively We can use the function glob.


1 Answers

find $directory -type f -name "*.in" 

is a bit shorter than that whole thing (and safer - deals with whitespace in filenames and directory names).

Your script is probably failing for entries that don't have a . in their name, making $extension empty.

like image 112
Mat Avatar answered Sep 30 '22 07:09

Mat