Not only navigating through the directories but we should also know how to create , move , copy and rename the files
Here are the commands for that :
Commands
TOUCHWhole thing on HTB
The main difference between working with files in Linux and Windows
is the way we can access the files. For example, we usually have to open
Explorer to find and edit files in Windows. In Linux, on the other
hand, we have a terminal where we can access and edit files using
commands. Moreover, we can even edit the files interactively without
using an editor, such as
vim
or nano
.
The terminal in Linux is a more efficient and faster tool because you
can access the files directly with a few commands and edit and modify
them selectively with regular expressions (
regex
). You can
also run several commands simultaneously and redirect the output to a
file. This saves time and is very handy when we want to edit many files
at once.
Next, let us work with files and directories and learn how to create,
rename, move, copy, and delete. First, let us create an empty file and a
directory. We can use
touch
to create an empty file and mkdir
to create a directory.
The syntax for this is the following:
satvik@htb[/htb]$ touch <name>
satvik@htb[/htb]$ mkdir <name>
In this example, we name the file info.txt
and the directory Storage
. To create these, we follow the commands and their syntax shown above.
satvik@htb[/htb]$ touch info.txt
satvik@htb[/htb]$ mkdir Storage
We may want to have specific directories in the directory, and it
would be very time-consuming to create this command for every single
directory. The command
mkdir
has an option marked -p
to add parent directories.
satvik@htb[/htb]$ mkdir -p Storage/local/user/documents
We can look at the whole structure after creating the parent directories with the tool tree
.
satvik@htb[/htb]$ tree ..
├── info.txt
└── Storage
└── local
└── user
└── documents
4 directories, 1 file
We can also create files directly in the directories by specifying
the path where the file should be stored. The trick is to use the single
dot (
.
) to tell the system that we want to start from the
current directory. So the command for creating another empty file looks
like this:
satvik@htb[/htb]$ touch ./Storage/local/user/userinfo.txt
satvik@htb[/htb]$ tree ..
├── info.txt
└── Storage
└── local
└── user
├── documents
└── userinfo.txt
4 directories, 2 files
With the command mv
, we can move and also rename files and directories. The syntax for this looks like this:
satvik@htb[/htb]$ mv <file/directory> <renamed file/directory>
First, let us rename the file info.txt
to information.txt
and then move it to the directory Storage
.
satvik@htb[/htb]$ mv info.txt information.txt
Now let us create a file named readme.txt
in the current directory and then copy the files information.txt
and readme.txt
into the Storage/
directory.
satvik@htb[/htb]$ touch readme.txt
satvik@htb[/htb]$ mv information.txt readme.txt Storage/
satvik@htb[/htb]$ tree ..
└── Storage
├── information.txt
├── local
│ └── user
│ ├── documents
│ └── userinfo.txt
└── readme.txt
4 directories, 3 files
Let us assume we want to have the readme.txt
in the local/
directory. Then we can copy them there with the paths specified.
satvik@htb[/htb]$ cp Storage/readme.txt Storage/local/
Now we can check if the file is thereby using the tool tree
again.
satvik@htb[/htb]$ tree ..
└── Storage
├── information.txt
├── local
│ ├── readme.txt
│ └── user
│ ├── documents
│ └── userinfo.txt
└── readme.txt
4 directories, 4 files
There are also many other ways to work with files using redirects or
text editors, which we will see and discuss later in other sections.
Use the tools we already know to find out how to delete files and directories