?
Solved

Bash regex to match files that end with specific extensions

Posted on 2014-09-30
7
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

801 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