Open In App

How to Manage Directories in Linux?

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 a directory in Linux. We will try to cover every topic from creating, and copying to deleting the directories and many more.

What is Linux?

Linux is a open source operating system kernel developed by Linus Torvalds in 1991, often referred as an operating system by people. Linux kernel is combined with other utilities 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

1. Creating Directories

To created directory in Linux you have to use mkdir command which stands for make directory.

mkdir directory_name

creating directory named ‘GFG’

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 Directory

You can also list a directory’s content by using ls command

ls path/to/direcotory

List inside contents of Directory

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 content

This will delete the directory with all its content inside it.

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 Directory

To 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 .

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 Directory

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 Directory

To 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’

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.

Frequently Asked Questions to Manage Directories in Linux

Are directories essential in any file system?

Yes, directories helps in keeping all the files organized so that it is easier to manage them.

How to create a directory ?

To create a directory use mkdir command followed by the directory name like:

mkdir newDir

Can we create multiple directories in a single command?

Yes, we can create multiple directories in a single command by writing the directories name using space like

mkdir newD1 newD2 newD3 newD4

How can we switch back quickly to last directory?

You can switch back to last directory by using ‘ cd –

How to delete a directory and all its content?

We can delete a directory with all its content by using rm -r directory_name

What is difference between ‘ . ‘ and ‘ .. ‘ and its use case in Linux directory management ?

The single dot ‘ . ‘ refers to the current directory and the double dot ‘ .. ‘refers to the parent directory. In linux we use it with cd command to change location, like cd .. is used to move to the parent directory and cd . is used to check in current directory.

What if permission denied error comes while using any of above commands?

You can use sudo command at start of any command told above, this will overwrite any permission and will perform the action without any error. But remember to use it wisely and use only when you know what you are doing. Below is the command for that:

sudo rm -rf newD1


Article Tags :