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 $
I could also have used used an asterisk to represent all files to perform the check. E.g.:
$ for f in *; 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 $
Also, instead of typing everything on one line by separating the commands with semicolons, I could type them line by line and have the system prompt me for the next one with a greater than sign as shown below:
$ for f in * > 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 $
If I had all of the file names stored in a file named files.txt
,
I could also read in the filenames from that file line by line and process
them with
while loop. E.g., I could put the file names in files.txt
as shown below:
oleObject1.bin oleObject2.bin oleObject3.bin oleObject4.bin oleObject5.bin oleObject6.bin
I could then type the following commands to process each file listed
in files.txt
with a while loop:
$ while read -r file; do ~/Documents/bin/filetype.py "$file"; done < "files.txt" Microsoft Office PowerPoint Microsoft Office PowerPoint Microsoft Office PowerPoint Microsoft Visio Microsoft Visio oleObject6.bin: CDF V2 Document, No summary info $