Solved

Bash regex to match files that end with specific extensions

Posted on 2014-09-30
7
299 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now