16

I can't get --random-sort to work with the sort command on a Fedora Linux-system.

Some context information:

$ cat /etc/fedora-release
Fedora release 7 (Moonshine)
$ which sort
/bin/sort
$ man sort | grep -A 2 '\-R'
       -R, --random-sort
              sort by random hash of keys

$ man sort | grep -A 3 '\-R'
       -R, --random-sort
              sort by random hash of keys

       --random-source=FILE

And the test:

$ echo -e "2\n1\n3\n5\n4"
2
1
3
5
4
$ echo -e "2\n1\n3\n5\n4" | sort -r # Sort in reverse order
5
4
3
2
1
$ echo -e "2\n1\n3\n5\n4" | sort -R # Sort in random order
1
2
3
4
5
$ # Fail! That's not random (I've tried it multiple times.)
  • works as expected in Ubuntu Ibex - so seems like a problem specific to your version – Ken Feb 11 '09 at 15:07
  • PCBSD (FreeBSD) doesn't have the -R or alternate random option. Cygwin, Fedora Core 10 and Ubuntu's versions all work. Perhaps more info, like the exact OS version and package version? – Godeke Feb 11 '09 at 15:15
  • You have the exact OS version above, or do you need something more? I'm at home now and it works on Ubuntu 8.04. I'll provide the package version tomorrow if I can find it (I'm not that good with RPM and YUM). :-) –  Feb 11 '09 at 23:58
  • 1
    It must have been a faulty installation in school. –  Mar 10 '09 at 20:58

4 Answers4

19

It works on my Ubuntu 8.04 machine. Maybe the problem is the source of randoms. From the manual:

--random-source=FILE

         get random bytes from FILE (default /dev/urandom)
# this should give always the same result:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/zero

# this should be random:
echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/urandom
Community
  • 1
  • 1
Joao da Silva
  • 7,353
  • 2
  • 28
  • 24
  • As default is supposed to be /dev/urandom it shouldn't matter. I tested it, and it didn't matter. It was worth a try though! –  Feb 11 '09 at 17:28
10

From the GNU coreutils manual:

Sort by hashing the input keys and then sorting the hash values. Choose the hash function at random, ensuring that it is free of collisions so that differing keys have differing hash values. This is like a random permutation of the inputs (see shuf invocation), except that keys with the same value sort together.

If multiple random sort fields are specified, the same random hash function is used for all fields. To use different random hash functions for different fields, you can invoke sort more than once.

GNU sort -R takes the same input and lumps it together; This thread may offer some alternatives: How can I randomize the lines in a file using standard tools on Red Hat Linux?

Community
  • 1
  • 1
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
2

I don't know if bash works this way, but in ksh there's the "whence" command which tells you exactly what will execute if you were to type the argument as a command, whereas "which" just tells you the first instance of the command in $PATH. For instance:

wembley 0 /home/jj33 > which ls
/bin/ls
wembley 0 /home/jj33 > whence ls
'/bin/ls -FC'

I doubt it's your problem, but a next troubleshooting step would be to specify the exact path (or escape a possible alias with a backslash) for "sort" when you execute it:

$ echo -e "2\n1\n3\n5\n4" | /bin/sort -R

After that, I might suspect an environment or locale setting that's making it wonky. Not necessarily important, but the LC_* variables often have unexpected side effects (the first thing I do on a new box is set LC_ALL=C to turn it all off =)).

jj33
  • 7,543
  • 2
  • 38
  • 42
  • Also keep in mind that sort might be a shell built-in – Magnus Hoff Feb 11 '09 at 16:06
  • From man which "searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash". I don't have whence. Exact path gives same result. And LC_ALL=C didn't help. Lots of nice suggestions though! But no dice. :-( –  Feb 11 '09 at 17:32
  • bash's version of `whence` is `type`: `$ which echo -> /bin/echo` but `$ type echo -> echo is a shell builtin` – sarnold Jul 11 '10 at 21:35
-1

I know this is an old post, but this solution works very well... just in case someone is looking

dir='/home/path-to-folder'
cd $dir
file=`ls |sort -R |tail --lines=1` 
echo $file
qqx
  • 18,947
  • 4
  • 64
  • 68
nuntius
  • 23
  • 1