Solved

Bash regex to match files that end with specific extensions

Posted on 2014-09-30
7
297 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:tel2
Comment Utility
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
Comment Utility
or simply

cd $myDir

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

Open in new window

0
 
LVL 11

Expert Comment

by:tel2
Comment Utility
Well said, Tintin.

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

Expert Comment

by:Tintin
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

10 Experts available now in Live!

Get 1:1 Help Now