Linux Essentials with Kali Linux | Day #2

Day #2 -> Getting familiar with File Management and Manipulation

On the previous post, we learnt a few things about directories like- how to list folders within a directory, create and access folders etc. Today, we’ll delve into further info.

First, to view the current directory, we need to type pwd. This means print working directory. This will show the current directory. Typing ls will show all contents within current directory.

Listing current directory

We can even view the details adding the command -l. The details will contain file type, permissions, owner info, size, date created etc. Adding command -a enables us to view all files including hidden ones. We can also combine multiple commands within one operation such as ls -al. However, adding -h will enable human readable format. For example, in this case the size of the files will be shown in regular units instead of plain integers. Now, suppose we want to view all subdirectories within current directory. Here, we’ll have to use -R (R for recursive). This’ll list all subfolders and files within current directory, much like tree command in Windows. Moreover, we can specify the search for a specific directory by passing the directory as an argument. For, example, if we want to look inside the Desktop directory, we’ll type ls -R ./Desktop or ls -R Desktop.

We’ve previously seen few operations with cd command (cd means change directory). We used it the enter a specific directory. Now, suppose we are within a directory but we need to go one folder back, what do we do? In this case we’ll type cd .. (double dot). By entering pwd command we can see that we are within the /home/merajmasuk directory. If we enter cd .. and list all the files we will be able to see only one folder. That’s because there is currently only one user on the system. Now we enter cd .. again. Thus, we finally come to the root directory of the Linux file system. If we list all the files, we will be able to see all the files within the root directory. If we wanted to go to the root directory directly, we could use cd /. Similarly, to go to the default directory, we have to use cd ~ (the tilde sign).

The root directory

If we are not sure what does a command do, we can use the whatis command. For example, whatis echo will display the function of the command echo. Now, the command echo displays a particular line of text. For example, typing echo “Hello, World!” will display the text Hello, Worlds! on our screen. We can also direct the line of text into a file. Before that, we create a file using touch, let’s say touch test.txt and then enter echo “Hello, World!” > test.txt; the text will be written to the file. Now, if we cat test.txt, the text will be printed on our screen. We can also redirect the content of a file to another file. For example, cat /etc/passwd > new.txt.

Once dealt with a file, we can remove it using rm command. To remove a directory, you need to specify if that is a recursive action. Before that, we’re gonna first create a directory; say, “New Folder”. Now we’re gonna copy the file ‘new.txt’ to this folder using command cp new.txt “New Folder”. Now we can remove the ‘new.txt’ file on the Desktop using rm new.txt. Now to remove the “New Folder” as a whole, we have to type rm -R “New Folder”. Alternatively, we can use rmdir (remove directory). Normally, it would delete a directory only if it’s empty. So, it won’t work in this case.

The command mv is used to move a file from one directory to another directory. Since there is no particular command for renaming, mv can also be used to rename files. For example, mv test.txt renamed.txt. Now, if we use cat to display the contents, we’ll find that they are the same. We can also change the extension this way.

On the next part, we will see how to alter file permissions.

link to part 1

link to part 3

Student of Computer Science & Engineering at Jahangirnagar University