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:
- 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:
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:
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:
This command will automatically take you to the root directory.
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
- 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.
Need More Personalized Help?
If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account -> Support and provide your server credentials within the encrypted field for the best possible security and support.
If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: firstname.lastname@example.org. We are also available to you through our phone and live chat system 24/7/365.