How to make a folder in Linux or Unix
I
am a new Linux user. How do I make a folder in Linux or Unix system using the CLI?
You need to use the mkdir command to create new directories or folders in Linux or Unix-like system. The syntax is:
mkdir folderName
mkdir [option] folderName
This page shows you how to create new folders and directories on Linux or Unix-like system using the command line option.
How to create a new folder named foo
Open the Terminal app and type the following command:
mkdir foo
To see directory listing use the ls command:
ls
ls -l
You can simultaneously create any number of folders/directories:
mkdir dir1 dir2 dir3 dir_4
Verify it:
ls -l
Fig.01: How to create Folders/Directories In Linux/Unix with the mkdir command
Let us try to understand the following line:
drwxr-xr-x 2 vivek vivek 4096 Aug 7 20:57 dir_4
drwxr-xr-x – The file mode (see below for file mode).
2 – Number of links
vivek – The owner name
vivek – The group name
4096 – The number of bytes in the file
Aug 7 20:57 – The date when the file was last modified
dir_4 – File/dir name path
More on file mode
The entry type character describes (the first character drwxr-xr-x) the type of file, as follows:
- : Regular file.
- b : Block special file.
- c : Character special file.
- d : Directory.
- l : Symbolic link.
- p : FIFO.
- s : Socket.
- w : Whiteout.
- So basically d character in above entry tell us that it is a directory/folder. The next three fields are three characters ach: owner permissions, group permissions, and other permissions. Each field has three character positions:
- 1. If r, the file is readable; if -, it is not readable.
- 2. If w, the file is writable; if -, it is not writable.
- 3. The first of the following that applies:
- S If in the owner permissions, the file is not exe-
- cutable and set-user-ID mode is set. If in the
- group permissions, the file is not executable and
- set-group-ID mode is set.
- s If in the owner permissions, the file is exe-
- cutable and set-user-ID mode is set. If in the
- group permissions, the file is executable and set-
- group-ID mode is set.
- x The file is executable or the directory is search-
- able.
- – The file is neither readable, writable, exe-
- cutable, nor set-user-ID nor set-group-ID mode,
- nor sticky. (See below.)
- These next two apply only to the third character in the last
- group (other permissions).
- T The sticky bit is set (mode 1000), but not execute
- or search permission. (See chmod(1) or
- sticky(7).)
- t The sticky bit is set (mode 1000), and is search-
- able or executable. (See chmod(1) or sticky(7).)
- How to create a new directory named bar
- Open the Terminal app and type the following command but pass the -v option to get visual confirmation about your folder/directory name:
- mkdir -v foo
Sample outputs:
mkdir: created directory foo
How to make parent folder if needed
The syntax is:
mkdir -p dir1/dir2
mkdir -p parent/child
mkdir -p pictures/vacations
ls -l
ls -l pictures
How to make a folder/directory and set permissions on it
New directories are by default created with the read, write and execute permissions. Pass the -m option to directory to set the the permissions of new directories:
mkdir -m {permissions} {dirName}
mkdir -m 777 delta
ls -l
To create a directory named delta for which all three types of permissions were enabled for all users, the sequence 777 would be added after the -m as above. More information about mkdir can be obtained from the mkdir man page:
man mkdir
Or
mkdir –help
Sample outputs:
Usage: mkdir [OPTION]… DIRECTORY…
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, –mode=MODE set file mode (as in chmod), not a=rwx – umask
-p, –parents no error if existing, make parent directories as needed
-v, –verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
–context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
–help display this help and exit
–version output version information and exit
To delete the directory, use the rmdir command/rm command.