MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
September
Sun Mon Tue Wed Thu Fri Sat
         
7
2017
Months
Sep


Thu, Sep 07, 2017 10:38 pm

Looping over a set of files in the Bash shell on Linux or OS X

I have a Python script, filetype.py, that takes a file name as a parameter. I use the script to identify if a file with a .bin extension is actually a Microsoft Visio or PowerPoint file. I run the script on an OS X system from a Terminal window where I have a Bash shell prompt. E.g.:

$ ~/Documents/bin/filetype.py oleObject1.bin
Microsoft Office PowerPoint
$

But I often want to have it check all of the .bin files in a directory. You can loop through all files in a directory or a set of files using a Bash for loop. E.g., to check all of the .bin files in a directory I could use the for-loop below:

$ ls *
oleObject1.bin	oleObject3.bin	oleObject5.bin
oleObject2.bin	oleObject4.bin	oleObject6.bin
$ for f in *.bin; do ~/Documents/bin/filetype.py $f; done
Microsoft Office PowerPoint
Microsoft Office PowerPoint
Microsoft Office PowerPoint
Microsoft Visio
Microsoft Visio
oleObject6.bin: CDF V2 Document, No summary info

$

[ More Info ]

[/os/unix/bash] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo