UNIX commands — Working with directories

You can always discover what directory you are currently in using the pwd command. The following shows this user is in the /home/username/example.com directory:

[server]$ pwd
/home/username/example.com

Creating directories

To create a new directory, use the mkdir command. The follow example creates a new directory named ‘directory_name’:

[server]$ mkdir directory_name

Deleting directories

There are actually a few ways to delete directories in the shell. To delete an empty directory, use the rmdir command:

[server]$ rmdir directory_name

To delete a non-empty directory (one that still contains files or other directories in it) rmdir will not work. Here you have two choices. You can either remove all the contained files and directories by hand using the rmdir and rm commands, or you can use rm's -r flag.

[server]$ rm -r directory_name

Be careful using this flag, as you will delete everything contained in the directory specified. There is no "Recycle Bin" or "Trash Can" in the shell. What you delete is gone forever, so use caution.

Changing directories

To change to another directory, use the cd command. The following changes the directory used to /target/directory:

[server]$ cd /target/directory

A successful change will not return any messages.

To change to your previous directory, use the cd - command:

[server]$ pwd
/current/directory/
[server]$ cd /new/directory/
[server]$ pwd
/new/directory/
[server]$ cd -
[server]$ pwd
/current/directory/

The user above has changed from one directory to another, then used the cd - command to return to their previous directory.

You can go to the parent directory quickly by using the ../. If you are logged into your webserver via SSH, running the following command changes your directory to your user’s directory:

[server]$ cd ../

You can go up multiple directories by stringing the ../ together.

[server]$ cd ../../

List contents of current directory

To list the contents of a directory, use the ls command:

[server]$ ls
 Maildir    example.com    logs

Add the -l flag to list the contents with full details, including permissions, file size and last modified date:

[server]$ ls -l
drwx--S---    12 username  groupname    4096 Mar 15 17:28 Maildir
drwxr-xr-x     5 username  groupname    4096 Mar  7 12:35 example.com
drwxr-sr-x     8 root      root         4096 Apr 17 06:33 logs

To list all files within the directory (including hidden files) in vertical format, add the -la flags:

[server]$ ls -la
-rw-------  1 username pg######   10541 Mar 12 18:46 .bash_history
-rwxr-xr-x  1 username pg######   430 Dec 18 14:45 .bash_profile
-rw-r--r--  1 username pg######   237 Sep 16  2014 .bashrc
drwxr-xr-x  5 username pg######   4096 Sep  6  2011 Maildir

Directory sizes

To determine the size of a directory, use the du command with the -sh flags:

[server]$ du -sh example.com
1532    example.com

Add the -sh flag to see the size in a more readable format (KB, MB, GB, and so on). You can also list multiple directories and files separated by spaces.

See also

Did this article answer your questions?

Article last updated .