UNIX commands — Working with directories

 
 

Overview

This article provides several examples of how to interact with directories after logging into your DreamHost server via SSH.

Navigating directories

This section explains how to change between directories and confirm your current location.

View your current directory

You can confirm the directory you are currently in using the pwd command. This shows the user is in the /home/username/example.com directory:

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

Change to the user's home directory

The user's home directory is where all files and directories are located for the specific user. To navigate to the directory, use the ~ symbol. For example:

[server]$ cd ~

Change directories

To change to another directory, use the cd command. This example changes to the example.com/blog directory located within the user's home directory.

[server]$ cd ~/example.com/blog

Return to the previous directory

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

[server]$ pwd
/home/username/current/directory/
[server]$ cd ~/new/directory/ [server]$ pwd home/username/new/directory/
[server]$ cd - [server]$ pwd /home/username/current/directory/

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

You can move up a single directory quickly by using ../.

[server]$ cd ../

String together to move up multiple directories.

[server]$ cd ../../

 

Creating and deleting directories

This section explains how to create and delete directories that are empty or contain files.

Create directories

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

[server]$ mkdir directory_name

Delete directories

There are a few ways to delete directories. 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. You must instead use rm -r to remove the directory and all of its contents.

[server]$ rm -r directory_name

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

 

Viewing directory content

The following explains different ways to list contents and their size on the server.

List contents of the 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 Mar 25  2024 .bashrc
drwxr-xr-x  5 username pg######   4096 Mar 25  2024 Maildir

View 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 PST.

Still not finding what you're looking for?