This is the 6th post of a series for teaching cyber-security in a coding-club. Read [part 5].
If you are teaching this course you can clone the files and directories to follow along. The files can be found here: https://github.com/alphydan/hacker-tools-m1-u1
Navigating directories using the Shell?
Bash provides a widely used command called cd
or change directories command that allows the user to navigate through directories.
In order to see how it works, perform the following:
- Click on the terminal window
- Type
ls
to list the current directory contents, return key - Type
cd dir1
to enter the dir1 directory, return key - Type
ls
to list the dir1 directory contents, return key
If you open the dir1
folder, it should match the contents you got from the ls
command.
That's it, you've learned how to enter a directory in bash scripting.
Command: cd
Definition:
The cd
command or change directories command allows you to go inside a directory by adding its name as an argument:
cd name_of_directory
At the top of this page, you entered a directory using cd dir1
. Can you guess how to get back to the main workspace
directory?
Remember the double dots?
When we executed the ls -a
command, we noticed a set of single and double dots: . ..
As we previously mentioned these dots are utilities that bash uses to help navigate the file system.
Going back one directory level
Having entered the dir1 directory in the previous section, we may then want to get back out of it:
- Execute the
pwd
command to confirm we are on the dir1 directory - Execute the
ls -a
command to confirm that we indeed have single and double dots available - Execute
cd ..
to exit the current directory and go back 1 directory (to the parent directory) - Execute
pwd
to confirm that we are back on the/home/user/workspace/
directory
Testing your knowledge:
Can you confirm that you are at
/home/user/workspace/
or the equivalent directory in your computer? Can you then change directory insidedir1
and exit again?
Reading files
Reading the content of files
Once you enter a server, how can you read what is inside a file if you don't have a screen?
Lets do that:
Execute
cd dir1
on the shell to move to the dir1 directory.What is inside the directory? let's find out: execute
ls
You should see
file1.txt
The end of the file .txt
tells us that it's a text file. But how do we read them? For that we use cats!
:
Command: cat
or 'Concatenate'
Definition: The cat
command shows the content of text files.
To Concatenate means to put one thing next to another. In the Shell it means to print the contents of the file next to each other on the shell output. Let's try it:
- move to the dir1 directory using
cd dir1
- print a list of the files using
ls
- print the content of the file with
cat file1.txt
Your terminal should have printed a bunch of numbers. file1.txt
actually contains a list of prime numbers (which are very important in cyber-security).
What about making files?
Once you get into the server, you will have to prove that you were in there. A way to do so is to leave a file behind. Let's create a file.
- Move to the dir1 directory
- Make sure we are inside dir1 with the
pwd
command - Execute:
touch file123.txt
Command: touch
Definition: if any file does not exist, the touch
command will create one for you.
Additionaly, the touch
command needs an argument, in this case, the argument is the name of the file to be created, don't forget the file extension:
touch file.extension
What is a file extension? File extensions are also known as file formats. You may be familiar with .doc, .docx, .xls or .xlsx
for Microsoft Word and Microsoft Excel respectively. In most cases file extensions determine the program that should or can read the file.
End of Unit 1
So far we've learned 6 bash commands:
whoami
pwd
to print working directoryls
and the optionsls -a
,ls -l
(Go ahead and try 3 options at once in the terminal:ls -alh
: which means: list --all --list --human-readable)cd
to change directorytouch somefile.txt
to create some file.cat somefile.txt
to see the content of the file
and finally some navigation tricks like cd ..
to move up a directory level or cd ~
to move back to the home folder.
We know that the command line interface, CLI for short, is also known as the shell or command terminal as well. Bash in the other hand, runs inside the terminal and is in charge of recognizing, interpreting and executing the text commands written in the shell.
So now you know how to interact with a server through the shell. But how do we get in?
More about that in the next unit.
Congratulations @alphydan! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Continue reading Part 7 of this course here: https://steemit.com/linux/@alphydan/teaching-cyber-security-part-7
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit