You need to use the mkdir command to create new directories or folders in Linux or Unix-like operating systems. This page shows how to create a folder (also known as “directory”) in Linux or Unix systems.
How to make a folder in Linux
The procedure is as follows:
- Open the terminal application in Linux
- The mkdir command is is used to create new directories or folders.
- Say you need to create a folder name dir1 in Linux, type: mkdir dir1
Let us see examples and other usage in details. The syntax is:
mkdir folder mkdir [option] folderName mkdir directory
Now you know the syntax. Let us explore 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 in Unix
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
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. For example, delete a folder named dir1:
rmdir dir1
OR
rm -rf dir2
Conclusion
This page explained how to make a folder in Linux. Please mkdir man page online here or type the following command:
man mkdir
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Use our forum for further help