Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As it turns out the reason for this problem was one filename with several newlines. (No idea how that happened.)

find ~/x/y/ | shuf > ~/Desktop/z.txt

This command works pretty much as expected except some empty lines appear in the "z.txt" file. This is inconvenient because the next step is to pick the uppermost file and open it, an empty line would obviously be useless. I'm curious why this happens and if there is some "standard" solution to the problem (some option?), or, alternatively, some robust and simple solution (an extra command to delete empty lines?).

share|improve this question
linux.die.net/man/1/shuf – slm Jun 14 '13 at 18:51
up vote 1 down vote accepted

shuf doesn't add empty lines, so they must come from the output of find. This indicates that there are files whose name contains two consecutive newlines somewhere in that directory. You can look see what they are with the command

find ~/x/y/ -name '*
*' -print -exec echo '========'


find ~/x/y/ -name '*
*' -exec ls -q

To cope with file names containing newlines with your shuf command, use a null byte as the separator.

find ~/x/y/ -print0 | shuf -z
share|improve this answer

I am not sure why blank lines are appearing in your output file, but you can try filtering out blank lines before writing to the file.

find ~/x/y/ | shuf | grep -v '^$' > ~/Desktop/z.txt

In order to track down the source of the blank lines, try running find by itself.

find ~/x/y/ > ~/Desktop/z.txt

Do blank lines appear in z.txt? If so, it is likely that they are being created by find.

You can use zero-terminated items instead of newline-terminated items. When you select the first item from the list, just use the text from the first character to the first null character.

find ~/x/y/ -print0 | shuf -z > ~/Desktop/z.txt
share|improve this answer
Ah, yes, for some reason a file name with lots of newlines has been accepted in my old osx files (I don't think it normally does that). – user36719 Jun 14 '13 at 18:44
Ok. :) Thanks for giving so quick and thorough answers! – user36719 Jun 14 '13 at 18:51
In case you are unable to rename the file, I have added a method for working with the file to my answer. – Evan Teitelman Jun 14 '13 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.