Search code examples
bashshellunixlscd

How to ls/cd using indexes rather than names


So my normal way of moving around the file system is using ls --color to see all the files and directories and then typing in cd [directory] and then typing ls --color again and rinse and repeat until I find a file, where after I type vim [file] to edit it.

This is a pretty inefficient way to do things IMO. I was envisioning these features:

  1. Combine cd and ls --color so that as soon as I cd into a directory it outputs the contents with color (so I can tell whats a directory and what's a file and etc)
  2. Have ls and cd use indexes. What I mean is have the output of ls print numbers next to all the stuff in a directory like [3] for the 4th item or something so that all I have to do is type cd [3]. This would help because the file names are sometimes long (like really long) and contain spaces and I don't wanna retype the long command because of inevitable typos.

I'm not sure if something like this is already in bash, or if it's a plugin I can download or if I'll just need to write a shell script, but any help would be great!

Edit: I should add this for exploring the filesystem. I don't have a target file I'm trying to reach.


Solution

  • There are multiple ways to browse a filesystem that could be more efficient.

    1. Use your shell's autocompletion feature. Probably the most efficient way to solve your problem.

      cd /a/TABTAB

      This will display the directories contained in /a/ or, if there's only one (that corresponds to what you've typed so far) will autocomplete your command with it.

    2. Use a GUI, as suggested in the comments.

    3. Instead of alternatively using ls and cd, you could use multiple ls and a final cd :

      $ ls /a  
      > b/ c/ d/  
      $ ls /a/b  
      > e/  
      $ ls /a/b/e  
      >fileIWant  
      $ cd /a/b/e  
      $ vim fileIWant
      

      You can repeat the path of the last step using ALT + ..
      You don't even need the final cd and could directly use vim /a/b/e/fileIWant.

    4. Use ls recursively : ls -R /a. The output can be a little hard to use.