30

I am trying to write a simple loop that will loop through the files the current directory and just print the file names.

I tried that:

#!/bin/bash    
FILES=/Users/nick/Desktop/*.jpg
    for f in $FILES
    do
        echo $f
    done

but it didn't work. Running ./script it just printed "/Users/nick/Desktop/*.jpg". No errors

I am running this on a MacBook Pro 10.10.4

Thanks

NikosDim
  • 1,398
  • 2
  • 21
  • 40
  • What do you mean by "didn't work"? Did it print anything? Did you get an error? How did you save the script, how did you run it? – choroba Aug 03 '15 at 22:39
  • @choroba I updated the question – NikosDim Aug 03 '15 at 22:44
  • 6
    @NikosDim if you see "/Users/nick/Desktop/*.jpg" as output, it's likely because that pattern didn't match anything. What happens if you just "echo /Users/nick/Desktop/*.jpg" (w/o the quotes)? This behavior is kind of a 'gotcha' in bash, since $FILES still evaluates to the glob pattern even if no files were actually found. A common thing to do is add an 'if [ -e $f ]; then ... fi' test around your for loop's body to handle the glob failing to match any files. Can you confirm if there are any .jpg files in your ~/Desktop? – Kyle Burton Aug 03 '15 at 23:09
  • possible duplicate of [How to iterate over files in directory with bash?](http://stackoverflow.com/questions/20796200/how-to-iterate-over-files-in-directory-with-bash) – l'L'l Aug 04 '15 at 02:31
  • Got it working now. Not sure what I was doing wrong. There were definitely jpg files in Desktop. Echo is exploding as expected. – NikosDim Aug 04 '15 at 12:54

4 Answers4

66
for f in /Users/nick/Desktop/*.jpg; do echo $f; done

Edit Actually, I think that this comment of @KyleBurton is very clever and should be taken into account since it explains why a result like OP's could be observed.

Leonid Usov
  • 1,508
  • 12
  • 16
4

Try this, please:

for f in $(ls /Users/nick/Desktop/*.jpg); 
do echo $f; 
done
gravity
  • 2,175
  • 2
  • 26
  • 34
Abdul
  • 51
  • 1
1

Single/one line based solution, (to use/run in Terminal shell):
find "./" -not -type d -maxdepth 1 -iname "*.jpg" -print0 | while IFS= read -r -d $'\0' fileName ; do { echo "$fileName"; }; done; unset fileName;

for your/OP's case, change the "./" into "/Users/nick/Desktop/"

To use in a script file:

#!/bin/bash
find "./" -not -type d -maxdepth 1 -iname "*.jpg" -print0 | while IFS= read -r -d $'\0' fileName ; do {
    echo "$fileName";
    # your other commands/codes, etc
};
done;
unset fileName;

or, use (recommended) below codes as script:

#!/bin/bash
while IFS= read -r -d $'\0' fileName ; do {
    echo "$fileName";
    # your other commands/codes, etc
};
done < <(find "./" -not -type d -maxdepth 1 -iname "*.jpg" -print0);
unset fileName;

Please checkout my other answer here for description of what code does what function.
As i have shown link to a description, i can avoid repeating same in here.

atErik
  • 923
  • 2
  • 13
  • 24
-1

You can use simple find command to get all things, with type file ..

find . type f
Manikandan
  • 1,234
  • 11
  • 18
  • The OP did not ask for a way to find files. He wants to simply increment his logic upon the base logic which is looping through the directory. Your answer does not help in anyway. – Shayan Ahmad Jan 07 '22 at 00:24