top of page
Search
  • OTW

Linux Basics for the Aspiring Hacker, Part 3 (Creating, Removing and Renaming Files and Directories)

Updated: Dec 12, 2022


Welcome back, my aspiring hackers!

In part 1 of this series, I introduced you to the file system, the cd or change directory command and some other Linux basics. In Part 2 of this series, I showed you how to find stuff in Linux.

In this tutorial we will be looking at how to;

1. create a file

2. create a directory

3. remove (delete) a file

4. copy a file

5. rename (move) a file

6. remove (delete) a directory)

Enough introduction, let's jump right in!

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 concatenate or placing pieces together. It is usually used for displaying the contents of a file, but also can be used to create a file. Usually these files are relatively small as there are other better text editors such as vim, emacs, leafpad, gedit and others for creating more voluminous files.

cat

When we use the command cat followed by a filename, it will display the contents of the file. 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;

kali > 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". Here, I typed "Hacking is the most valuable skill set of the 21st century!". This is something everyone knows! When I am done, I hit Ctrl-D to exit and return to the Kali prompt.

Then when I want to see what is in the file "hackingskills", I simply type;

kali > cat hackingskills

Note that I did not use the redirect symbol and Linux spit back the contents of my file.

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.

kali > cat >> hackingskills

Linux goes into interactive mode, waiting for what I want to add to the file. I type "Everyone should learn hacking" and then hit Ctrl+D and return to the prompt.

Now, when I display the contents of that file with cat, you can see that the file has been appended with "Everyone should learn hacking!".

If we want to overwrite the file, we can simply use the cat command with a single re-direct (>) such as;

kali > cat > hackingskills

Once again, Linux goes into interactive mode and we type "Everyone in IT security without hacking skills is in the dark!". I then hit Ctrl+D and Linux brings me back to the prompt. Now when I look for the contents of the file "Hackingskills", I can see that the file contents have been overwritten with my new content.

touch

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.

Let's create or "touch" a newfile.

kali > touch newfile

When I then do I long listing (ls -l) of the directory, I can see that a new file has been created named "newfile".

2. Create a directory

The command for creating a directory in Linux is mkdir or a contraction of make directory. If I wanted to create a directory named "newdirectory", I would simply type;

kali > mkdir newdirectory

To then move to this newly created directory, we simply type;

kali > cd newdirectory

3. Removing a file

Removing a file is rather simple in Linux. We have the rm command for removing a file.

kali > rm newfile2

We can do a long list on the directory to confirm that the file has been deleted.

4. Copy a File

To copy files in Linux we use the cp command. Copy makes a copy of the file in the new location and leaves the old one in place.

If I wanted to copy my oldfile to a my /root/newdirectory directory (this leaves oldfile in place), I would simply type;

kali > cp oldfile /root/newdirectory/newfile

When we then navigate to newdirectory, we can see that there is an exact copy of the oldfile 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 move command.

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 newfile to newfile2, I can use the move (mv) command to do so, such as;

kali > mv newfile newfile2

When I do a long listing on that directory now, I see newfile2, but not newfile as it has been renamed. The same can be done with directories.

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 rmdir;

kali > rmdir newdirectory

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" as seen below.

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 after rm such as;

kali > rm -r newdirectory

Just a caution, though. Beginners should be wary and cautious of using the -r option with rm as it is very easy to remove valuable files and directories by mistake. Using the rm -r in your home directory, for instance, would delete every file and directory there. Probably not what you were intending.

Every hacker should be proficient in Linux, so keep coming back my aspiring hackers. We will cover networking in Linux in tutorial 4 of this series.

For more on using Linux for hacking, check out my book "Linux Basics for Hackers" now available here on Amazon.


7,047 views
bottom of page