Hack Like a Pro: Linux Basics for the Aspiring Hacker, Part 3 (Managing Directories & Files)

Linux Basics for the Aspiring Hacker, Part 3 (Managing Directories & Files)

Welcome back, my tenderfoot hackers!

This is the third installment of my series on basic Linux skills that every hacker should know. Although some hacking tools are available for Windows and Mac, every real hacker uses Linux—for good reason.

Make sure to check out Part 1 and Part 2 of this series before continuing.

In this installment, we'll look at how to manage files and directories in Linux, namely copying, renaming, moving, and viewing. Then we'll look a bit at networking and the ifconfig command.

Step 1: Copying Files (Cp)

In my previous installment in this series, we created a file called newfile in the /pentest/wireless/aircrack-ng directory.

Let's imagine that we need a copy of the file in our home directory, user root. We can do that by:

  • bt > cp newfile /root

We simply tell Linux copy (cp) the newfile (in our current directory) to the directory of the root user (once again, don't confuse this with the / directory). We don't need to specify the directory that newfile is in, if it's in our current working directory. The copy command makes a copy of the file specified and places it in the specified directory leaving the original untouched and unchanged, so we now have two copies of the original file.

You can see in the screenshot above that when we change directory (cd) to the root user and list the files (ls) that now a newfile copy appears in that directory.

What if we wanted to copy a file from a directory that wasn't in our current working directory? In that case, we would need to specify a path to the directory, such as:

  • bt > cp /etc/newfile /root

Also, note that we don't need to specify the file name we're copying it to. It simply makes a copy and gives it the same name as the original "newfile."

Step 2: Moving Files (Mv)

Unfortunately, Linux doesn't have a rename command for renaming files, so most users use the move (mv) command to both move files and rename them. Let's imagine now that we placed that newfile in the wrong directory and we really wanted it in the root (/) directory. We can use the move command to do so.

  • bt > mv /root/newfile /

This command says, move the newfile from the root user directory to the root (/) directory. The move command literally moves the file and does not leave a copy where the old one existed. Note that the newfile has moved to the root directory.

Sometimes, we want change the name of the file and not actually move it to a different location. The move command can be used for that also. We simply tell Linux to move the original file to a new file with a new name. Take for instance our newfile in the aircrack-ng directory. Let's say that we want to rename that file to "crackedpasswords. We can simply type:

  • bt > mv newfile crackedpasswords

Notice here that I did not use any directory paths because I was moving a file in my current working directory and to a file in my current working directory. If we run a directory listing now, we can see that newfile is gone and crackedpasswords now exists in the aircrack-ng directory.

Step 3: Viewing Files (Cat, More, Less)

From the command line in the terminal, we can view the contents of files by using the cat command. cat is short for concatenate, which is a $20 word for putting together a bunch of pieces (we are putting together the words for display on the screen). Concatenate is a fancy word, but is used throughout computer science and information technology, so add it to your vocabulary.

Staying in the /pentest/wireless/aircrack-ng directory, let's cat some files. First, let's get a listing of files in this directory.

Notice in the screenshot above, there is a file called README. Often, software developers use this file to provide important notes to their users. This file can be critical, especially with hacking tools because most are open source and seldom have manuals. Let's take a look at the contents of this file.

  • bt > cat README

When you run this command, you'll see lots of text running across your screen. Obviously, it goes by way too fast to read, but when its done, we could use the scroll button on the terminal to scroll up to read all the text. There is another way, though, that might be easier.

There are two commands that work similar to cat but don't simply run the text across the screen until it hits the end of file. These are more and less. They are very similar, each only displaying one page of information on your screen until you prompt it to scroll down. Let's try more first.

  • bt > more README

As you can see, when I use more and the filename, it displays the file until the screen fills and waits for further instructions from me. If I hit enter, it will scroll down one line at a time, while if I hit the spacebar, it will scroll one page at a time.

Now let's try the more powerful less (in some Linux circles, there is a saying "less is more", meaning that less is more powerful than more).

  • bt > less README

You can see that less followed by the filename, once again displays the README file until it fills up my terminal just like more. Though, note that less displays the name of the file that I'm viewing in the lower left-hand corner. Probably more importantly, less has powerful text searching capabilities that are missing from more. I can search for text within this file by typing the forward slash followed by what I'm searching for and less will find it and highlight it for me.

That's one of the primary reasons I prefer less.

Step 4: Networking (Ifconfig)

Before I finish this tutorial, I want to show you one last simple networking command, ifconfig. Those of you comfortable with Windows networking, know that you can use the ipconfig command in Windows to display key information on your networking configuration. ifconfig in Linux is very similar, with only one letter different. Let's run ifconfig see what it tells us.

  • bt >ifconfig

As you can see, it displays much of the key info I need to know about the network configuration of my system including IP address, netmask, broadcast address, interfaces, MAC address of my interface, etc. We'll spend some more time with networking in future Linux tutorials.

If you haven't already, make sure to check out Part 1 and Part 2 of this series, and if you have any questions, ask away in the comments below or hit up the Null Byte forum for more help.

Penguin photo by Matt Biddulph, Linux photo by Black_Claw

41 Comments

Amazing and nice words you collect to brief here...

Thank you!

Wow this is by far the best tutorial from scratch I have ever seen.!!!

How can I contact you my friend?

Muta:

Thanks for that compliment. I'm glad you enjoyed it.

You can always contact me here at wonderhowto.com.

OTW

Did you make pdf booklet of these tuts, i want one.
Thank You for such great help.

OTW:
what does star at the end of crackedpasswords mean?is it a part of syntax or is it about some information?

Giri:

That is a stray mark that appeared when this article was published. Thanks for pointing this out. I'll remove it.

OTW

Sir OTW ,

Whenever i try to open a file with less , i always have to open another terminal for the next command (unlike in cat where the cursor automatically moves to next command ).Is there a way i can type my next command in same terminal after viewing the file through less and not get stuck there.

BTW your tutorials are very organised and easy to understand .
Thank You

Pranav:

Simple type "q" to exit less to a command prompt.

OTW

sir
how can we copy (or move ) files from (or to) a pendrive or any partitioned drive ? can we use cd to change our drive ?
thank you

Pranav:

You need to mount the pendrive and then simply copy to that directory.

cd is not used to change drives in Linux. There are no drives in Linux, just directories.

OTW

Sir

I'm not able to mount the drive. After reading your part-13 of this series i tried to do "mount -t /dev/sdb /media" then the help appears .

and when i type "mount" my device "/dev/sdb" isn't listed .

please help ! bcoz after all this i'm not able to open my pendrive even on the GUI on my KALI virtual machine.It says Unable to mount error

Also can you tell me how to copy a directory.
Thank You

Pranav:

Are you running Kali in a VM?

OTW

Yes sir , I don't understand , Is there a problem ?

Well, not a problem, but you must attach removable devices in the VM.

i had done it sir , i had enabled the pendrive in the removable devices menu .i.e why i could see it on kali . but as you insisted i did it again just now and same thing happened

Thank You

Pranav;

I noticed in your screenshot that the error says your pendrive is "read only". This is usually an indication that its filesystem is corrupted. Do a filesystem check and repair.

OTW

Sir

i did it it using windows 8 & it says that no errors found.And i also tried it with another pendrive , unfortunately , same problem . But when i did it with 2nd pendrive , and did "fdisk -l" , 2nd one was also assigned the name "/dev/sdb"(same as previous one) is this alright or they should have different names.

Thank You

In linux, the second drive is always sdb.

Sir

i'm sorry i couldn't reply . The problem was with my virtual machine . i mounted kali linux iso on my usb & tried the command on live mode and it worked , Thank you very much for the time you gave to this small problem(anyone else would've just asked me to google it). Thank you very much.

Just clear me two things

  1. how does mounting actually Works . does the data actually gets copied to the directory (that is unlikely as then mounting would take a lot of time ) , but then how are we able to use that data without actually copying it .
  2. How can i copy a whole directory (& not only a file)(cp didn't worked when i tried to copy a directory)

Thank you very much
Pranav

Pranav:

Its very hard to answer your questions when you give me incomplete information.

OTW

Sir

i'm sorry for the inconvinience ,

I meant to say that whenever a pendrive is mounted then we are able to see the data in the directory we mounted it in , i assumed that this data isn't copied to that directory , as it would take up much time to copy the data and much disk space . i wanted to ask that if it's not copied then how are we able to see all that data in the specified directory when it is actually not there . in other words how does mounting actually works.

my 2nd problem was that whenever i copy some data i rarely copy only one file i copy directories(folders) but when i tried to copy a directory by cp command it didn't work. so can you tell me a command to copy a directory .

I think i'm much more clear than before , & i also think i have annoyed you with constant asking so i'm sorry for that.

Thank you
Pranav

Sorry but, I haven't done even the first step. I'm using ubuntu when I try copy my "newfile" to another directory Terminal says : Permission Denied.

Then I try copy with sudo command, tough seems ok but there's nothing changed..

Can somebody help me ?

Image via tinypic.com

Said:

The problem is that you have both a file and a directory named "new". You copied the "new" file to the / directory, but you are looking for it in the /new directory.

Thank you.. I did understand what I was doing wrong.

im trying to copy an EXISTING file to an EXISTING directory but this happens.

jen@jen-desktop:~/Desktop$ cp newfile /newdirectory
cp: cannot create regular file '/newdirectory': Permission denied

i had the same problem, all i did was specify the entire path from root.
not just /newdirectory

hopefully i can explain this well so you understand me.

when i use the less command and view a file i search for a word using forward slash and everything is working except i get stuck at the end of the file. it just says end.

i am unable to continue typing more commands in the terminal. is there a way to get back to typing new commands without opening a new terminal?

also i am loving your linux tutorials, thank you

You can always press the "control" and "Z" keys at the same time.

Hey does anyone know how to get permissions to get into the root directory?

could we change the directory name?

My first thought was .. I know changing directories.. copying, moving files. i can skip this and move to next one.

But i still stopped, read it as i thought : may be i ll find anything new.

And i learned difference between more and less.
Less is more,indeed.

Thankyou.
Moving on to next chapter.

Bydway, i just found you.

  • Your New Apprentice :)

--Apprentice : May be i can use that as my Pen name.. Hmmm.. ;)

i got a problem...i use Kali linux 2.0 and there's no/pentest/wireless/aircrack-ng directory...

im having the same issue :/

use command - locate aircrack-ng you will find all the directories related to aircrack-ng. Directory structure is changed in KALI 2.0. Now you will find these directories in /usr/share or /usr/bin.

Hello there. I am actually trying these things on my OS X and when I have tried to cp file I was not able to, because : Permission denied

And yeah I am running terminal as root

cant find the pen test directory
I'm not sure even that there is a pen test directory

Share Your Thoughts

  • Hot
  • Latest