How to have a better view when typing out directories in terminal?

I am currently using a Mac and learning to use the terminal, but the question applies to Linux systems as well.

I would like to ask, when typing out paths in the terminal/command line, is there a way within the terminal to have a better view of directories ?

For example, I want to cd to a particular directory. Once I start typing the address, is there a way to basically ls at each directory ? If for example I want to copy a file from one directory to another and want to type 2 absolute paths, do I need to memorise 2 paths ? I know that pressing Tab helps with autocomplete, but what if I don't know the first letter ?

Right now I rely on using Finder for that, but I imagine people who use the terminal exclusively have a better way.

Thanks !


Solution 1:

Tab is precisely what you need. If you hit tab twice, it will show you all possible completions:

$ cd /usr/  ## Hit tab twice here
bin/     include/ lib64/   sbin/    src/     
games/   lib/     local/   share/   var/     

You don't need to know the first letter, the second tab will show you everything.

Solution 2:

Some words more about Tab

Tab is great to complete a partial written command (or an option) among the available ones as just underlined by Terdon in another answer.

To complete a command is enough to press Tab once. To have the full list press Tab again.
Moreover you may like to custom the behaviour of Tab. Try for example those 2 lines:

bind "TAB:menu-complete"
bind "set show-all-if-ambiguous on"

The first Tab shows you all the possibilities and autocomplete the command with the first available, from the second time you press Tab it will start to cycle among them.

Notes:

You can write your own bash completion scripts, with rules options...
There are more than some hints in the article An introduction to bash completion: the 1st part is for the bases and the second to write your own script.

You may find interesting bind complete and the references here below.

2nd Answer: Absolute paths and shortcuts

It is not always mandatory to write the absolute path (full) in each command even if is a good practice when you write a script and you want to avoid the risk of exploit as trojan horses.

There are some shortcut to go faster as, for example, Tab, Alt+., ., ~ and a all the variables defined in the current shell.

Here below some examples (. means here and ~ means home):

To copy a file to the current directory is enough to write cp /my/far/far/away/myfile .
To copy from your home directory a file to the present directory you can write cp ~/myfile .
To copy a file from the current directory to the last one cp myfile $OLDPWD
Alt+. writes the last argument of the last command stored in the history. If pressed again it will substitute the one just found with the one of the previous position in the bash history. With an example is more simple:

mkdir OldDir
mkdir NewDir
cd # here press `Alt + .` and it will autocomplete with NewDir
   # press `Alt + .` again and you will see "cd OldDir"

References:

  • An introduction to bash completion: part 1 and part 2
  • help bind, yes help because bind is a built in command
  • help complete and complete -p | less to have a list in a cosy format.
  • help pushd and help popd to push and pop a directory from the stack.
  • Programmable Completion Builtins