How can I play all MP3 files in a folder using a bash script?

0
0
Asked By CreativeCoder98 On

I'm learning bash and trying to enhance my project management script. One feature I want to add is a command that plays every MP3 file in a specific folder after I open my project in my IDE. I've written this loop:

```bash
for i in *.mp3; do
ffplay -nodisp -autoexit "/home/scottishcomedian/Music/bash_bullshit/$i"
done
```

When I execute it, I just get a blank console. What could I be doing wrong? Any advice would be appreciated!

5 Answers

Answered By FindItAll On

Try using the `find` command to avoid ambiguity with file matches. Run this snippet:

```bash
find /home/scottishcomedian/Music/bash_bullshit/ -type f -name '*.mp3'
```
This will give you a list of all the MP3 files which you can loop over directly from that output. Super handy!

Answered By PageFault On

First, make sure you’re running the script in the right directory. If your working directory doesn’t contain MP3 files, your script won’t find any. You can adjust your loop to specify the full path for the MP3s:

```bash
for i in /home/scottishcomedian/Music/bash_bullshit/*.mp3; do
ffplay -nodisp -autoexit "$i"
done
```
This way, it directly scans the target folder for MP3s.

EarlyLearner2023 -

Thanks! I think I was confused about the directory. I'll give that a try!

Answered By TechWizard22 On

If you're still running into issues, consider redirecting stderr to check for errors from ffplay. You can do something like this:

```bash
touch error_log.txt
for i in /home/scottishcomedian/Music/bash_bullshit/*.mp3; do
ffplay -nodisp -autoexit "$i" 2>> error_log.txt
done
```
This way, if any errors occur, they'll be logged in the error_log.txt file for you to review later.

LearningBash -

I have no idea what stderr means, but I'll look it up! Thanks for the advice.

Answered By ZeeKar On

You can also simplify things by not hardcoding the path inside the loop. Either use a parameter to pass the folder's location or switch to that directory first with `cd`:

```bash
(cd /home/scottishcomedian/Music/bash_bullshit && for i in *.mp3; do
ffplay -nodisp -autoexit "$i"
done)
```
This keeps your script clean and ensures it looks in the right place.

Answered By BassMasterFlex On

You might also want to check if there are actually MP3 files in that folder. Adding some debug lines could help. Try doing this before your loop:

```bash
echo "Expecting files in: /home/scottishcomedian/Music/bash_bullshit"
ls /home/scottishcomedian/Music/bash_bullshit/*.mp3
```
This will let you see if the files are there before you try to play them!

CuriousCoder -

Great tip! I'll make sure to check the contents before running the player.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.