user3447273
user3447273

Reputation: 361

How do you exclude files from a search using a file list

I am building a “m3u” playlist using an iterative bash script. Once the m3u file has tracks listed in it, how do I use that file as a filter to exclude existing tracks when appending?

As you may already know, an m3u playlist is simply a list of file pointers.

For example, let's say you have "myplaylist.m3u" with these songs in it:

/path/a_fine_frenzy/bomb_in_a_birdcage/06_-_a_fine_frenzy_-_swan_song.mp3
/path/a_fine_frenzy/one_cell_in_the_sea/06_-_a_fine_frenzy_-_almost_lover.mp3
/path/a_fine_frenzy/bomb_in_a_birdcage/07_-_a_fine_frenzy_-_elements.mp3
/path/a_fine_frenzy/one_cell_in_the_sea/07_-_a_fine_frenzy_-_think_of_you.mp3

You now want to continue appending "myplaylist.m3u" with a different search that might include the above songs, but you do not want them added again. So how do you exclude the files listed in future searches?

Your search might normally be something like:

find /home/username/Music/ -type f -iname "*.mp3" >> myplaylist.m3u

Upvotes: 1

Views: 117

Answers (1)

janos
janos

Reputation: 124804

Filter the results of find before appending, using grep:

find /home/username/Music/ -type f -iname "*.mp3" | grep -vxf myplaylist.m3u >> myplaylist.m3u

Explanation of the flags:

  • -f file use patterns from file
  • -x match entire lines
  • -v invert the logic: print lines that don't match, instead of lines that match

As a result, grep will output only lines of find that are not yet in the file.

Upvotes: 1

Related Questions