Skip to main content
added 363 characters in body
Source Link
Nemdr
  • 11
  • 1
  • 3

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

EDIT: This seems to work:

nrofjpg=$( grep -c '\.jpg$' <<< ${FILES[*]} )

the grep -c and '\.jpg$' seems to do what I want...

EDIT 2: Later in the code I am checking file extensions in a loop with a if statement, and files with spaces are seen as 2 files. This is not good, the double quotes around $f do not help.

for f in $FILES; do
    if [[ "$f" == *.jpg || "$f" == *.png || "$f" == *.jpeg || "$f" ==   *.bmp ]]; then
        ..code..
    fi
done

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

EDIT: This seems to work:

nrofjpg=$( grep -c '\.jpg$' <<< ${FILES[*]} )

the grep -c and '\.jpg$' seems to do what I want...

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

EDIT: This seems to work:

nrofjpg=$( grep -c '\.jpg$' <<< ${FILES[*]} )

the grep -c and '\.jpg$' seems to do what I want...

EDIT 2: Later in the code I am checking file extensions in a loop with a if statement, and files with spaces are seen as 2 files. This is not good, the double quotes around $f do not help.

for f in $FILES; do
    if [[ "$f" == *.jpg || "$f" == *.png || "$f" == *.jpeg || "$f" ==   *.bmp ]]; then
        ..code..
    fi
done
added 146 characters in body
Source Link
Nemdr
  • 11
  • 1
  • 3

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

EDIT: This seems to work:

nrofjpg=$( grep -c '\.jpg$' <<< ${FILES[*]} )

the grep -c and '\.jpg$' seems to do what I want...

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?

EDIT: This seems to work:

nrofjpg=$( grep -c '\.jpg$' <<< ${FILES[*]} )

the grep -c and '\.jpg$' seems to do what I want...

Source Link
Nemdr
  • 11
  • 1
  • 3

Counting file extensions in a string array

I have a $FILES array which consists of strings of 4 filenames:

FILES=$(ls /my/directory | shuf -n 4)

The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4

I want to count how many of each file types there are. Currently I am doing this:

nrofimg=$( grep -o .jpg <<< ${FILES[*]} | wc -l )
nrofgif=$( grep -o .gif <<< ${FILES[*]} | wc -l )

but this is not optimal, because if a file is called file.jpgfile.jpg, it will count it twice. What is a better error-proof precise way of counting how many of each file extensions there are in the array?