views:

762

answers:

3

Scenario :

A folder in Linux system. I want to loop through every .xls file in a folder.

This folder typically consists of various folders, various filetypes (.sh, .pl,.csv,...).

All I want to do is loop through all files in the root and execute a program only on .xls files.

Please help!

Thanks guys :)

Edit :

The problem is the program I have to execute is 'xls2csv' to convert from .xls to .csv format. So, for each .xls file I have to grab the filename and append it to .csv.

For instance, I have a test.xls file and the arguments fro xls2csv are : xls2csv test.xls test.csv

Did I make sense?

+5  A: 

Look at the find command.

What you are looking for is something like

find . -name "*.xls" -type f -exec program 

Post edit

find . -name "*.xls" -type f -exec xls2csv '{}' '{}'.csv;

will execute xls2csv file.xls file.xls.csv

Closer to what you want.

Tom
`find -maxdepth 1` to exclude subfolders. This also converts `test.xls` to `test.xls.csv` instead of `test.csv`. So not *quite* what OP asked for, but pretty close.
ephemient
+7  A: 

bash:

for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done
Ignacio Vazquez-Abrams
+1 for simplicity
Tom
Simply awesome! Works perfectly! Thank you so much!
ThinkCode
A: 
find . -type f -name "*.xls" -printf "xls2csv %p %p.csv\n" | bash

bash 4 (recursive)

shopt -s globstar
for xls in /path/**/*.xls
do
  xls2csv "$xls" "${xls%.xls}.csv"
done
ghostdog74