Directory operations in Linux

The directory is the most essential component of any operating system. We often use a common term folder for the directory. A directory is just like a container to hold either another directory or a file or both. So we have a list of tasks for the directory operation –

Creation

To create a new directory we use the mkdir command.
Syntax –
mkdir [option] directory_name


mkdir LearnLinux

Multiple directories

What else when we need to create multiple directories? So we can also achieve this using space between two directories. Like.


cd LearnLinux
mkdir js css

Nested directories

Above all directories are siblings meaning this way we can only create a directory on the first level. But when we need to create a nested directive. We will use this command.


mkdir -p js/libs/graph

Here -p (parent) is a flag and it will create intermediate directories as required.

Permission

We can also create directories with the user required permission.


mkdir -m 777 LearnLinux

Here -m (mode) set the file permission bits of the final created directory to the specified mode.

Deletion

To remove the directory we can use rmdir and rm commands.
rmdir [option] directory_name
But Here we have a twist cause rmdir only works for the empty directories.

Empty directory

An empty directory contains neither a directory nor a file. If you have an empty directory, it can be easily removed by rmdir. No other options are needed.
rmdir [option] directory_name


mkdir img
rmdir img

Non-empty directory

Deleting a non-empty directive is a little tricky. Here we will use the rm command with the -r flag which means it will attempt to remove the file hierarchy rooted in each file argument.


mkdir img
cd img
touch graph
cd ..
rmdir img
rmdir: img: Directory not empty
rm -r img

Nested empty directory

A nested empty directory will delete the same as a normal directory remove.


mkdir -p js/libs/graph
rmdir -p js/libs/graph

Verbose mode

There is also a verbose mode that you may use. You might have already noticed that the rmdir command doesn’t show any result for successful commands. With this flag, you can see the related log on the screen. You can use also this mode for mkdir.
rmdir -v directory_name


mkdir -v js
js
rmdir -v js
js

Traversing

Traversing means how to navigate the directory or how we can travel from one directory to another. For that we can use the cd command means change directory command.

Parent to child

If your current directory contains another directory then it will be a parent and the contained directories are children. So we can navigate the parent to a child using the cd command.
cd child_directory_name


mkdir js
ls
js
cd js
ls

Child to parent

To navigate child to parent we will use a special .. link. That always points to parents.


mkdir js
ls
js
cd js
ls
cd ..
ls
js

Previous directory

To navigate the current working directory to the previous working directory. We will use – the option.


mkdir css
ls
css
cd css
ls
cd -
ls
~/Documents/Wayofwebs/LearnLinux

Move

To change the place of any directory is just like the windows operating system cut and paste operation. You can use the mv command to move directories as well.
mv source_directory target_directory
In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. This means that the source_directory will become a sub-directory of the target_directory.


mkdir js scripts
ls
js scripts
mv js scripts
ls
scripts

Copy

To create a replica of any directory. Just like the window operating system copy and paste operation. To create a copy we can use the cp command. Be careful cause because the directory would either empty or non-empty.
cp -r source_dir target_dir


mkdir js scripts
ls
js scripts
cp js scripts
cp: js is a directory (not copied).
cp -r js scripts
ls
js scripts

Copy Multiple Directory

You can also copy multiple directories at once with the cp command in Linux. Just use it the same way you did for a single directory.
cp -r source_dir_1 source_dir_2 source_dir_3 target_dir


mkdir js scripts lib
ls
js lib scripts
cp -r js lib scripts
ls
js lib scripts
ls scripts
js lib

Renaming Directory

Renaming the directory means changing the name of the directory. Here we will use the mv command.
mv source_directory path_to_non_existing_directory


mkdir js scripts
ls
js scripts
mv js scripts
ls
scripts
mv scripts libs
libs

Viewing and Searching

Viewing the current working directory. This means all directories and files which contain the current directory. we can use the ls command. We have already used this command above.
ls


touch index.html about.html
ls
about.html index.html

Searching means searching or finding some specific directory or file. We can use the find command.
find file_name


touch index.html about.html
find *.html
home.html
index.html

About the Author: Pankaj Bisht