Welcome!
Linux Commands
Here you will learn about some common Linux Commands.
The commands you learn in this module are not all the commands that Linux users or hackers use but some of the most common ones. It is important for you to practice this commands as many times as necessary, until you are very familiar with them.
In this training session, we will introduce this commands using a scenario of superheroes.
© CyberShaolin. All rights reserved.
Congratulations!
You've completed the scenario!
Scenario Rating
In this course, you have learned to use some common linux commands.
Hope you had fun learning! :-)
Your environment is currently being packaged as a Docker container and the download will begin shortly. To run the image locally, once Docker has been installed, use the commands
cat scrapbook_cybershaolin_intro-to-linux/linux-commands-training_kwoon_container.tar | docker load
docker run -it /cybershaolin_intro-to-linux/linux-commands-training_kwoon:
Oops!! Sorry, it looks like this scenario doesn't currently support downloads. We'll fix that shortly.

Steps
Linux Commands
Start
Here you will learn about some common Linux Commands.
The commands you learn in this module are not all the commands that Linux users or hackers use but some of the most common ones. It is important for you to practice this commands as many times as necessary, until you are very familiar with them.
In this training session, we will introduce this commands using a scenario of superheroes & villains.
The scenario is as follows:
The EndGame virus has wreaked some havoc in your Linux computer system. It has hidden some files and created some duplicates of the real original files. It has also removed some folders that we need to recreate and extract some files that have been compressed.
Are you ready for learning?
Don’t worry, we will help you through this exercise, step by step.
pwd (Print Working Directory)
pwd
stands for Print Working Directory.
Some examples of directories in the Linux OS are /root, /boot, /bin, /opt, /run, /usr
and /home
.
The pwd
command tells you the current folder where you are.
The default directory that you start out with is the home directory of the user who is logged in.
If you are the root user, your first directory would be /
.
If you are a user named duke
, then your home directory would be /home/duke
Type pwd
and press enter.
You will find that you are in the /
directory as shown below.
Note:The hostname (The value after the word [email protected]
may be different than what is shown below.)
[email protected]:/# pwd
/
ls (List Directory Contents)
ls
stands for list directory contents.
The ls
command displays the names of files contained within a directory, as well as any requested, associated information.
Type ls
and hit enter.
You should see several directories. One directory that is of interest that we will use for our learning is the cybershaolin
directory.
[email protected]:/# ls
bin cybershaolin etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
cd (Change Directory)
cd
stands for Change Directory.
Type cd cybershaolin
and press enter.
What you just did was you changed your directory to the cybershaolin
directory.
You can check by typing pwd
and pressing enter.
You will now be in the /cybershaolin
directory.
Now type ls
and press enter.
You will see that there are some files related to superheroes comic companies (e.g., dc and marvel) as well as a villains file that is compressed as a tape archive (.tar file) as shown below.
[email protected]:/cybershaolin# ls
superheroes_dc1.txt superheroes_dc2.txt superheroes_dc3.txt superheroes_marvel.txt villains.tar
cd and ../(DotDotSlash))
In order to go to the previous folder, you can use the shortform ../
(DotDotSlash) along with the cd
command.
Since you are in the cybershaolin
directory now, you can go to the previous directory using the cd ../
command.
mkdir (Make Directory)
mkdir
stands for Make Directory.
It is used to create a folder or directory so that you can be organized.
You can think of directories like a bookshelf. In the top shelf you may have your school books, the middle shelf may have some fiction books and the bottom shelf may contain some activity books. So also in a computer system, you can organize your files in directories.
You notice that there are some files in there, but no folders (or directories) to organize them. So let us create two directories to organize these files.
The two directories you are going to create are a directory called dc
and another one called marvel
.
Type mkdir dc
and press enter.
What happened? It may seem like nothing happened if your command ran successfully.
To see if your directory was created successfully, you can use the ls
command which will list all the contents of the current directory where you are.
Type ls
and press enter.
You should see aa directory/folder called dc
.
Now type mkdir marvel
and press enter.
And is &&
NOTE: You could have also created both the directories in a single command using the &&
operand. The command in that case would have been mkdir dc && mkdir marvel
.
diff (Difference)
diff
or diff3
stands for Difference.
The diff
command compares two files line by line. The diff3
command compares three files line by line.
Type ls
and press enter.
You see that there are the a few text (.txt) files and the folders that you created.
We need to move the files into the respective directories, meaning the superheroes_dc
file need to be moved into the dc
folder and the superheroes_marvel
file need to be moved into the marvel
folder.
But wait, there are three superheroes_dc
text files. So which of these files is the correct file that we need to move into the dc
directory.
One way we can find out about each of these files is look into the files using the cat
command, but that would take time. So we can use the diff
or diff3
command.
Type diff3 superheroes_dc1.txt superheroes_dc2.txt superheroes_dc3.txt
and press enter.
You will see the contents of the three files that are different from each other line by line.
The first file has the word 'Flash' misspelled as 'F|ash' (note the second letter is a pipe character and not a lowercase 'L').
The third files has the word 'Batmans' and not 'Batman'.
The second file is the only file that has the names of the DC superheroes, correctly spelled.
So we will move the superheroes_dc2.txt
into the dc
folder and the superheroes_marvel.txt
into the marvel
folder, but first let us remove the two incorrect files.
rm (Remove)
rm
stands Remove.
To remove the superheroes_dc1.txt
file and the superheroes_dc3.txt
file, type rm superheroes_dc1.txt && rm superheroes_dc3.txt
and press enter.
NOTE: To remove a directory, you will use the rmdir
command. If there are files in the directory that you are trying to delete, then your rmdir
command will fail and you will see something likermdir: failed to remove 'myGames/': Directory not empty
To delete a directory that has content inside it, you can use the rm
command along with the operands called -rf
which will do a forcefully (-f) remove (rm) the listed directories and their contents recursively.
The DeathStars of the rm Commands
Never ever, never ever, never ever run the following commandrm -rf *.*
unless you absolutely know that you are in the directory from which you want to delete ALL folders and files.
The *.*
here is a wildcard syntax to mean any files of any type.
mv (Move or Rename)
mv
stands for Move
Now let us organize the files.
Type mv superheroes_dc2.txt dc
and press enter.
This command which will move the superheroes_dc2.txt
file into the dc
folder.
Type mv superheroes_marvel.txt marvel
and press enter.
Now type ls
and you will find that the text files don't exist as they did before. They have been deleted and moved into the respective directories.
tar (Tape Archive)
tar
stands for Tape Archive
The tar
command saves many files together into a single tape or disk archive, and can restore individual files from the archive.
The -x
operand is used to extract files from an archive.
The -v
operand is used to verbosely list the files that are processed.
The -z
operand is used to filter the archive through gzip, a compression utility.
The -f
operand is used to use the archive file.
Type ls
and press enter.
You will find notice that there is a compression tar file called villains.tar
that we would like to untar (uncompress).
Type tar -xzvf villains.tar
and press enter.
You will see that the files in the villains.tar archive got extracted into a directory called villains
and the name of the file that got extracted into that folder is README.txt
(as shown below)[email protected]:/cybershaolin# tar -xzvf villains.tar
villains/
villains/README.txt
cat (Concatenate)
cat
stands for Concatenate
It is the command that is used to concatenate (read and adds) and print the contents of a file.
The cat utility reads files sequentially, writing them to the standard output (terminal).
To read what the README.txt
contents in the villains directory, you can first navigate into the villains
directory using the cd
command and then use the cat
command to read the contents of the README.txt
file.
Type cd villains
and press enter.
Then Type cat README.txt
and press enter.
You will see the following text print on the screen.The villians list that you are looking for is not here.
It must be hiding somewhere.
Pay careful attention as you search... for even a dot can mean a lot.
Can you try to 'locate' the villains file?
Note: The cat
command is used extensively in managing files in Linux.
locate
locate
stands for Locate
The message in the README.txt
file suggested that the file may be hidden. It also stated that a dot means a lot so let us try to search for the villains fill with a dot in front.
You can use the locate
command if you know the name of the file or directory that you are trying to find (locate).
You can try to type locate .villains
and press enter to find the hidden villains list.
You should see that a file called .villains.txt
is hidden in the /cybershaolin
directory as shown below.[email protected]:/cybershaolin/villains# locate villains
/cybershaolin/.villains.txt
/cybershaolin/villains.tar
Hidden Files (using ls -a)
Files that are named with a dot (.)
are hidden and not shown when you run the ls
command.
To see hidden files, we need to modify the ls
command with some additional information. This additional information that is added to a Linux command is called an operand.
Type ls -a
and press enter.
The -a
operands will list directory entries whose names begin with a dot (.) and are hidden.
file
file
stands for File (Determination)
The file
command helps to determine the type of Files.
Because we know that the villains file is in the cybershaolin
directory. You have to run cd ../
to go back to the cybershaolin
folder since you are inside the villains
folder.
Now we learned earlier that in order to read the contents of a file, we can use the cat
command.
Type cat .villains.txt
and press enter, once you are in the cybershaolin
folder.
You will notice that some text is displayed on the screen but it is not humanly readable (kinda looks garbled).
Aha, the villains have not only hidden the file, but also tried to hide (obfuscate) the contents of the file by changing the type of the file.
A .txt
file should be able to be opened by the cat
command, but since this did not work as expected for us, let us determine the type of the file using the file
command.
Type file .villains.txt
and press enter.
You will notice that the file type is a zip
(compressed) file, with a .txt
file extension.
Next we will need to unzip
this file to see what the file contains, but before we can do that, we need to rename the file from the .txt
extension to the .zip
extension.
Rename using Move (mv)
To rename the file we will interestingly use the mv
command. The mv
command that you learned earlier can be used for two reasons.
- Move source(s) to directory and
- Rename the Source to Destination
Type mv .villains.txt villains.zip
and press enter to rename the file from .villains.txt to .villains.zip.
Technically what the mv
command did in this case was it renamed the villains.txt
file to village.zip
file.
While we are renaming the villains.txt
file we might as well move it to the villains
folder.
Type mv villains.zip villains/
and press enter.
unzip
unzip
stands for Unzip
The unzip
command is used to extract all contents of a zipped
file.
Since we have a zip file renamed villains.zip
in the villains
directory, we will navigate into the villiains
directory and unzip the villains.zip
file.
Type cd villains
and press enter.
Type unzip villains.zip
and press enter.
You will see this text appear on the screen.
Archive: villains.zip
inflating: villains.txt
This is saying that the folder (archive) villains.zip
is decompressing (inflating) to reveal villains.txt
.
Type ls
to see if the villains.txt
file is there.
file
Why don't we use the file villains.txt
to make sure and guarantee that this is really a text file. You should see villains.txt: ASCII text
to show that this truly is a text file.
grep (Global Regular Expression Print)
grep
stands for Global Regular Expression Print
We can use the grep
command to search specifically for a word or phrase in a text file. If you want to search for one word in a extremely huge document you can use the grep
command.
Since we know that this list contains villain names we can search for a villain using the grep
command.
Type grep 'Joker' villains.txt
to see if the Joker is a part of the villains list.
To see a list of all the villains in the villains.txt
file, which of the Linux commands that you have learned can you use?
If you answered cat
, you are correct.
Type cat villains.txt
and press enter.
The End
In this course, you have learned to use some common linux commands.
The end result of this training kwoon should have all the superhero and villain text files organized in their respective folders as shown below.
. .. dc marvel villains villains.tar
Hope you had fun learning! :-)