Here is my script, as simple as possible:


for file in /home/pi/watch-dir/*.torrent; do
   echo "$file"
   sleep 2

Base on this answer How to iterate over files in a directory with Bash? I believe the for-in should see /home/pi/watch-dir/*.torrent as a list.

Inside the watch-dir directory:

pi@raspberry:~/watch_dir $ ls -al
total 68
drwxrwx---  2 pi pi  4096 Sep 15 18:59 .
drwxr-xr-x 12 pi pi  4096 Sep 15 19:03 ..
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 asdf.torrent
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 qwer.torrent
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 zxcv.torrent

And this is the only-one-line-output after run the scipt, I really don't know what I am doing wrong...

pi@raspberry:~ $ ./test.sh

2 Answers 2


You've got watch-dir in your loop and watch_dir is the folder!

- != _
  • 1
    The loop 'watch-dir' contains a minus character. The actual folder 'watch_dir' contains an underscore character.
    – CoderMike
    Commented Sep 15, 2017 at 12:30

This is obviously nothing related to Raspberry Pi but to Linux and Bash scripting.

The for-in operator operates on a space separated (line separated? You can change it with IFS=<your separator>) lists.

IFS=" "
for x in alice bob cindy d e f; do
  echo $x

will output


There is no globbing or thatsoever. So instead of your glob you will have to get a listing of the actual files. Something like $(find . -type f -name "*.torrent")

So you should be able to achieve your intent with


for file in $(find /home/pi/watch-dir -type f -name "*.torrent"); do
   echo "$file"
   sleep 2

$(ls *.torrent) should also work if you run the script from within the same directory or change into the dirctory within your script.

  • Base on this answer link I think the for-in should see /home/pi/watch-dir/*.torrent as a list right?
    – Aura
    Commented Sep 15, 2017 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.