Linux Commands You Need to know Part 2: Working with Files
Tags: tutorial, linux, learning, beginners
Published: 2022-04-11 17:42:45

---
title: Linux Commands You Need to know Part 2: Working with Files
published: true
description: Getting started with Linux? In this series we're going to learn helpful Linux commands so you're not lost at the command prompt. In this article we'll work with files.
tags: #tutorial #linux #learning #beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fr03y1and3h16jo399eo.jpg
canonical_url: https://www.allhandsontech.com/linux/linux-commands-files/
series: Learning the Linux Command Line
---

In the last article in our series, [Linux Commands You Need to Know - Navigation](https://dev.to/pluralsight/linux-commands-you-need-to-know-part-1-navigation-15k5) 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 <a href="#mkdir">make a directory</a>
- <a href="#rmdir">Remove a directory</a>
- <a href="#cp">Make a copy of a file</a>
- <a href="#mv">Move or rename a file</a>
- <a href="#touch">Create an empty file</a>

So let's dig in!!

---
<h2 id="mkdir">1. mkdir</h2>

#### _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"][0]

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"][1]


---

<h2 id="rmdir"> 2. rmdir</h2>

#### _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"][2]

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"][3]

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"][4]

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

---
<h2 id="cp"> 3. cp </h2>

#### _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"][5]

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

`cp [file name] [new location]`

!["Helpful Linux Commands"][6]

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


---
<h2 id="mv"> 4. mv</h2>

#### _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"][7]

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"][8]

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

---
<h2 id="rm"> 5. rm</h2>

#### _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"][9]

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

`rm -rf *`

It's a handy command for removing files.

---
<h2 id="touch">6. touch</h2>


#### _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"][10]

<h2 id="find">7. find a file (find)</h2>

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"][11]

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"][12]

I scored a 203, so I still have some room to grow. What's your score? [Take your SkillIQ now.](https://www.pluralsight.com/redeemlink/genericV4?redemptionId=7a28c861-d736-41d3-842a-28a97d8e5714&mac=368c7cca1cac8158bae0da91fa137b5d01891402f7f7119a10a6a8be938da98a&utm_source=allhandsontech&utm_medium=organic-web&utm_campaign=allhandsontech)

Have any questions? Comments? [Let me know!](https://www.twitter.com/jeremycmorgan)

--Jeremy



[0]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccca1pkatyuoly49vkjg.png
[1]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qu4yevr30mq9rhq2ucc4.png
[2]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jp9q0cngz290v4iijkl9.png
[3]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnzzgwrongi5phxser57.png
[4]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3rrrd1mbeyzhsyjurx1.png
[5]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fx6jwd91snx0xleairja.png
[6]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf72zn6dlcp3e9j81nxt.png
[7]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwc0k6zuji85mj13rc3f.png
[8]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p57dqr8q25y0560pq9v.png
[9]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2z0aojnz3d4r6nu9b5a.png
[10]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kmcjn61003oy8d8h39p.png
[11]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzqchi7io6zx8l2ezcup.png
[12]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1u8ju3ogo0k4jykwqdx.png