How to Manage Directories in Linux?
Last Updated :
25 May, 2024
Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories and many more.
We will explore various topics, including how to use mkdir
to create directories, cp
to copy directories, rm
and rmdir
to remove directories, and mv
to move or rename directories. You will also learn how to navigate through directories using the cd
command and how to check the size of directories with the du
command. Understanding these commands will help you efficiently manage files and directories in Linux.
How to Manage Directories in Linux
What is Linux?
Linux is a open source operating system kernel developed by Linus Torvalds in 1991, often referred to as an operating system by people. Linux kernel is combined with other utility software like GNU, desktop environments etc to form a full operating system which are called as Linux distros.
What is a Directory?
Directory which is also known as folder is used to store and organize computer system's files. Directory has many features which makes it a very good of storing and organizing files like defining permissions, hierarchy, naming etc.
Managing Directories in Linux
To manage directories in Linux based system, follow the given steps carefully without making any mistake -
Step 1. Creating Directories
To create a directory in Linux you have to use mkdir command which stands for make directory.
mkdir directory_name
creating directory named 'GFG'Step 2. Listing Directories
To list you can use ls command or you can also use flag -l with it to check if its directory of not like in below image you will see that in directory details it starts with 'd' in 'drwxrwxr-x' where 'd' is for directory.
ls
or
ls -l
List DirectoryYou can also list a directory's content by using ls command
ls path/to/directory
List inside contents of DirectoryStep 3. Removing Directories
To remove a directory you can use 'rmdir' but for that the directory must be empty
rmdir directory_name
To remove a directory which has contents in it use 'rm' with recursive flags '-r' or '-rf'
rm -rf directory_name
Removing a directory and its contentThis will delete the directory with all its content inside it.
Step 4. Changing Directories
To change directories you have 'cd' with various attributes.
cd Directory_name
If directory is somewhere else then give the path also with directory name.
cd /path/to/directory
Changing DirectoryTo move back to last directory you were in
cd -
To move to the parent directory of your current directory use
cd ..
To move back to the default directory use
cd ~
To check in current directory use (while it has no much use case other than scripting but it is still useful to know it)
cd .
Step 5. Copying Directories
To copy a use 'cp' command with '-r' flag to copy the contents inside the directory also. While you can copy any directory from anywhere like if you not in the parent directory of the directory which you are copying, you can just provide the full path of its location from where you are copying and to where you want to copy.
cp -r source_dir destination_dir
Copying DirectoryStep 6. Moving or Renaming Directories
To move or rename a directory, for both 'mv' command is used.
To move a directory provide the path of source directory and destination path. You can move a directory from and to anywhere, you just need to provide correct path with directory name which you want to move and the destination where you want to move, required you have permission to do so.
mv source_dir /path/to/destination
Moving DirectoryTo rename a directory you need to present in the parent directory. just provide the old name and followed by new name.
mv old_name new_name
Renaming Directory from 'GFG' to 'gfg'Step 7. Checking Directories Size
To check a directories/files size use 'du' command and to get in human readable format (kb,mb,gb) use '-h' with it.
du -h
Check Size Conclusion
So we had discussed all the core concepts of managing directories in Linux. Directories are essential part for maintaining and organizing the files and data in a file system. We saw the use of commands 'mkdir', 'mv', 'cp', 'rm', 'rmdir', 'ls' to create, list, delete, navigate and move directories. Its essential to remember all the commands by practicing them as you will need them while using Linux. So We hope you got the idea of how to manage directories in linux based system. Still if you have any related queries, following frequently asked questions will definitely help.
Similar Reads
How to Remove Directory in Linux
In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files
5 min read
How to Copy a Directory in Linux
There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
5 min read
How to Manage Debian Packages in Linux?
Debian, one of the oldest and most respected Linux distributions, is renowned for its stability, security, and robust package management system. Managing Debian packages effectively is crucial for maintaining a healthy and well-functioning Linux system. In this comprehensive guide, we will explore e
9 min read
How to Manage Logs in Linux?
Linux log files are records of system activities, It contains information about processes, errors, and events. Managing these files is crucial for troubleshooting issues, analyzing system performance, and ensuring system stability by identifying security threats. This article will review the command
4 min read
How to Rename a Folder in Linux
Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
12 min read
How to Delete Files in Linux?
Linux comes with several tools that can assist us in removing Directories and files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial. Use caution when using the commands b
6 min read
How to Check Disk Space in Linux
Efficient disk space management is important for maintaining the performance and stability of your Linux system. Over time, files and applications can fill up your storage, potentially causing slowdowns or errors. Commands to Check DIsk Space in LinuxKnowing how to check disk space in Linux helps yo
6 min read
How to Create File in Linux
Today, we're going to learn about something really important â how to create files in Linux. It's like creating a fresh piece of digital paper to write or store things. We'll explore different ways to do this using simple commands. Whether you're just starting out or have been using Linux for a bit,
7 min read
How to remove a directory in R?
In this article, we will discuss how to remove a directory using R programming language. To remove a directory in R we use unlink(). This function deletes the named directory. Syntax: unlink(directory-name, recursive = BOOLEAN) Parameter: directory-name: a character vector with the names of the dire
1 min read
How to Insert a Directory to PATH in Linux
Adding a directory to the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without requiring the full path each time. When you type a command in the Linux terminal, the shell searches for the corresponding executable file to run it. These executables
5 min read