How To Create Files And Directories Using The Command Line [Linux Basics]
# tutorial# how to

How To Create Files And Directories Using The Command Line [Linux Basics]

Sometimes, you don't always have a GUI (Graphical User Interface) when working with computers, we're all so used to having a screen, keyboard and mouse hooked up to a computer that it's become second nature creating files and directories.

One of the most basic Linux command line commands that you can run is the one used to create files, it's super handy and useful when working with remote servers, such as the server that your website runs on.

I get it, graphical interfaces can be great, and they can look great as well, but personally, I find it quite satisfying using the command line to create files and directories, not to mention it's a tool you can use to impress your friends with as well!

In this article, I'm going to teach you just how to create files and directories using the command line.

Firstly...

How Do I Create A Directory?

how to create directory

Creating directories is going to be one of the first steps required before you can start creating files.

Why?

Because you're likely going to want to organise your files in several directories to make them easier to find right?

In this example, we're going to assume that you want create a directory for your todos so that you can keep track of everything you need to complete, to create a directory, you're going to use the mkdir command like this:

mkdir todos

Simple right?

Creating Multiple Directories Using The Command Line

how to create multiple directories

Let's take this a bit further, your To Do items aren't really going to make much sense being listed out inside that directory, you're likely going to want to create a directory for each of the days of the week...

So that you can organise everything that you need to do on each of those days, so to do this, you're going to need to create several directories within the todos folder that we made...

First, you're going to need to use the cd command to move into the directory:

cd todos

Now, we can go ahead and create multiple directories from the command line within our todos folder like so:

mkdir monday tuesday wednesday thursday friday saturday sunday

Each of the directories that you've just made will be empty, you can verify and check this by running the ls -l command from your terminal to find out.

Creating Empty Files Using The Command Line

create empty files from command line

Okay, sweet, you're doing great so far! You've got a todos folder with the days of the week inside of that folder.

Now we need to go ahead and actually start creating the To Do items that you need to complete on each of those days right?

Let's say for instance that on three of the days you've got a 9:00am meeting at work, so we're going to need some kind of file to tell us that for each of these days:

  • Monday
  • Wednesday
  • Friday

Then, on the weekend, you've got a 1:00pm afternoon coffee with a friend on Saturday, and then an evening meal with your family at 5:00pm on Sunday.

Right, so you've got quite a few items to create right?

To create empty files, we use the touch command commonly found on almost all computers, it's installed by default and just works, let's create an empty file for our 9:00am Monday call inside of our Monday folder:

touch monday/9am-call.txt

Notice how we didn't use the cd command to move into the directory first? You could've made the file that way, but it's easier to stay here.

Let's Create Multiple Files

In a similar way to how we made multiple directories, we can do the exact same when it comes to creating multiple files as well.

So that's exactly what we're going to do next, since we've already created Monday's to do items, let's create the other days of the week...

touch wednesday/9am-call.txt friday/9am-call.txt saturday/1pm-coffee.txt sunday/5pm-meal.txt

Sweet! Once you've got all of your files, now we just need to add something to each of them right?

Creating Files With Text

create files with text

Whether you're still following the example of creating To Do items with me, or have skipped ahead because you want to create a file with text using the command line, this is how you can do that...

In the case of our To Do items, we now need to populate each of the files with something, it can literally be any text you'd like, but take note, what we're about to do will overwrite any contents within the files, so be careful.

We can utilise the echo command and the > operator to achieve this, I'm going to show you one example, and you can complete the rest!

echo "9am Morning Call" > monday/9am-call.txt

This command is great, however, there are some issues with it, when you're using the > operator, it will create or overwrite a file, if the file already exists (which it will in our case) the contents will be overwritten.

A safer option will be to use the >> operator instead since this will create a file and append text to it, or if the file already exists, will append the text to that file.

Conclusion

conclusion

If you're so used to creating files and directories by right-clicking and creating either a file or folder, then creating them from the command line may come as a shock to you, maybe you never knew that this was even possible!

It's a useful skill to know since you're likely going to be needing to create files and directories from the command line when accessing your server, so it's handy to get to grips with.

We've got another article that goes over the essential Linux commands that you should know, so feel free to read up on that as well!

I hope you enjoyed this article and found it useful, if you did, feel free to share it and make sure to check out our other blog articles as well!

More posts

The role of website uptime monitoring in ensuring customer satisfaction

As a business owner, ensuring customer satisfaction should be a top priority. One key aspect of customer satisfaction is having a reliable and efficient online presence, which is where website uptime monitoring comes in.

Read more
Website uptime monitoring for e-commerce stores what you need to know

As an e-commerce store owner, you understand the importance of having a reliable and efficient online presence. Let's look at why website uptime monitoring is so important for e-commerce. Read more today.

Read more
Maximizing the efficiency of your website uptime monitoring strategy

As a website owner, uptime monitoring is crucial to ensure that your website is always available and functioning properly. In this article, we'll discuss how to maximize the efficiency of your website uptime monitoring strategy, including the use of tools, setting up alerts, and optimizing your website's performance.

Read more

Subscribe to our PRO plan.

Looking to monitor your website and domains? Join our platform and start today.