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
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:
ls to list the current directory contents, return keycd dir1 to enter the dir1 directory, return keyls to list the dir1 directory contents, return keyIf 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: cdDefinition:
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?
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.
Having entered the dir1 directory in the previous section, we may then want to get back out of it:
pwd command to confirm we are on the dir1 directoryls -a command to confirm that we indeed have single and double dots availablecd .. to exit the current directory and go back 1 directory (to the parent directory)pwd to confirm that we are back on the /home/user/workspace/ directoryCan you confirm that you are at
/home/user/workspace/or the equivalent directory in your computer? Can you then change directory insidedir1and exit again?
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:
cd dir1lscat file1.txtYour terminal should have printed a bunch of numbers. file1.txt actually contains a list of prime numbers (which are very important in cyber-security).
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.
pwd commandtouch file123.txt
Command: touchDefinition: 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.
whoamipwd to print working directoryls and the options ls -a, ls -lls -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 fileand 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.