Author: Jeremy Morgan
We are streaming LIVE now on Twitch. Join us!
Linux Commands Part Two: Working With Files
In the last article in our series, Linux Commands You Need to Know - Navigation we learned how to travel around the file system in Linux. Today, we’re going to learn how to work with files.
We will learn:
- How to make a directory
- Remove a directory
- Make a copy of a file
- Move or rename a file
- Create an empty file
So let’s dig in!!
Purpose: to create a directory
As we covered in the last article, folders in Linux are called “directories”. They serve the same purpose as folders in Windows.
mkdir [directory name]
Here’s mkdir in action:
You can even make a directory within a directory, even if the base one doesn’t exist with the
Here I will create a new directory called test2 within a test directory, with the -p option:
Purpose: to remove a directory
rmdir command, you can remove a directory quickly and easily.
rmdir [directory name]
Here’s rmdir in action:
Now this works great if the directory is empty. But what about the directory I created that has another directory in it? Here’s what happens when I try to use rmdir on that directory:
rmdir cannot remove directories that have files or directories in them. To do that, you must use the rm command (which we’ll cover again in command #5 )
Do that we need to type in
rm -rf [directory name]
Note: this will delete all files and directories within the directory.
Purpose: to make a copy of a file
Here’s one you’ll use all the time, especially if you’re making a config file backup. Let’s use that as an example. I want to make a backup of this file. If I mess something up, I can go back to the old version.
cp [file name] [new file name]
You can also copy the file to another directory and keep the same file name:
cp [file name] [new location]
This is a great way to make copies of a file. But what if I want to move it?
Purpose: to move a file to another location or rename it
This one is pretty straightforward. You use it to move a file from one place to the other.
mv [file name] [new location]
It’s used the same way as cp, though it moves the file instead of making a copy.
This is also how you rename a file.
mv [file name] [new file name]
So if I want to rename my nginx configuration file, I can do this:
And it’s done. What if I want to remove it?
Purpose: to delete a file
We used rm earlier to remove a directory. It’s also you delete individual files.
rm [file name]
You can remove all the files in a directory with the following:
rm -rf *
It’s a handy command for removing files.
Purpose: create an empty file
You may have noticed my “nginx.conf” was zero bytes. This is a nifty command for creating empty files. This is handy for creating a new file or testing things.
touch [file name]
This creates a file with nothing in it:
7. find a file (find)
This is a powerful command for finding files in the file system.
find [path to search] -name filename
Let’s say I want to find my hello world code. I know the filename. I just don’t know where it’s located.
Now I can see exactly where my file is located with a simple command.
In this article, we learned how to work with files. Working from the command prompt can be very fast and efficient, so now we know how to create, move, copy, and rename files from the command line.
Please keep checking back to this site as we add to this series. We’ll get into some advanced command line topics that will have you feeling like a Linux wizard in no time.
You can test your Linux skills to see where you’re at:
I scored a 203, so I still have some room to grow. What’s your score? Take your SkillIQ now.
Have any questions? Comments? Let me know!