Hey everyone! I know it’s been quite a while but here I am.
Today I bring you a Linux Tutorial (I guess you could call it that). As most of you know, OTW had a Linux Series entitled ‘Linux Basics for the Aspiring Hacker’ back in NB and is now reposting it on his own website Hackers Arise.
It was with this tutorials (and a few others) that I started learning Linux, and I thought I should share my own notes regarding this series. Basicaly I wrote a text document (which I have now converted to a .pdf file) that’s is pretty similar to OTW’s original article, but with a few chages to make it more simple and accessible, at least for me.
Here’s the link:
As suggested, here’s goes the raw text. Hope you enjoy:
/ Opening a Terminal > Shortcut: Ctrl + Alt + T
Most Terminals default to open a shell. A shell is simply a command line environment that enables us to run commands on the underlying operating systems and write scripts. Their are many different shell environments in Linux, but the most popular is the BASH shell or Bourne Again Shell.
/ File System > Unlike Windows file system, Linux systems are not limited by the physical drive. The Linux file system has at the top of its file structure the root or /. This does not represent the physical drive, but simply the top of the logical file structure.
Notice in this diagram of the Linux file system above that at the very top of the file system is the / or root of the file system. Here, root simply is the very base of the file structure. As a beginner, probably the most important sub-directories under / are:
- /root > this represents the home directory of the super-user
- /etc > where the configuration files reside in Linux
- /home > home directory of a user
- /mnt > where other file systems are attached or mounted to the filesystem
- /bin > where the binaries or executables in Windows language reside
- /lib > directory where the libraries reside.
/ Getting started with the command line…
pwd > or
directory, returns where you are in the directory structure.
whoami > outputs the current logged in user
cd > or change directory. Crucial command to navigate around the filesystem.
cd <name_of_directory> - moves to the desired directory; cd .. - moves up one level in the file structure
ls > or
st, lists the contents of a directory (for a complete list of the contents, type
-h > or
--help. Nearly every command has a help file. These help files provide a cursory help screen to assist your understanding of the command, utility or application.
<program_name> -h or <program_name> --help
man > In addition to the help switch available for most commands and applications, you can find more information about a particular application or command by going to its
manual page. Nearly every Linux distribution maintains a manual for all utilities, commands and applications. You can view the manual by simply typing
man before the command, utility or application such as:
With man, Linux opens the manual with the
more commands, a file display command that we will look at a bit later. We can scroll through this manual file by using the ENTER key or page down using the PGDN or PGUP key. To exit, we simply type “q”.
/ Finding files in the filesystem
locate > Linux has multiple ways of finding application, commands, files, etc… from the command line. Probably the easiest to use is locate. locate, followed by a keyword, will go throughout your file system and locate every occurrence of that word. Sometimes what locate finds is overwhelming, too much information. In addition, if you just created a file, it may not appear in this list as locate uses a database that is updated once a day. A file you created today usually won’t appear in that database until tomorrow.
whereis > If we know what we are looking for is a binary (similar to an executable in Windows), Linux has a specific command for that. This command is whereis. whereis will not only return the location of the binary, but also it’s manual or man page.
which > The which command is even more specific. It will only return the location of binaries that are in the PATH variable in Linux.
find > The find command is the most powerful and most flexible of the finding utilities. find is capable of beginning your search in any designated directory and looking for a number of different parameters including, of course, file name, but also can find files that meet other criteria, such as:
- date of creation or modification;
The basic syntax for find is:
find <directory list to search> <options> <expression>
So, if I wanted to search for a file starting in the top of the file system (root) / directory with the name apache2, I would type:
find / -type -f -name apache2
- / is the directory to start searching
- -type is the type of file in this case -f or an ordinary file
- -name search by name or apache2
It’s also important to note that unlike some of the other search commands like
find only displays exact name matches. If the file apache2 has an extension, such as apache2.conf, it will not match. We can remedy this limitation by using wildcards ( *., ? and ).
grep > Very often, when using the command line, we may want to find a particular keyword. grep is a filter to search for keywords. It is often used when output is piped from one command to another. Linux allows us to take the output of one command and send it to another command. This is called piping and we use | to do this . So, for instance, if I wanted to see all the services running on my Linux system, I can use the
ps command followed by the
-aux switches such as:
> ps -aux
This command outputs all the services running in this system. What if we wanted to find just one single service among this long list? We can do this by piping the output from
grep and look for a keyword. For instance, if I wanted to find out whether the apache2 service was running, we could type:
> ps -aux | grep apache2
###/ Managing & Manipulating files
####1. Creating Files:
There are numerous ways to create files in Linux, but we will examine two here. The first is
cat. cat has nothing to do with your favorite domesticated feline, but rather it is short for con
catenate or placing pieces together. It is usually used for displaying the contents of a file, but also can be used to create a file, i.e:
cat <some_file> - Will output the contents of
<some_file> to the terminal
We can also use the
cat command to create a file by following the
cat command with a redirect (>) and a file name such as:
cat > hackingskills
When we hit enter, Linux will go into interactive mode and wait for us to start typing the contents that go into the file. To beginners that can be puzzling. Simply begin typing and whatever you type will go into the file “hackingskills”. When you’re done, hit Ctrl+D to exit and return to the terminal prompt.
To view the content of “hacking skills”, simply type
cat hackingskills , and you’re done!
If we wanted to add something to that file or append it, we can use the
cat command with a double redirect (>>). When we do this, whatever we type will be added to the file.
cat >> hackingskills
Linux goes into interactive mode, waiting for what we want to add to the file. After typing whatever you want, hit Ctrl+D and return to the prompt. Now if you display the content of the file again, you’ll see that whatever you appended will be there, without overwritting what was there already.
If we want to overwrite the file, we can simply use the
cat command with a single re-direct (>) such as:
cat > hackingskills
Once again, Linux goes into interactive mode and we type whatever we want. Now when we look for the contents of the file “hackingskills”, we can see that the file contents have been overwritten with the new content.
Linux has a command that might at first glance not seem like a file creation command called
touch. This command was originally developed to simply “touch” a file to change its creation or alteration date. By default, it creates the named file if it doesn’t already exist. As you might guess, it is that part of the command that makes it so useful for file creation.
####2. Creating a directory:
The command for creating a directory in Linux is
mkdir or a contraction of
directory. If I wanted to create a directory named “newdirectory”, I would simply type:
And then to move to this directory:
####3. Removing a file
Removing a file is rather simple in Linux. We have the
rm command for
moving a file.
To confirm the file has been deleted, you can ls the current directory.
####4. Copy a File
To copy files in Linux we use the
py makes a copy of the file in the new location and leaves the old one in place.
If we wanted to copy my
<some_file_1> to my /root/newdirectory directory (this leaves oldfile in place), I would simply type:
cp oldfile /root/newdirectory/newfile
When we then navigate to newdirectory, we can see that there is an exact copy of the
<some_file_1> called newfile.
####5. Rename a File
Unfortunately, Linux doesn’t really have a command for renaming a file like Windows and some other operating systems, but it does have the
The move command can be used to move a file or directory to a new location or simply to give an existing file a new name. If I wanted to rename
<some_file_2>, I can use the move (
mv) command to do so, such as:
mv <some_file_1> <some_file_2>
To check if you renamed the file successfully, simply
ls the current directory.
####6. Remove a directory
To remove a directory, the Linux command is similar to the remove command for files, but with appended “dir”. Such as
It’s important to note that
rmdir will NOT remove a directory if there is anything (files or sub-directory) in the directory. It will give you a warning message that “directory is not empty”.
You must first remove all the contents of the directory before removing the directory.
rm does have an option to automatically remove all files and directories within the named directory. Simply use the
-r (recursive) switch after
rm such as:
rm -r <some_directory>