Moseleyi
Moseleyi

Reputation: 2859

Bash - Looping through files in a directory

I know there are lots of questions like that but for the love of all the gods I can't get it to work or I am missing something completely obvious, in that case please forgive..

Simple directory, and I want to call the name of the file and mime type of each:

for i in "attachments/*"
do
     echo $i $(file --mime-type -b $i)
done

Why does my output look like:

attachments/query.sql attachments/script.sh text/plain text/x-shellscript

when I expect:

attachments/query.sql text/plain attachments/script.sh text/x-shellscript

Upvotes: 1

Views: 52

Answers (1)

sat
sat

Reputation: 14949

You don't need quotes around /tmp/attach/*.

for i in /tmp/attach/*
do
     echo "$i : $(file --mime-type -b $i)"
done

When you use quotes, then you are passing /tmp/attach/* to file command, not just one by one filenames.

Upvotes: 4

Related Questions