Solved

Bash regex to match files that end with specific extensions

Posted on 2014-09-30
7
302 Views
Last Modified: 2014-09-30
I'm trying to iterate over the files in a directory and perform an operation on only the files with specific extensions. Unfortunately my bash-fu is weak today, can anyone help?

In this example, I'm looking for files that end with ".txt" or ".doc":

for myFile in $myDir
do
   if [[ $myFile =~ \.(txt|doc)$ ]] ; then
      echo $myFile 
   fi
done

Open in new window

0
Comment
Question by:Mark Olsen
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40353413
Why not use "find"

find $myDir -type f \( -name "*.txt" -o -name "*.doc" \)

If you don't want to recurse into subdirectories add "-maxdepth 1"

find $myDir -maxdepth 1 -type f \( -name "*.txt" -o -name "*.doc" \)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40353427
Or extended globbing?

shopt -s extglob
for myFile in $myDir/+(*.docx|*.txtx)
do
     echo $myFile
done

or short:

shopt -s extglob
ls -1 $myDir/+(*.docx|*.txtx)
0
 
LVL 3

Author Closing Comment

by:Mark Olsen
ID: 40353429
Using "find" did the trick. Thank you for the fast answer. Like I said, my bash-fu is weak today and I forgot about that command.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Expert Comment

by:tel2
ID: 40353433
I guess this is a bit late now...

#!/bin/bash

myDir=dir1

for myFile in `find $myDir -regextype posix-awk -regex '.*(txt|doc)'`
do
        echo $myFile
done

Open in new window


This gives entire file paths.  Not sure if that's what you wanted.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40353447
or simply

cd $myDir

for myFile in *.txt *.doc
do 
      echo $myFile 
done

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
ID: 40353465
Well said, Tintin.

Would that run into problems if there were too many matching files?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40353494
Would that run into problems if there were too many matching files?

Indeed it will.   In most cases, the globbing limits aren't an issue.   Depends on the nature of the task.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question