April 12, 2022
In our previous short article we learned about the File system of Linux. In this article we are going to learn about how we can deal with files directly from our terminal window.
But did we must have to learn this? Because Kali Linux and other most popular Linux distributions comes with good graphical user interface (GUI), so why we need to learn how to something with a file from command line where we can do it just like we do on Windows system?
Well in our opinion when we are reading this on this website then we have interest on cybersecurity field. When we are dealing with an remote system (read compromising a system) we have to do it on terminal. We need to break the privileges and get into it. There are lots of things with file we need to do here (from modifying system file data to uploading shells). So we need to have at-least a basic idea to dealing with files from terminal.
Creating a File using Terminal
First we will learn how we can create a file on Linux terminal. We will going to use touch command. We just need to run touch filename command to create a file. As shown in the following screenshot:
Copy Files using Terminal
We can copy files/directory from a directory to another one by using terminal window. To do that we need to use cp command. Suppose we have a file on our /home/kali/desktop directory. We need to copy this file to /home/kali/new_folder directory. We use following command for that:
As we can see in the following screenshot:
In the following screenshot we can see that our file is copied to our destination directory. We did this for a file only we can do the same for a folder/directory.
Moving Files using Terminal
We can move a file from a directory to another directory to other directory by using mv command. This is very similar like cp command. mv full/path/of/file destination/path as we can see in the following screenshot:
|Our file is moved from source directory|
Renaming Files using Terminal
Basically we move a file/directory on the same directory and change the name. That is what renaming do. The mv command to change the name of a file without moving it to another directory.
|We can see that we renamed the file|
Deleting Files using Terminal
We also can delete a file directly from terminal by simply using rm command. We just need to rm filename command to delete any file. To delete a file forcefully we need to use -f flag -r flag used to remove contents recursively.
|Deleting files using Terminal|
Editing Files using Terminal
Let we have take look at file editing in terminal. As we told that this is too much important to have Linux skill, especially during the pen-testing if we need access to a Linux or UNIX based OS or server.
There are some cool text editors like gedit, leafpad and mousepad, they may looks far better than command line text editors for their graphical user interface, but we will focus on terminal based text editors. Everyone might have their own favorite text editors, but here we are going to cover two most common options, Nano and Vi.
Nano is the most user friendly and simplest text editors. To open a file and start editing we simply run nano <file name>.
After the file opened we can start editing the text as we can do on any graphical user interface using keyboard. As we can see in the following screenshot:
|Editing text using nano text editor on terminal|
If we see in the bottom of following screenshot, we can see the command menu there. We need to memorize some widely used keyboard shortcuts like:
- CTRL+O – Write changes to the file.
- CTRL+K – Cut to Current Line.
- CTRL+U – Uncut a line, and paste it at the cursor location.
- CTRL+W – Search
- CTRL+X – Exit
To know more about nano, we need to see it’s
Frankly speaking, vi is very absurd to use, many users avoid it. However as a penetration tester’s point of view learning vi is very good so, have some time to exploring it. vi is installed on every POSIX-complaint system. vi lovers considered as ultra-pro Linux user in the community.
vi is extremely powerful, Want to explore vi ? The following sources have very good manual to learn vi.