Saving filenames that are played currently
Since the play command terminates after playing a single file, what we can do is instead of using xargs and giving play a batch of files, we'll take out each file, one at a time, and echo it to file, and play the file afterwards. The edited script would look like below. Notice that here are added additional options and IFS= read -d'' -r command to deal with filenames in safe manner.
#!/bin/bash
# create file for playing songs first
echo > playlist.txt
# Now play each song and echo filename to file
find ./ -type f -print0 | sort -z -R | while IFS= read -d '' -r filename
do
clear
echo "$filename" >> playlist.txt
play "$filename"
done
The advantage of this approach is that filenames will go into playlist.txt as they are played, which allows us to track output of the script in real time with something like tail -F playlist.txt.
NOTE: to avoid playlist.txt being listed in find's output change find command like so:
find ./ -type f -not -name "playlist.txt" -print0
Additionally if we want to ensure that only .mp3 files are listed we can do this:
find ./ -type f \( -not -name "playlist.txt" -and -name "*.mp3" \) -print0
Saving list of found files to be played
If our goal is to safe the file list before it is played, there's not a lot of science to it - the find-sort pipeline can be written to file first, and that file can then be fed to play either via xargs or again via while IFS= read -r ; do ... done structure
#!/bin/bash
find ./ -type f -print0 | sort -z -R > playlist.txt
while IFS= read -d '' -r filename
do
clear
play "$filename"
done < playlist.txt