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
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.
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.
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.
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.
To view the content you can use the cat command and file name.
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.
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 therm
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.
Top comments (5)
FYI, if you're using Win 10 or Win 11, you can install "windows subsystem for Linux" (which enables Windows OS syscalls to work with Linux syscalls) then install your specific distro (eg. Ubuntu). So you can try these commands directly in your windows machine.
Other alternative, you can rent a vps (eg. Digital ocean $5/month), install Ubuntu and give Linux a shot, without messing up your PC setup
Nah, just ditch Windows and move to Linux ;) Lmao
Virtual machines are also an option, especially if you want to toy around and play with different distro's.
WLS2 is really nice if you just want to play around with Linux command line, but VM's and live booting distro's off a USB can be another fun way to play around and get familiar with the ecosystem. Desktop flavors like Gnome, XFCE, Cinnamon, KDE, and the many more (not to mention the whole world of window tiling managers that exist) make Linux such a great a fun place to be!
"live booting distro's off a USB"
Yeah, this is how I have been learning with a few coworkers. We are starting with Gentoo and working through the handbook to install it.
I'll eventually work on some easier Linux distros (I think we are going to do Fedora) and try out a VM so I know how to do all of those things :)
Nice. Gentoo is really good. So is Fedora. I've been using Manjaro as my daily driver for a few years and am totally in love. Playing around with differnt distrobutions and seeing what they have to offer is one of the things that's so fun and great about the linux ecosystem. So many different flavors, you get to experiment around and see what one works best for you. It's also something that can feel quite overwhelming for a lot of people, so vm's and live usb's are a great way to get ease into the ecosystem.
Always remember:
π€£
When using
cat
orecho
to add text to a file, you can also use>>
to only append the new text to the file, without completely erasing it. :)So cool to see you writing about Linux! β€οΈ Can't wait to read more, great tutorials!