Lets say I have multiple files in a directory which all start with the same string

$ ls
  fileAA234 fileAA247 fileAA255 fileAA264
  fileAA269 fileAA277 fileAA285 fileAA294

Often, when browsing such data files in the command line, I just want to read one, anyone, of these files to see how they are structured.

But if I enter

$ less f 

And then hit tab for completion the answer will be

$ less fileAA2
  fileAA234 fileAA247 fileAA255 fileAA264
  fileAA269 fileAA277 fileAA285 fileAA294

Which doesn't get me all that much closer to open one of the files.

So for now I either use the mouse to mark a random file name and copy paste it to the prompt, or I lean forward and have my poor eyes figure out a unique string to finish typing the filename.

Is their an easy solution for this? I'm just missing something obvious, right? I'd really like to have something like tab+enter to auto complete and open the first file in the list, like in this case fileAA234.

Thanks for any help. I imagine there is a way to build a shell script by combining some tools like ls and head but I'm still a beginner and haven't found a solution myself yet.


Just use a wildcard (*):

$ less fileAAA*

That will open the first file in less. When you get to the end of the file, less will continue to the next one. If all you want is a quick look at any one file, and always opening the first (alphanumerically sorted) one is ok, just hit "q" to exit less as soon as you have seen enough.

  • Oh boy, i knew there must be an obvious solution. Thanks a lot! Also works well for directories! – user1323995 Aug 23 '12 at 12:46

For bash, put the following in ~/.inputrc:

"\C-i": menu-complete

This will cause Tab to always complete full names; press it again to switch between completions

  • Thank you, thats really cool. But now, when I enter $ cd dir/ and hit tab I won't see the list of subfolders anymore. I have to press enter and use ls. – user1323995 Aug 27 '12 at 20:52
  • @user1323995: Then bind menu-complete to a different key instead of Tab. For example, \C-q for Ctrl+Q, or \e[Z for Shift+Tab. – user1686 Aug 27 '12 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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