DEV Community

Cover image for Linux Commands You Need to know Part 2: Working with Files
Jeremy Morgan for Pluralsight

Posted on • Originally published at allhandsontech.com

Linux Commands You Need to know Part 2: 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:

So let's dig in!!


1. mkdir

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.

usage:

mkdir [directory name]

Here's mkdir in action:

"Linux Commands for Working with Files - mkdir"

You can even make a directory within a directory, even if the base one doesn't exist with the -p option.

Here I will create a new directory called test2 within a test directory, with the -p option:

"Linux Commands for Working with Files - mkdir -p"


2. rmdir

Purpose: to remove a directory

With the rmdir command, you can remove a directory quickly and easily.

usage:

rmdir [directory name]

Here's rmdir in action:

"Helpful Linux Commands"

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:

"Helpful Linux Commands"

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]

"Helpful Linux Commands"

Note: this will delete all files and directories within the directory.


3. cp

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.

usage:

cp [file name] [new file name]

"Helpful Linux Commands"

You can also copy the file to another directory and keep the same file name:

cp [file name] [new location]

"Helpful Linux Commands"

This is a great way to make copies of a file. But what if I want to move it?


4. mv

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.

usage:

mv [file name] [new location]

It's used the same way as cp, though it moves the file instead of making a copy.

"Helpful Linux Commands"

This is also how you rename a file.

usage:

mv [file name] [new file name]

So if I want to rename my nginx configuration file, I can do this:

"Helpful Linux Commands"

And it's done. What if I want to remove it?


5. rm

Purpose: to delete a file

We used rm earlier to remove a directory. It's also you delete individual files.

usage:

rm [file name]

"Helpful Linux Commands"

You can remove all the files in a directory with the following:

rm -rf *

It's a handy command for removing files.


6. touch

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.

usage:

touch [file name]

This creates a file with nothing in it:

"Helpful Linux Commands"

7. find a file (find)

This is a powerful command for finding files in the file system.

usage:

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.

"Helpful Linux Commands"

Now I can see exactly where my file is located with a simple command.

Summary

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:

"Helpful Linux Commands"

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!

--Jeremy

Top comments (0)