Linux Commands Examples

A great documentation place for Linux commands


change directory


cd [directory path]

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!


cd    # change directory to your home - same as : cd ~

cd -    # go back to the directory you went just before changing directory

cd .. #change directory to the upper directory

cd ../.. #go up 2 levels

cd / #go to root directory

cd ~/.mozilla/ # go toward your firefox (or other Mozilla's product) profile directory
example added by LeBerger
cd lights;./arc;cd ../

Smart tab completion (for directories)?

You can enable menu-complete in Bash to step through the entries on the command line each time you press Tab. This is not really what you're looking for. If you want to try it, do this at the command prompt:

bind '"\C-i": menu-complete'

To make it persist, add this to your ~/.inputrc file:

"\C-i": menu-complete

Zsh has a feature that allows you to use the arrow keys to select an entry. Add this (or another variation) to your ~/.zshrc file:

zstyle ':completion:*' menu select=0

See man zshcompsys and search for "select=" (it will be in the section for the menu "Standard Style") for more information.

Otherwise, in Bash, you could write a function (or even create your own completion function) that would do something based on the select command. This is extremely simplistic:

$ select a in x y z; do cd $a; done
1) x
2) y
3) z

You'd have to flesh that out a lot to get it to do what you want.


Directory "Bookmarking" in Linux

I use one-letter variables for temporal bookmarking, and properly named symlinks for permanent storage.


The CD command with "-" argument will change to previous directory. Is there a way to know which directory it is?

Yes, it is $OLDPWD.

~$ cd src/
~/src$ cd ..
~$ echo $OLDPWD


How to minimize typing when frequently changing directories?

  1. Try using the hist command to bring up the history of all commands you have executed.
  2. Create a shell script with the cd commands for each directories that you often cd to.

bash: Is there a way to use tab completion through out CDPATH?

You should install and enable bash-completion — it defines a _cd function and sets it as the completer for the cd command, which overrides the default completion and includes $CDPATH.


'cd' command in terminal - using partial folder name

If you know that the name is unique after a few typed letters, you can do – for example to go to the folder "FooBarBaz":

cd Foo*

The * glob will expand to the name of all folders starting with Foo, and cd will go to the first folder found.

The same works the other way around, or if the part you know is in the middle of the folder name:

cd *Baz
cd *Bar*

This probably requires the least keypresses. You can just press Enter and it'll expand automatically.

You can get even shorter if you set the autocd option, assuming you use Bash, with shopt -s autocd. It requires you only to type the directory name without cd to have the shell cd to it. For example:

shopt -s autocd

Of course, you can add this option to your shell's configuration file to have it loaded automatically (e.g., ~/.bash_profile or ~/.bashrc, depending on what you use).


"cd -" equivalent on Windows?

popd, which is to be used alongside pushd.


How do I `cd` and then* `ls` automatically in linux?

Try this:

cd() { builtin cd "$@" && ls; }

builtin makes the cd inside the function invoke the builtin cd command rather than trying to call the function recursively.


cd Command Linux and Mystery Flags

  1. cd - is not the same as cd ... The cd - command goes to the previous directory you were in. For example, if you're in /home/bob and then you run cd /var/log, then running cd - will take you back to /home/bob. cd .. on the other hand, always goes to the parent directory.
  2. If you want to keep a history like that, I suggest checking out pushd and popd which will let you use a "directory stack":

    user@host:/etc/init.d$ pushd /var/log
    /var/log /etc/init.d
    user@host:/var/log$ pushd /tmp
    /tmp /var/log /etc/init.d
    user@host:/tmp$ popd
    /var/log /etc/init.d
    user@host:/var/log$ popd

    As for stepping back multiple levels in the directory tree, there's no common way that I know of. .. isn't a feature of the shell, it's actually a special directory link in *nix that refers to the parent dir.

  3. cd is a built in command, so it's flags and usage may vary by shell. But, in general the options to cd in bash on one system ought to be the same as on another system.

  4. My bash shell doesn't list either of these flags... are you experimenting using bash or tcsh? If you're using tcsh you're essentially using a totally different command, so you probably want to run bash and mess with cd there.


According to the tcsh man page:

With -p, prints the final directory stack, just like dirs. The -l, -n and -v flags have the same effect on cd as on dirs, and they imply -p.

(You can check the section about dirs if you want to read the specifics). Looks like it's basically making it print the directory stack after the command runs:

host:43> pushd /etc/init.d
/etc/init.d ~ 
host:44> pushd /var/log
/var/log /etc/init.d ~ 
host:45> pushd /tmp
/tmp /var/log /etc/init.d ~ 
host:46> cd -p
~ /var/log /etc/init.d ~ 
host:47> cd -v
0       ~
1       /var/log
2       /etc/init.d
3       ~
host:48> popd
/var/log /etc/init.d ~ 
host:49> cd -v
0       ~
1       /etc/init.d
2       ~

both show the directory stack, but -v seems a little easier to read. The top of the stack (element 0) is the current directory, and 1 is where you'd go if you execute popd once, and so on.


cd ~ dumps me in a seemingly empty directory

~ is your home directory. For the root directory, use cd /.

To find out where exactly cd ~ sends you, enter pwd afterwards.

If you want to go to your home directory, you can use cd without arguments.

It's possible there are (hidden) files in the directory you enter. Use ls -lA to list them.


ssh and cd into current directory

Answered on Stack Overflow.

ssh -t "cd /directory_wanted"

You could add this into a shell script:

ssh -t "cd \$PWD; bash"


Upstart: chdir stanza VS cd command

In Upstart, you can have more than just one type of script. For example you can have pre/post start and pre/post stop scripts as well. The chdir stanza sets a default directory for ALL scripts. The cd command will only change the working directory within the scope of the current script.


navigate back and forward using cd

You can use pushd and popd

A small tutorial on the subject.


The cd command using variable to mapped NFS volume within ssh in linux script is not working

Does it work when you manually change the directory on the command line? If not, it could be that you simply don't have the permissions to see that directory.

Other than that, it seems you're using an incredibly complicated way to change a directory. What's wrong with

ssh -2 root@  /bin/sh  <<EOF
cd "/vmfs/volumes$(grep -m 1 / vmvar_file)"

Also, you might consider double-quoting all your variable assignments, in case they result in values with spaces. Talking of spaces, if your $esxi_vmfile_path contains spaces, it might also try to change to the wrong directory.

How can this site be more helpful to YOU ?

give  feedback