Directories in Linux are folders that contain files and other directories. They are organized in a hierarchical structure, starting from the root directory (/) that contains all other directories and files. In this tutorial, we will cover handling directories in Linux and explain how to create, rename, delete, and move through directories.
Handling directories in Linux – Absolute and Relative Paths
When traversing directories in Linux, there are two ways to type a file or directory path, an absolute path and relative path.
An absolute path is the specific location irrespective of the current working directory or combined paths and are written with reference to the root directory, starting with a forward slash /. Example of using absolute path, “cd /usr/bin”
A relative path is the path relative to the current working directory using period and double periods, indicating current and parent directories. Example of using a relative path when the current working directory is /usr/, “cd bin”
Both types will take you to the same place but take different steps and criterias to do so, when you handling directories in Linux.
How to Create a Directory
To create a directory, we will use the mkdir command to do so. Follow the steps below to proceed.
1. To create a directory in Linux, you can use the mkdir command followed by the name of the directory, as shown below.
The command will create a directory in the root directory (/) called “Net_Growth_21”.
mkdir /Net_Growth_21 which will create a directory in / with that name.
2. Creating a directory in a different directory, the full path can be given with the command.
mkdir /guide/Net_Growth_21 which will create the directory in a directory, /guide/, as shown below.
To navigate through directories, we will be using the following commands, pwd, ls, and cd.
1. The pwd command will print the working directory which you are currently working in.
2. The ls command will list all items in the directory you are currently working in.
Another good option with ls is ls -lh which will present the items size and in a list form.
3. The cd command is used to move through directories.
- Using cd . will navigate to the current directory as the period (.) is used to represent the current directory.
- Using cd .. will navigate one directory above the current directory.
- Using cd ~ will take you to the home directory of the currently logged user. Two examples are listed below for root and psuissa users.
How to Move a Directory in Linux
If you’re trying to move a directory in Linux from one location to another, just follow these 4 steps:
1. To move a directory in Linux, you can use the mv command followed by the source and destination directories, as shown below.
mv [source directory] [destination directory]
2. So, if you wanted to move /directory1 into a second directory name /directory2, you would use the following command:
mv /directory1 /directory2
Note: If no “/directory2” exists, /directory1 will be renamed as “/directory2”, essentially “moving” it into a newly created directory, otherwise, all content from /directory1 will move to /directory2.
3. This can also be done for multiple files and directories at once. Let’s say you have 3 files (File1, File2, and File3), and you’d like to move all them into /Directory1. Simply list out all your sources before your destination, and Linux will automatically move all these files at once.
mv file1.txt file2.txt file3.txt /home/directory1
4. Keep in mind that in order to move files and directories, a user must have permissions allowing them to modify both the source and destination directories. Otherwise, an error will occur and permission will be denied.
How to Copy a Directory in Linux
If you’re trying to make a copy of an existing directory, just follow these 4 steps:
1. To copy a directory or file in linux, just use the copy command followed by the Source (the directory you’re copying), and the Destination (the place you’re copying it to):
cp -R [Source] [Destination]
2. to make a copy of /directory1 and place it inside of /directory2, you would use the command:
cp -R /directory1 /directory2
*Note: If no “/directory2” exists, a new directory will be created, with that name, and containing the contents of /Directory1.
3. Like moving files, it is possible to copy multiple files into a single directory by listing out multiple filenames for sources, followed by a single destination. For example:
cp -R file1.txt file2.txt file3.txt /directory1
Which would copy file1, file2, and file3 into /directory1.
4. Additionally, if you wanted to copy the entire contents of /Directory1 into /Directory2 (duplicating the files inside the folder but not the folder itself), you can do so by adding ” /* ” after the source directory. For example:
cp – R /directory1/* /directory2
This would create copies of all the files located in /directory1, placing duplicates of its content into /directory2, but not duplicating the actual directory itself.
How to Rename a Directory in Linux
If you need to rename an existing directory in Linux to something new, just follow these 2 steps:
1. Interestingly, due to the way Linux’s file structures work, moving a file and renaming a file are actually the same action. This is because when you’re “moving” a file in Linux, you’re really “renaming” its file path: giving it a new name but also a new location in your system’s structure.
mv [Source Directory] [Destination Directory]
So, to rename /directory1 to /directory_new, you would use the command:
mv /directory1 /directory_new
If “/directory_new” already exists, then this command will move the contents of /directory1 to /directory_new. However, if “/directory_new” does NOT already exist, this command will rename /directory1 to /directory_new.