Root to Linux: Hands-on Commands Part 2
Tags: linux, tutorial, beginners
Published: 2021-12-17 21:03:30

## Introduction
In part 1 of this guide, you will learned some of the basic Linux commands to see your working directory, list directories and files, make new directories, and create text files.

In part 2 of this guide you will learn the commands to move directories, copy files, add content, and delete files. 

## Prerequisites
You will need a Linux shell to work with to try these commands in this tutorial. If you do not have a [Linux distro](https://en.wikipedia.org/wiki/Linux_distribution) set up, you can use an online command shell to practice these commands. For this tutorial I used [JSLinux](https://bellard.org/jslinux/).

## Making Changes to Your Directories

![Directory hierarchy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pm5e1kcgr4bdc3t3lfjo.jpg)

Look at the directories above. 
In part one of this guide you created the top directory hierarchy. When you created the original directories you added the Elliot directory under `Pride_prejudice`. 

Sometimes when you create directories or files you may find that they are in the wrong place and you need to move them. The character Elliot is actually in Jane Austen’s book Persuasion not Pride and Prejudice.  

### Moving a Directory
Instead of deleting the Elliot directory and creating a new one under Persuasion, you can use the `mv` command to move the directory. When using the `mv` command you add the file path you want to move, then the file path you want to it move to. 

In the Linux shell type:
```bash
mv austen/Pride_prejudice/Elliot/ austen/Persuasion/
```
This will move the Elliot directory from the Pride_prejudice file path to the Persuasion file path. You can navigate to the Persuasion or Pride_prejudice directory and use the `ls` command to see that the file was moved.

![Moving a directory ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efvwup8fus82zyj0keg0.png)

### Copying a File
Look at the image of the directories again. You can see that in the top directory there is no partner text file under the Pride_prejudice directory. 

In the bottom directory there is a partner.txt file under `Pride_prejudice/Darcy`. You could navigate to that directory and use the `touch` command to create the file. 

However, in this section you will use the `cp` command to copy the `partner.txt` file that is under the Persuasion/Wentworth directory instead.

Currently these files are empty but if they had a lot of information in them it would be more efficient to copy the file instead of creating a new one with the `touch` command and adding the information again.  

In the Linux shell type the `cp` command followed by the file path you want to copy and then the file path you want to copy it to. 

In this case you will copy the file from Wentworth to Darcy.

![Copying a directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9skhgfdmvuk6cq4gk49.png)

### Deleting a File
Another important command to know while working in the Linux shell is how to delete a file or directory. 

Look at the top and bottom directories again. 
In the top directory you can see the `partners.txt` file under Churchill. This file is under Knightly instead in the bottom directory.

Delete this file by using the `rm`(remove) command. Be sure to use the correct file path when deleting a file. For this example type:
```bash
rm austen/Emma/Churchill/partner.txt
```
After you delete the file, change directories into Churchill and use the `ls` command to see if the file was removed. If no files are listed then your file was successfully deleted.

![Deleting the partners.txt file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jw1lheuiddcya2m87l7.png)

### Editing a Text File
So far you have created three empty `partner.txt` text files. Now you will use the `cat` command to add text to the files. The `cat` command is one of the more frequently used commands in Linux. It can be used to create single or multiple files, view the content of those files, concatenate files, and redirect output in terminal or files.

In this guide you will use it to add content to a file and to view the content of the file. 

Change directories to `austen/Emma/Knightly`.

Then type:
```bash
cat > partners.txt
```
You will be able to type content to the shell. Type `Mr. Knightly marries Emma` into the shell. To exit type `Ctrl + d` from the keyboard.
![Using cat command to add content to file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7yywjjussnt3jyymo7o.png)

To view the content you can use the cat command and file name. 
![Using cat command to see contents of a file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10c674yb6vribxx8jdii.png)

Alternatively, you can use the `less` command to view content of a text file. 
If you use the `less` command you will need to type `q` on your keyboard to exit.
![Less command to see content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6ohoiof3ck2cgjfgnwp.png)
![Content displayed with the less command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzearly63zxz9obmc05o.png)

For more practice, navigate to the other partner.txt files and add content to them. 

In `Wentworth/partner.txt` add the following content:
`Captain Wentworth marries Anne Elliot.`

In `Darcy/partner.txt` add the following content:
`Mr. Darcy marries Elizabeth Bennet`. 

## Conclusion
In part 2 of this hands-on guide you learned the following basic commands:
- `mv` to move directories
- `cp` to copy directories or files
- `rm` to delete files (if you want to delete a directory and all of it's contents use the `rm` command with the `-r` (recursive) option: `rm -r directory_name`
- `cat` to edit a text file or view it's content

If you have worked through both of the hands-on guides take the time to go back through all of the commands to reinforce what you have learned.