File and Directory Commands

cd

The cd command changes directories. When you open a terminal you will be in your home directory. To move around the file system you will use cd.

  • To navigate into the root directory, type:

    cd /

  • To navigate to your home directory, type:

    cd

    or

    cd ~

    [Note]

    The ~ character represents the current user's home directory. As seen above, cd ~ is equivalent to cd /home/username/. However, when running a command as root (using sudo, for example), ~ points instead to /root. When running a command with sudo, the full path to your home directory must be given.

  • To navigate up one directory level, type:

    cd ..

  • To navigate to the previous directory (or back), type:

    cd -

  • To navigate through multiple levels of directories at once, specify the full directory path that you want to go to. For example, type:

    cd /var/www

    to go directly to the /www subdirectory of /var/. As another example, type:

    cd ~/Desktop

    to move you to the Desktop subdirectory inside your home directory.

pwd

The pwd command outputs which directory you are currently located in (pwd stands for “print working directory”). For example, typing

pwd

in the Desktop directory, will show /home/username/Desktop.

[Note]

GNOME Terminal also displays this information in the title bar of its window.

ls

The ls command outputs a list of the files in the current directory. For example, typing

ls ~

will show you the files that are in your home directory.

Used with the -l options, ls outputs various other information alongside the filename, such as the current permissions on the file, and the file's owner.

cp

The cp command makes a copy of a file. For example, type:

cp foo bar

to make an exact copy of foo and name it bar. foo will be unchanged.

mv

The mv command moves a file to a different location or will rename a file. Examples are as follows:

mv foo bar

will rename the file foo to bar.

mv foo ~/Desktop

will move the file foo to your Desktop directory but will not rename it.

rm

rm is used to delete files.

rm foo

deletes the file foo from the current directory.

By default, rm will not remove directories. To remove a directory, you must use the -R option. For example,

rm -R foobar

will remove the directory foobar, and all of its contents!

mkdir

The mkdir command allows you to create directories. For example, typing:

mkdir music

will create a directory named music in the current directory.