Linux Shell Script For Each File in a Directory Grab the filename and execute a program

后端 未结 4 791
耶瑟儿~
耶瑟儿~ 2020-12-07 08:14

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

相关标签:
4条回答
  • 2020-12-07 08:57

    bash:

    for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done
    
    0 讨论(0)
  • 2020-12-07 09:07

    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.

    0 讨论(0)
  • 2020-12-07 09:08
    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
    
    0 讨论(0)
  • 2020-12-07 09:19
    for i in *.xls ; do 
      [[ -f "$i" ]] || continue
      xls2csv "$i" "${i%.xls}.csv"
    done
    

    The first line in the do checks if the "matching" file really exists, because in case nothing matches in your for, the do will be executed with "*.xls" as $i. This could be horrible for your xls2csv.

    0 讨论(0)
提交回复
热议问题