-2

Suppose I'm at a terminal in Linux/UNIX and I run ls and see the following displayed:

my_file_1.txt my_file_2.txt

Now I want to open the first one in gedit, or maybe it's a folder and I want to enter it.

Is running gedit my_file_1.txt the fastest way to do this? Suppose the file has a long and awkward name, is there any shortcut along the lines of gedit ls[0] - where I'm asking gedit to open the first item in the list returned?

If there's not, I won't lose any sleep over it, but if such a shortcut exists I'd make use of it, especially when dealing with lengthy file and folder names.

pminogue
  • 143
  • 1
  • 1
  • 10
  • 3
    You could always start typing the file name and press tab for autocomplete – Hevlastka Feb 12 '14 at 23:08
  • Thanks I feel a little silly for overlooking that! I thought autocomplete was more modern than such terminals - learn something new every day. – pminogue Feb 12 '14 at 23:19

2 Answers2

0

You could do this:

gedit "$(ls | head -1)"

But probably using filename completion or cutting and pasting with the mouse will be easier.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks - this is exactly what I was looking for. Autocomplete will do the job but I was also curious as to how to achieve it via command. – pminogue Feb 12 '14 at 23:16
0

I haven't seen anything that indexes the results of ls.

What you could do is the auto-completion feature that is built in. For example if your file name is very long you can just start writing a little bit of the file name and hit 'Tab' until you find the file you need.

diaz994
  • 354
  • 3
  • 13
  • Note it would be trivial to actually "index the results of ls", e.g. by overriding `cd` to both `cd` and index the results of `ls` into some custom environment variable, but there really is no reason to do this since tab-complete is going to be faster and it's hard to keep track of order. – Reinstate Monica Please Feb 12 '14 at 23:22
  • True, i think the OP realized that. – diaz994 Feb 12 '14 at 23:25