How to create directories in linux terminal

Terminal Basics #2: Making Directories in Linux Terminal

Learn to make new folders in the Linux command line in this part of the Terminal Basics tutorial series.

In the previous chapter of the Terminal Basics series, you learned about changing folders in the Linux command line. I gave an exercise at the end that briefly mentioned making directories. In this part of the series, I’ll discuss how you can make new folders in the Linux command line using the mkdir command.

Making a new directory in Linux

You should be familiar with the concept of absolute and relative paths in Linux by now. If not, please refer to this tutorial. Open the terminal on your system if it is not already opened. Normally, you start with your home directory (/home/username). But for the sake of this tutorial and to recall a couple of things, I presume you are not in your home directory. So, change to your home directory first.

Yes. If you simply enter cd without any options and arguments, it takes you to your home directory. You could also use cd ~ among other methods. Here, make a new directory called practice.

Great! Now you have a dedicated folder where you’ll practice the Linux command line tutorials in this series.

Creating multiple new directories

You just created a new directory. What if you have to create more than one? Let’s say three of them. You may use the mkdir command three times in a row for each of them. It will work. However, it is not really needed. You can save time and effort by creating multiple directories at the same time like this:

Create multiple new directories in Linux with mkdir command

Go on and do that please. You can list the contents of the practice directory to see all the newly created directories. More on the ls command later.

Making multiple nested subdirectories

So, you now know about creating multiple directories at once. But what if you have to create a nested directory structure? Let’s say that you have to create a directory subdir2 inside subdir1 inside dir1.

The problem here is that subdir1 does not exist. So if you try `mkdir dir1/subdir1/subdir32, you’ll get an error:

[email protected]:~/practice$ mkdir dir1/subdir1/subdir2 mkdir: cannot create directory ‘dir1/subdir1/subdir2’: No such file or directory

If you didn’t know better, you would go for mkdir dir1/subdir1 and then run mkdir dir1/subdir2 . That will work. However, there is a much better way. You use the -p option, which makes parent directories if needed. If you run the command below:

mkdir -p dir1/subdir1/subdir2

There is no naming convention, but it is better to avoid spaces in file and directory names. Use underscore or dash instead because handling spaces in file/directory names requires special effort.

Читайте также:  Linux скрипт вывода информации

Test your knowledge

  • Without entering the dir2 directory, create two new subdirectories in it.
  • Without entering the dir3 directory, create two-level nested subdirectories (subdir1/subdir2)
  • Change to the dir2 directory. From here, create a directory named temp_stuff in your home directory. Don’t worry; we will delete it later in this tutorial series.
  • Go back to the parent practice directory and try to create a directory named dir3 . You see an error. Can you make it go away with the -p option?

In the next chapter of the Terminal Basics series, you’ll learn about listing the contents of a directory with the ls command.

Do let me know if you have questions or suggestions.

Источник

Linux essentials: How to create and delete files and directories

Learn how to use the mkdir, touch, and rm commands to create files and directories, then clean them up when you’re ready.

File system

Creating and deleting files and directories are standard operations for a sysadmin. Depending on your operating system and filesystem, there may be different ways to perform these tasks. The most efficient way is to use the shell (for instance, Bash). This article assumes you already understand how to enter commands into a Linux terminal. (Read Nathan Lager’s What sysadmins need to know about using Bash if you want a refresher.)

Connect to your Linux terminal and get ready to sling some files.

[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]

Create a directory

Before creating a new directory, use the pwd command to understand where you are in the filesystem:

I’m in the localuser’s home folder (and you’re probably in whatever user’s home directory you’ve logged in as). Checking for files with the ls command, I see that I have none:

Create a new directory as the starting point for this article’s exercises. The command to create a new directory is mkdir . Check its options and available parameters:

$ mkdir --help 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

IT Automation ebook

The basic syntax for using this command is mkdir (replace with the desired name of your directory). Before creating any directory or file, remember that most Linux filesystems are case-sensitive. That means a resource named Dir or File is different from dir or file , which is also different from DIR or FILE . Bash interprets the name exactly as you spell it.

Читайте также:  Виртуальная машина java linux

Create a new directory called mydir , change to it with the cd command, and list its contents:

$ mkdir mydir $ file mydir mydir: directory $ ls -l drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:47 mydir $ cd mydir/ $ pwd /home/localuser/mydir $ ls -l total 0

You’ve just created a new directory, confirmed that it is indeed a directory-type object with the file command, entered the directory, and verified that it is empty. To create more than one directory simultaneously, specify the names of the new directories after mkdir with a blank space between them:

$ mkdir dir1 dir2 dir3 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 [mydir]$ ls -R .: dir1 dir2 dir3 ./dir1: ./dir2: ./dir3:

You created three new empty directories.

To create a directory with a directory inside of it, use the -p option:

$ mkdir -p dir4/subdir1 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 $ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 ./dir1: total 0 ./dir2: total 0 ./dir3: total 0 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1 ./dir4/subdir1: total 0 [mydir]$ ls -l dir4/ total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1

Create files

Now that you have some directories, create some files. There are multiple ways to create files. To create files using shell redirection, refer to How to manipulate files with shell redirection and pipelines in Linux. You can also create empty files with the touch command. Here are its options and parameters:

$ touch --help Usage: touch [OPTION]. FILE. Update the access and modification times of each FILE to the current time. A FILE argument that does not exist is created empty, unless -c or -h is supplied. A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output. Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING parse STRING and use it instead of current time -f (ignored) -h, --no-dereference affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink) -m change only the modification time -r, --reference=FILE use this file's times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time --time=WORD change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m --help display this help and exit --version output version information and exit

The basic syntax to create an empty file is touch :

$ ls dir1 dir2 dir3 dir4 $ touch file1 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1

To create multiple files, type each file’s name in front of another with blank spaces separating them:

$ touch file2 file3 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3

To create an empty file inside a subdirectory, specify the full path of the desired directory before the name of the new file:

$ touch dir4/subdir1/file4 $ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 ./dir1: total 0 ./dir2: total 0 ./dir3: total 0 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:35 subdir1 ./dir4/subdir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:35 file4 [mydir]$ ls dir4/subdir1/ file4

Delete files and directories

Training & certification

Now that you have created some files and directories, you can delete everything you’ve created so far.

Читайте также:  Забыл имя пользователя кали линукс

It can be easy to get disoriented in the terminal, which can have disastrous consequences. Use the pwd command to display exactly which part of the filesystem you’re in.

The safest way to remove files and directories is to send them to a trash bin, just as you do on your desktop. Projects such as trashy and trash-cli help you remove files from a directory without actually deleting anything until you’re ready for the data to be erased. However, these tools are not often installed by default. The standard tool to remove resources is rm .

Use the rm command when you’re sure you’re ready to erase data permanently. Unlike trash commands, there is no unremove command, so use rm judiciously.

$ ls dir3/ dir2 file3 $ rm dir3/file3 $ ls dir3/ dir2

To delete a directory and its contents, use the -r or -R option with rm :

If you’re dealing with an empty directory (such as my example dir3 , which had its contents removed), use the -d parameter to delete it:

Advanced operations

The shell makes creating files and directories easy, scriptable, and efficient. You can use special shell operations to create multiple directories at a time. Try mkdir along with seq :

Wrap up

The Linux terminal is a powerful tool. Now you know how to use it for a few basic file-management tasks. Creating and deleting files and directories are essential tasks. Tasks such as copy and move are equally important, and I’ll write about them in my next article.

Источник

Оцените статью
Adblock
detector