8

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 !

3 Answers 3

8

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.

2
  • <kbd>Tab</kbd> applies to the Windows command line as well.
    – Hind-D
    Jun 8, 2015 at 20:36
  • And bash /zsh a least on linux Jun 8, 2015 at 23:00
5

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
2

And have a look at Midnight Commander - a text mode file manager which is really great. You can navigate in two panels, once a file located a simple F5 copies it. F6 moves it... I use terminals a lot, if not most of the time, but I couldn't live without mc (the normal name of the package).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .