Friday 20 April 2018

Delete Directory, Files - rm, rmdir command in Unix / Linux

rm, rmdir command, Unix Command, Linux Command

How to delete directories and files in unix/linux

Unix provides rmdir and rm commands to remove the directories and files. Let see each command in detail.

Unix rmdir command syntax


The syntax of rmdir command is

rmdir [options] directories

The rmdir command options are

-p : Removes directory and its parent directories
-v : Provides the diagnostic information of the directory processed

Unix rmdir command examples


1. Write a unix/linux command to remove a directory?

The rmdir command deletes only the empty directories. If a directory contains files or sub directories, then the rmdir command fails.

rmdir docs/
rmdir: docs/: Directory not empty

Here the docs directory is not empty, that is why the rmdir command failed to remove the directory. To remove the docs directory first we have to make the directory empty and then delete the directory.

rm doc/*
rmdir docs/

We will see later how to remove non-empty directories with a single command.

2. Write a unix/linux command to remove the directory and its parent directories?

As mentioned earlier the -p option allows the rmdir command to delete the directory and also its parent directories.

rmdir -p docs/entertainment/movies/

This rmdir command removes the docs directory completely. If you don’t use the -p option, then it only deletes the movies directory.

3. Write a unix/linux command to remove directories using pattern matching?

You can specify the directory names using the regular expressions and can delete them.

rm doc*

This rm command deletes the directories like doc, documents, doc_1 etc.

Now we will see the rm command in unix.

Unix rm command syntax


The syntax of rm command is

rm [options] [directory|file]

The rm command options are

f : Removes all files in a directory without prompting the user.
i : Interactive: prompts the user for confirmation before deleting a file.
R or r : Recursively remove directories and sub directories.

The rm command can be used to delete both the files and directories. The rm command also deletes the non-empty directories.

Unix rm command examples


1. Write a unix/linux command to remove a file?

This is the basic feature of rm command. To remove a file, logfile.dat, in the current directory use the below rm command

rm logfile.dat

2. Write a unix/linux command to remove all the files in a directory?

use the * regular pattern as the file list in rm command for deleting all the files in the current directory.

rm *

3. Write a unix/linux command to delete empty directory?

The rm command can also be used to delete the empty directory. The command for this is

rm docs/

If the directory is non-empty, then the above command fails to remove the directories.

4. Write a unix/linux command to delete directories recursively (delete non empty directories)?

As mentioned earlier, the -r option can be used to remove the directories and sub directories.

rm -r docs

This removes the docs directory even if it is non-empty.

Related Posts

0 comments:

Post a Comment