Sunday, 21 June 2015

How to create/remove directory/folder in Linux

mkdir command is used to create directory/folder on linux file system.

Syntax:
mkdir [OPTION] DIRECTORY

Refer below options used in mkdir command:

-m, --mode=MODE Set file mode (as with the chmod command).
-p, --parents Create parent directories as necessary. When this option is used, no error is reported if a specified DIRECTORY already exists.
-v, --verbose Verbose output; print a message for each created directory.
--help Display a help message, and exit.

Note: If directory already exists then command will modify the creation time of directory.

Examples:

#Creates a new directory called linuxinanutshell whose parent is the current directory.
mkdir linuxinanutshell

#Create the linuxinanutshell directory, and set its permissions such that all users may read, write, and execute the contents.
mkdir -m a=rwx linuxinanutshell

#Creates the directory /home/linuxinanutshell/a/b/c. If the parent directory /home/linuxinanutshell/a/b/c does not already exist, mkdir will create that directory first.
mkdir -p /home/linuxinanutshell/a/b/c


To remove directory, use rmdir command

The rmdir utility removes the directory entry specified by each directory argument, provided the directory is empty.


Arguments are processed in the order given. In order to remove both a parent directory and a subdirectory of that parent, the subdirectory must be specified first so the parent directory is empty when rmdir tries to remove it.

Syntax:
rmdir [-p] directory



--ignore-fail-on-non-empty ignore any failure which occurs solely because a directory is non-empty.
-p Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the lastmost component. (See rm for fully non-discriminant recursive removal.)
-v, --verbose Display verbose information for every directory processed.
--help Display a help message, and exit.

Example:
rmdir linuxinanutshell


No comments:

Post a Comment