Linux Command Line for Beginners – creating directories and files

In this Command Line for Beginners tutorial series I try to lay down the basics in simple steps to get a feel for what is possible with the command line and how to actually work with it. In previous tutorials in this series, we talked about file navigation via the command line, and we also looked at how to easily find and install applications. In this new tutorial, we will look at how we can create and delete files and directories via the command line.

Content

  1. Introduction
  2. Creating new directories via the command line
  3. Removing directories via the command line
  4. Creating new files via the command line
  5. Removing files via the command line
  6. Permanently removing vs using the Trash bin
  7. Related reading
  8. Final words

Introduction

If you have also read my previous tutorials, then you now know how you can easily install applications via the command line and how you can navigate through your directories and files on your Linux system. In this tutorial, we go one step further by investigating how we can create and delete directories and files. It doesn’t need any further introduction, so let’s get started right away.

Creating new directories via the command line

Let’s start by creating new directories within a directory structure. We already learned how to navigate via the command line in my previous tutorial, so let’s first make sure we’re in our Home folder. The /home folder itself is also a subfolder under what we call the Root directory which is indicated with just a “/”. So /home is the direct next level below the main level or root level “/”. 

Do you want to know more about the meaning of those folders then read my earlier article “Simple explanation of the Linux file structure“.

Note: You will probably notice that I use the terms Folder and Directory interchangeably. In normal day talk probably most people use the term Folder because it is closer to our visual concept of something that holds our files, like photos, videos, and documents. Folders and Directories are often used to indicate the same, but they are conceptually a little bit different. There are different, and sometimes very complex explanations widely available, but in short…

a directory is a file system concept, and a folder is a graphical user interface metaphor that is used to represent a folder

(rewritten, based on https://en.wikipedia.org/wiki/Directory_(computing)#Folder_metaphor)

You will see that in this article it is useful to use Directory in addition to Folder, as some commands are directly related to directories, such as “mkdir”.

Start the Terminal with the key combination Ctrl + Alt + T.

You now see something like:

johnbeen@JBi7LinuxWS001:~$ 

Here we see, among others, the ‘~’ symbol, which is called the “tilde”. This indicates the complete path to your home folder, in my case /home/johnbeen.

Let’s create a new folder or directory. Type the below command and hit Enter:

mkdir NewFolder

This command is simple to explain. It is an abbreviation of “make directory” followed by the name of the directory or folder. You can read more about all the additional options for the mkdir command by typing the below command:

mkdir --help

This gives the following overview:

johnbeen@JBi7LinuxWS001:~$ 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

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

Note: When creating new folders, you have to keep in mind that they are case-sensitive, so NewFolder is different from newfolder or newFolder.

Now type the next command to verify that the folder is indeed created:

ls

Now we can see the new folder “NewFolder” is there:

johnbeen@JBi7LinuxWS001:~$ ls
cv_debug.log  Documents  Music      Pictures  Public     Videos
Desktop       Downloads  NewFolder  Postman   Templates  Zotero
johnbeen@JBi7LinuxWS001:~$ 

When you are reading Linux-related sources, you probably read about the powerful options of using the command line. One of these powerful options that you can’t do that easily via the graphical user interface, is creating multiple folders via only one command. 

Type the below command to create multiple folders in one go:

mkdir MyFolder01 MyFolder02 MyFolder03 MyFolder04

Type the below command to see that multiple directories have been created:

ls -l

Now you see:

drwxrwxr-x  2 johnbeen johnbeen  4096 Jul 12 10:14 MyFolder01
drwxrwxr-x  2 johnbeen johnbeen  4096 Jul 12 10:14 MyFolder02
drwxrwxr-x  2 johnbeen johnbeen  4096 Jul 12 10:14 MyFolder03
drwxrwxr-x  2 johnbeen johnbeen  4096 Jul 12 10:14 MyFolder04

We can even go further by creating a hierarchy of directories in one go. Type the following command:

mkdir -p MainFolder/SubFolder001

here the -p means “make parent directories”.

Type the following commands to verify that indeed the folder and subfolder have been created:

cd MainFolder
ls -l

Now you see that the SubFolder is indeed part of the MainFolder:

johnbeen@JBi7LinuxWS001:~/MainFolder$ ls -l
drwxrwxr-x 2 johnbeen johnbeen 4096 Jul 12 10:20 SubFolder001

Removing directories via the command line

Removing a directory is something you have to do with care because you want to be sure that the directory is empty, or that the files in the directory are not important anymore.

To remove an empty directory, like the NewFolder from the previous section, use the following command:

rmdir NewFolder

In this case, because it is empty, you could also have used the rm -d (-d stands for directory) command:

rm -d NewFolder

If the directory is indeed empty, it will be removed without problems. If it is not empty, you will get an error message like:

rmdir: failed to remove 'NewFolder': Directory not empty

In case the directory is not empty, you can remove the directory including all underlying files and folders with the rm command with the option -r (which stands for recursive):

rm -r NewFolder

As we saw with creating multiple directories, you can also remove multiple directories via just one command:

rm -r MyFolder01 MyFolder02 MyFolder03 MyFolder04

Creating new files via the command line

Like with creating and removing directories, there is also a lot possible via the command line for creating and removing files. So let’s start with creating files.

We can create an empty text file via the touch command. The touch command is primarily used for changing the timestamp of a file, but can be used for creating files as well. Just type for example the below:

touch MyNewFile001.txt

Another way of creating a file is via the echo command. 

Type for example the following command:

echo > MyNewFile002.txt

The nice option here is that you can directly type some content that must be stored in the file. Type for example the following command:

echo "Here you can provide your text to be stored in the new file to be created." > MyNewFile003.txt

You can look at the content of the file via the following command:

cat MyNewFile003.txt

To create a new file, it is also possible to use the same cat command as above, which stands for concatenation. The cat command is more known for looking at the content of the file but can be used for creating a file as well. The nice thing here is that you can directly add some text to the file in the same action. Type the below:

cat >> MyNewFile004.txt

When you hit enter after this command, you will see that there is an empty line where you can add text to the file. If you don’t want to add text now, just use the keyboard command ctrl + D to save and leave the file.

Removing files via the command line

Removing a file is something you have to do with care because you want to be sure that the files are not important anymore.

To remove a file you can use the rm command:

rm MyNewFile004.txt

As we saw with deleting multiple directories, you can also remove multiple files via just one command:

rm MyNewFile001.txt MyNewFile002.txt MyNewFile003.txt

Permanently removing vs using the Trash bin

It is important to know that all of the above removal commands will result in a permanent removal. So you will not find these removed folders and files in the trash bin, from where you can undo the removal. If you want to have a function to move removed files and folders first to the Trash, you must install trash-cli. Type the following command to install the trash app:

sudo apt install trash-cli

When the Trash app has been installed, just type for example the below to trash a file:

trash-put MyNewFile002.txt

When you want to restore the file, just type:

trash-restore

Now you will see an overview of all files in the trash. Here you can select which file(s) you want to restore:

   0 2024-07-12 16:37:51 /home/johnbeen/MainFolder/MyNewFile005.txt
   1 2024-07-12 16:22:54 /home/johnbeen/MainFolder/MyNewFile003.txt
What file to restore [0..1]: 1

Related reading

This article is part of a (work in progress) tutorial series on the Linux Command Line. Have also a look at my previous articles:

Or look at the complete overview:

Linux Command Line for Beginners – Tutorial series

Final words

Ok. We reached the end of this tutorial on creating and removing directories and files via the command line. I hope you have enough information and some basic understanding to do some simple things on your own. Much more is possible in the field of creating and deleting directories and files, such as using the find function and various deeper criteria in addition to the basic commands, but because this is a beginner tutorial, that is out of scope for now.

Probably you think now that we could do most of the above also with a file manager with a graphical user interface. And of course, you are right. But I hope you can see that for example combining multiple directory structures in one command, is something you can’t do with the standard graphical file managers. Next to that, you should see this tutorial as a step up or a foundation for more complex activities that we can do with Terminal-related commands. At the end of this complete (in progress) tutorial series, you will be able to combine terminal commands to do navigation, create, copy, move, and delete folders and files, and do even far more complex stuff.

To be continued…


Have a look at my latest book on Linux, in Paperback or Kindle format.

For more info on my book click here.

When you want to buy the book click on the image below.


If you appreciate what I do on this website…,

User Avatar

About John Been

Hi there! My name is John Been. At the moment I work as a senior solution engineer for a large financial institution, but in my free time, I am the owner of RealAppUser.com, RealLinuxUser.com, and author of my first book "Linux for the rest of us". I have a broad insight and user experience in everything related to information technology and I believe I can communicate about it with some fun and knowledge and skills.

View all posts by John Been →