Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 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 '========'

or

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). –  Tor Thommesen Jun 14 '13 at 18:44
    
Ok. :) Thanks for giving so quick and thorough answers! –  Tor Thommesen 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. –  paraxor Jun 14 '13 at 18:53

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.