How to Create Directories on Terminal in Ubuntu
This is an article about creating directories on terminal in Ubuntu, a popular Linux distribution. In this guide, we’ll cover the basics of working with directories and files using command-line tools, focusing specifically on the mkdir command which is used for making new directories.
In this article you will find information about how to create, navigate through, and manage multiple directories and subdirectories from your terminal in Ubuntu. Read this article to find out about:
- How to create a single directory.
- Creating multiple directories at once.
- Using options with mkdir for advanced directory creation.
- Navigating through directories using cd.
- Deleting directories with rmdir.
Understanding how to manipulate directories is crucial when working in the terminal. This skill not only helps you organize files efficiently but also enables you to automate tasks and scripts that require folder management.
Prerequisites
Before diving into creating directories, ensure that your system meets these basic requirements:
- You are logged in as a user with administrative privileges.
- Your Ubuntu system is up-to-date with the latest packages installed via sudo apt update && sudo apt upgrade.
With this setup, you’re ready to start managing directories from the command line.
Creating a Single Directory
The most straightforward way to create a directory using terminal commands is by utilizing the mkdir command (make directory). Here’s how to do it:
Basic Syntax
Replace directory_name with your desired folder name. For example, if you want to create a directory named projects, simply open your terminal and type:
Press Enter to execute the command.
If you’re in a different location within your file system (for instance, /home/username/Documents) when creating the new directory, it will be created directly under that folder. Thus, the full path of projects would be /home/username/Documents/projects.
Checking Directory Creation
After creating a new directory, verify its creation by listing files and directories in your current location with:
or simply use ls for a quick overview.
Creating Multiple Directories at Once
Creating multiple directories can be done efficiently using one command. The syntax is similar but includes additional directory names:
Basic Syntax
For example, to create three directories named docs, images, and scripts:
This single command will make all specified folders in the current working directory.
Recursive Directory Creation
If you want to create a series of nested directories (parent-child relationship), use the -p flag:
The parent, child, and grandchild directories will be created automatically, even if they don’t exist in your current directory structure.
Advanced Directory Creation with mkdir
Specifying Permissions
You can set specific permissions for a newly created directory using the -m option followed by permission bits:
This command sets the default permissions to owner (read, write, and execute), group (read and execute), others (no access).
Creating Symbolic Links
While not directly related to mkdir, creating symbolic links for directories can be handy. Use ln to create a link:
Replace /path/to/existing_dir with the path of your existing directory and /desired/location/new_link with where you want to place this symbolic link.
Navigating Through Directories
Once directories are created, it’s important to know how to navigate through them:
Using cd
The command for changing directories is cd. Here’s an example:
This will change your working directory to /home/username/Documents/projects.
For navigating back a directory level:
To move directly to the root directory:
Deleting Directories
When you no longer need certain directories, deleting them is straightforward:
Using rmdir for Empty Folders
Use rmdir to remove empty directories. The basic syntax looks like this:
For multiple folders in one command:
Note: If the directory is not empty, you will need to use a more powerful tool such as rm.
Using rm for Non-Empty Directories
To remove non-empty directories, or if you prefer using rm, use the -rf option:
Be cautious with this command as it does not prompt before removing files and directories.
Conclusion
This article has covered the basics of creating directories on Ubuntu’s terminal. From setting up a single directory to managing multiple levels of nested folders, you now have the essential skills needed for effective file organization via the command line interface. Remember, practice is key to mastering these commands, so experiment with different options and flags as you work with your files and directories.
By incorporating the mkdir, cd, rmdir, and rm commands into your workflow, you will enhance your efficiency in managing projects and data on Ubuntu systems. Continue exploring additional command-line utilities and options to further expand your Linux terminal capabilities!
Last Modified: 17/11/2015 - 23:50:46