I'm new to Linux. I wonder if it is possible to make terminal display directory contents automatically while I typing a command with directory arguments?

For example, if I want to do

cp ./fileA ~/folderA/folderB/folderC/fileA

Sometimes I can't memorise the destination directory correctly, as a result I need to use ls repeatedly to find the correct directory before finally using cp command, which is not convenient.

If I cannot remember what's in folderA beforehand, it'll be nice if the contents in folderA is automatically displayed while I typing:

cp ./fileA ~/folderA



The usual action in case you don't remember the name is to press Tab. Most shells (including bash, zsh, ksh) will guess as many characters as they could on the first keystroke, then display a list of matching files and directories on the second.

For example, if you have dir1, dir2 and dir3 in your home directory, then typing cp file ~/d and hitting Tab twice would produce

dir1    dir2    dir3
$cp file ~/dir

Here, your shell could guess from the letter "d" you have typed that you want one of the three directories mentioned above, and filled the common part ("dir") in your command for you. All you have to do is to type "1", "2" or "3", and hit Enter.

Tab can be used multiple times while typing the same command. If your target directory is buried deep in the directory three, or if there are many files/directories to chose from, it is convenient to type a few characters, hit Tab, check how many the shell could guess, type a few more, hit Tab again etc. Thanks @EightBitTony for the remark.

Note that command line competition using Tab also works with command names. cp is short enough to type entirely, but if you need something longer like wpa_supplicant then typing wpa_s and hitting Tab will save you a fair amount of keystrokes. Personally, I use zsh which is by default configured to guess command line options, e.g. typing service sshd r and hitting Tab is automatically expanded to service sshd restart.

  • 1
    ... and you can use completion more than once in the same command, so cp fileA ~/f then Tab then type A/ to get cp ./fileA ~/folderA/ then type /f and hit Tab again, rinse and repeat. – EightBitTony Mar 4 '16 at 12:05

Hit Tab key double time to complete the file or folder name while typing. That is feature of linux.

  • 4
    That's a shell feature. – Emmanuel Mar 4 '16 at 11:59
  • Which is also dependant on shell options, and while it's usually turned on in modern shell deployments, there's no guarantee. – EightBitTony Mar 4 '16 at 12:02

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.