-2

I'm trying to write a bash script that will search a folder for specific file extensions (lets say .txt and .csv). The folder can have thousands of each type of extension. If the folder has only these two extensions across all files then the script can proceed. If any other extensions are present (and the two allowed extesions may also be preset), the script copies the folder to a holding bucket and writes to a log file. The folder that is being searched will never have subfolders.

So, if the folder has: .txt and .csv, this passes If the folder has: .mp3, this fails If the folder has: .txt, .csv and .mp3, this also fails.

Thanks!

  • 2
    What error were you getting? Please post the error message so we can help you debug your code. – alvits Mar 11 '16 at 21:29
  • Try enabling `extglob` sh option with `shopt -s extglob` and listing the inverse of `*.txt` and `*.csv` with `ls !(*.txt|*.csv)` and finally checking for exit status `echo $?`. – alvits Mar 12 '16 at 00:45

2 Answers2

1

here is a prototype to get you started

$ diff -q <(for f in *; do echo "${f##*.}"; done | 
  sort -u) <(echo -e "csv\ntxt") > /dev/null 2>&1

create a sorted list of extensions for a given directory compare with the given sorted list and fail if differs. You have to check the returned status (0 pass 1 fail). Note that it doesn't take care of subdirectories or files without extensions.

karakfa
  • 66,216
  • 7
  • 41
  • 56
1

Umm, why not:

\ls | grep -v -e '\.txt$' -e '\.csv$' | wc -l

As in:

if [ `\ls | grep -v -e '\.txt$' -e '\.csv$' | wc -l` -gt 0 ]; then echo "Folder has files other than .txt, .csv"; fi
anonymouse
  • 11
  • 1