1

I'm currently using the Linux command line and was just wondering whether there is a quick command you can enter into the console to open any of a given directory.

I'll give you an example of what I mean.

say in a directory ligands/

we have:

    ligand_1993324
    ligand_1993444
    ligand 1993255
    shoe_lace
    water_bottle

Lets just say there are 100000 of these very similar directories. Because I'm lazy I just want to pick any random one of these, but it has to begin with ligand_199 for example.

Please not I'm trawled through the manual and can't find anything, I've also looked at other stacks, any help would be great!

Small Legend
  • 733
  • 1
  • 6
  • 20
  • 1
    what does "open a file" mean for you? open a text file with some editor? – Yves Jun 10 '16 at 09:41
  • I give you what I know: if you want to open files from the console, you can use some linux commands, ofc you can make a shell with the commands to do that. If you want to open files with some editor so that you can read them from desktop(just like double click on them), I know that in Mac OS system it's very simple because we have applescript, but for linux, I don't know. Maybe google: Is there an equivalent to AppleScript for ? – Yves Jun 10 '16 at 09:45
  • you see, I use mac and I usually listen some music. What I do is that: I create a folder, put all of music in it and create an applescript, something like this: `open /myapp/musicplayer /mymusic/*`. With this script, I just execute the script when I want to listen music. Maybe this is what you need but I dont know any equivalent way for Linux. – Yves Jun 10 '16 at 09:51
  • Linux would use a bash script. It's the exact same syntax but with `xdg-open` instead of `open` – timlyo Jun 10 '16 at 10:04

3 Answers3

1

Maybe something like

number=$(((RANDOM%10000)+1)) && emacs -nw "ligand_199$number" ?
Erik Alapää
  • 2,585
  • 1
  • 14
  • 25
1

You can use following:

files=(/my/dir/*)
file=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`
cat file
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
1

There are a couple of versions of a program called variously "randomline" or "randline" about. This version shows its age (it's in Perl).

#!/usr/bin/perl

while(<>)
  {
  push @lines, $_;
  }

$randline = $#lines;
$randline = rand($randline);
print $lines[$randline];

Given this in a file ~/bin/randomline, then your task reduces to the following, assuming that you want to open the file with vim:

vim $(ls ligands/ligand_199* | ~/bin/randomline)
vielmetti
  • 1,864
  • 16
  • 23
  • There's another version of "randline" in C from scs@eskimo: https://www.eskimo.com/~scs/src/#randline – vielmetti Jun 14 '16 at 10:46
  • And indeed there's a whole bunch of random line programs mentioned here http://stackoverflow.com/questions/448005/whats-an-easy-way-to-read-random-line-from-a-file-in-unix-command-line?lq=1 including `shuf` from GNU coreutils which might already be on your system; if it is then `randomline` is just `shuf -n 1`. – vielmetti Jun 15 '16 at 05:58