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.
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.
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.
We can look at the whole structure after creating the parent directories with the tool tree.
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:
With the command mv, we can move and also rename files and directories. The syntax for this looks like this:
kalijester68@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.
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.
Let us assume we want to have the readme.txt in the local/ directory. Then we can copy them there with the paths specified.
Now we can check if the file is thereby using the tool tree again.
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.