I have a Music folder, in which I have some music tracks, which aren't organized too well. Thus, if I want to find a particular track, I usually type: ls Music | grep <keyword>, where <keyword> is some keyword I expect the filename to have. Then my command line will return the name of the file for which I am looking, <name>, and then to open the music file, I will type vlc <name> &.

My question is whether it would be possible to streamline all of this into one operation? I tried using ls Music | grep <keyword> | vlc, but this was unsuccessful. How would I go about doing this?

(More generally, how would I use the pipeline for this sort of purpose?)

2 Answers 2


You can use xargs for this. It will take what is piped in on stdin and use it as arguments to a subcommand. So in your case it might look like this:

ls Music | grep <keyword> | xargs vlc &

Now, this command sequence will probably still have some issues, notably whitespace. By default, xargs will split its input on any whitepsace, so if you had a file named like Artist Name - Track Name.mp3, then xargs will send 5 separate arguments to vlc: Artist, Name, -, Track, Name.mp3. Luckily there's a way around this. If you use the -0 option to xargs, it will use null \0 to split its input into arguments to the sub command. And as it turns out the find command supports writing out file name matches with a null separator (and find is a better tool for finding files than ls | grep anyway).

So this may be a better pipeline:

find Music -iname '*<keyword>*' -print0 | xargs -0 vlc &

While leaning how to use xargs is very useful, in this particular case it'd be better to just use a glob.

vlc Music/*keyword* &  ## will find all filenames containing the string 'keyword'

If you want recursiveness (that is: looking in subdirectories) then you could use the globstar shell option:

shopt -s globstar
vlc Music/**/*keyword* &
  • This is a better way of accomplishing this task. Thanks!
    – Newb
    Commented Nov 11, 2013 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .