How to create a file and directory in Linux


This article is all about how to create a file in Linux and how to create a directory or folder in Linux operating system using the different type of methods. In the previous article, we learn about ls command for Linux operating system. ls command is the very helpful command for Linux which is briefly described by us in the previous article. Now here we describe much more commands for Linux operating system. This article is really helpful for beginners who shifted from Windows to Linux operating system. 

In this article, you can learn about creating files and directories using some simple commands no matter on Which distribution of Linux you worked. These commands work on all the distros of Linux like Debian, Fedora and any other distribution. There is mainly one command used in Linux for creating dirctories which are mkdir and we generally use four methods to create files which are touch command, cat command, Vim command, and nano command. In which vim and nano are editors they are generally used for creating files and for editing also. So here we only explain cat and touch command for creating directories. By using these commands you can able to use Linux more easily than others. So, let’s start here how it works.

Also read: – Some useful commands for list files and directories in the Linux {ls command explained}

Command to create a directory/folder in Linux

mkdir

mkdir command is the full form of the make directory, and it is used to create the directory or folder in the Linux operating system. And the structure of command to create the directory is as following.

mkdir<space>directory name

First, we will see that how many directories are present at the location before you make a new directory using a list command ls.

root@kali:~# ls
a Documents f Pictures r.mp4 Templates
Desktop Downloads Music Public satish Videos

Now we will create a new directory of a student using mkdir command in the terminal box.

root@kali:~# mkdir student

Now your directory of a student is created and you can check it using many of the list commands.

root@kali:~# ls
a Documents f Pictures r.mp4 student Videos
Desktop Downloads Music Public satish Templates

Now you can see there is a new folder of a student, and the directory of the student is successfully created.

Command to create an empty file

touch

Touch command is used to create an empty file in the Linux operating system. There will be no data inside the file which is created by the touch command. And the structure of command to create an empty file is as following.

touch<space>filename.txt

Now we will create an empty file inside a student directory and name it as a.txt.

In the first, we will go to the student directory where you want to create an empty file.

root@kali:~# cd student
root@kali:~/student#

Now we will create an empty file using touch command.

root@kali:~/student# touch a.txt

Now your file a.txt is successfully created, and you can check it whether it is created or not by list commands.

root@kali:~/student# ls
a.txt

So it is showing that a.txt is created, and now you can see the content of the file using cat command.

root@kali:~/student# cat a.txt
root@kali:~/student#

Now it is showing that the file doesn’t have any content inside it, so it is an empty file.

Command to create a file with some content in it

cat command in Linux

cat command is used to create a file with the content inside the file, and it is also used to saw the content of the file. And the structure to create a file with content inside is as following.

cat<space>>filename.txt

Now we will create a file inside a student directory and name it as b.txt.

First, you will go to the student directory using above given instruction, and then you will create a file name as b.txt.

root@kali:~/student# cat >b.txt

After type above command, you will press enter and then write the content inside it such as.

root@kali:~/student# cat >b.txt
hello 
how are you?

Now to save the file you will press ctrl+D, and then your file with your content will be saved as b.txt in the student directory. And now you can see the content of the file using cat command such as.

root@kali:~/student# cat b.txt
hello 
how are you?

Now you can see that the file is created successfully with a content inside it.

Command to saw the content of the file

cat Command

As we have studied above that cat command is used to create a file with content inside, and it is also used to saw the content of the file but its structure is slightly different from the above structure. And the structure of command to saw the content of the file is as following.

cat<space>filename

Here above we have created a file name as b.txt with content inside it, so we will saw the content of this file using cat command such as.

root@kali:~/student# cat b.txt
hello
how are you?

So you can see the content of the file using cat command. and there is one more command to saw the content of the file.

See the content of the file using more command

more command is also used to saw the content of the file, it is another option to see the content of the file. And the structure of command to saw the content of the file is as following.

more<space>filename

Now we will see the content of the file name as b.txt using more command which we have created above.

root@kali:~/student# more b.txt
hello 
how are you?

So now you can see the content of the file using more command.

                                                                       THAT’S IT

These are the commands by which you can create all the types of directories and files in Linux. By using these commands you can easily operate Linux operating system. I think this article is helpful for you. If you have any queries regarding this then simply solved out through the comment section. Also, provide feedback to us because your feedback is valuable for us. Subscribe and follow our blog for further updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s