Root to Linux: Hands-on Commands Part 2

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 comma…


This content originally appeared on DEV Community and was authored by Christina Gorton

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 set up, you can use an online command shell to practice these commands. For this tutorial I used JSLinux.

Making Changes to Your Directories

Directory hierarchy

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:

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

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

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:

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

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:

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

To view the content you can use the cat command and file name.
Using cat command to see contents of a file

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
Content displayed with the less command

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.


This content originally appeared on DEV Community and was authored by Christina Gorton


Print Share Comment Cite Upload Translate Updates
APA

Christina Gorton | Sciencx (2021-12-17T21:03:30+00:00) Root to Linux: Hands-on Commands Part 2. Retrieved from https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/

MLA
" » Root to Linux: Hands-on Commands Part 2." Christina Gorton | Sciencx - Friday December 17, 2021, https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/
HARVARD
Christina Gorton | Sciencx Friday December 17, 2021 » Root to Linux: Hands-on Commands Part 2., viewed ,<https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/>
VANCOUVER
Christina Gorton | Sciencx - » Root to Linux: Hands-on Commands Part 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/
CHICAGO
" » Root to Linux: Hands-on Commands Part 2." Christina Gorton | Sciencx - Accessed . https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/
IEEE
" » Root to Linux: Hands-on Commands Part 2." Christina Gorton | Sciencx [Online]. Available: https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/. [Accessed: ]
rf:citation
» Root to Linux: Hands-on Commands Part 2 | Christina Gorton | Sciencx | https://www.scien.cx/2021/12/17/root-to-linux-hands-on-commands-part-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.