Home

Bash For Loop to List MP3 Files

A simple for loop in bash to print all .mp3 file names to a text file is:

for file in *.mp3; do
  echo "$file" >> music_list.txt
done

Here’s a breakdown of what each part does:

This script will go through every .mp3 file in your directory, print its name, and add it as a new line to the music_list.txt file.

Tags: Bash, Cli, Linux