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.
Creating directories is going to be one of the first steps required before you can start creating files.
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:
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:
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.
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:
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:
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.
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?
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.
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!
Notification Contacts make alerting team members incredibly easy and simple, find out more about this new feature that we've released for all of our customers!Read more
Here at the Domain Monitor, we're delighted to announce that Pretty Purchases, a UK based E-Commerce website that specialises in selling handbags has joined our website monitoring tool, learn more and find out how you can get a 5% discount today!Read more
If you've ever received a notification from our website uptime monitoring tool, you'll know how scary it can be to find out your website is down, your stomach literally drops and you start to panic, is your website actually down? Here's what to do when your website goes down.Read more
Plus, more great content, product news and updates