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.
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.
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
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.
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.
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.
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:
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.
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
cat > partners.txt
For more practice, navigate to the other partner.txt files and add content to them.
Wentworth/partner.txt add the following content:
Captain Wentworth marries Anne Elliot.
Darcy/partner.txt add the following content:
Mr. Darcy marries Elizabeth Bennet.
In part 2 of this hands-on guide you learned the following basic commands:
mvto move directories
cpto copy directories or files
rmto delete files (if you want to delete a directory and all of it's contents use the
rmcommand with the
rm -r directory_name
catto 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.