In this tutorial we’ll cover how to create, change, and remove a directory within Linux using the terminal window and a few basic commands.
Use the the table of contents below to jump to a specific section or read on to learn more!
How to Create a Directory in Linux
To create a directory in Linux, just follow these simple steps:
- From the terminal window, you can create a new directory using the mkdir command, followed by the name of the directory you’d like to create. For example:
mkdir sampledirectory
- Using the command above would create a new, empty directory, called sampledirectory. The file name can be replaced with any text of your choosing.
- Once you’ve created your directory, you can confirm that it has been created successfully using the ls command. When used, you should see the name of the directory you just created among the output list of files and directories.
- If you wanted to create a new directory in a specific location (as opposed to just your current directory), you can do so by specifying the file path following the mkdir command. For example:
mkdir/tmp/sampledirectory
This would create the new sampledirectory within the existing /tmp directory.
How to Change Directory in Linux
To change your current directory in Linux, just follow these easy steps:
- Before you can change to a different directory, you should first determine which directory you’re currently in. If you’ve just opened the terminal, you’ll be in the home directory by default. You can check this either by using the print working directory command, pwd, or, if you know which files are located in which directories, you can use the ls command to list the contents of your current directory.
- Now that you know how to determine which directory you’re currently in, the next step is to determine which directory you’d like to go to. You’ll use the cd command to change directories. For example:
cd /tmp/sampledirectory
This command would automatically move you into the directory named sampledirectory, within the /tmp directory. This can be confirmed using the pwd command.
- Using the exact file path isn’t the only way to navigate through your directories in Linux. There are several shortcut commands that can be used as well. For example:
cd /
This command will automatically take you to the root directory.
cd ~
This command will take you to the home directory.
To move up a directory, use cd .. and to move back a directory, use cd –
How to Remove a Directory in Linux
To remove a directory in Linux, just follow these simple steps:
- There are a couple of commands that can be used to delete directories within Linux. Which one will work best for you will depend on the contents of the directory you are attempting to delete.
- If you are attempting to remove an empty directory, such as the sampledirectory we created above, you can use either:
rm -d sampledirectory
or
rmdir sampledirectory
- If the directory you are attempting to remove has files within it and you would like to delete these files as well, you can use the command:
rm -r sampledirectory
*Note: if any of these files are write-protected, you will be prompted to confirm the deletion of these files. If you would prefer to delete these files without be prompted for confirmation, you can use the command rm -rf sampledirectory, to override these prompts.
Popular Links
Looking for more information on Linux? Search our Knowledge Base!
Interested in more articles about Operating Systems? Navigate to our Categories page using the bar on the left or check out these popular articles:
- How to Check the Apache Version on a Linux Dedicated cPanel Server
- How to Change a Password in Linux
- How to Check if Your Linux Server is Under DDoS Attack
Popular tags within this category include: Linux, Windows, Apache, CentOS, Debian, Fedora, RedHat, and more.
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
Seeking a better Dedicated Server solution? In the market for Private Cloud or Colocation services? Check out Hivelocity’s extensive list of products for great deals and offers.
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.
Unsure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.