Navigation
Navigation is essential, like working with the mouse as a standard Windows user. With it, we move across the system and work in directories and with files, we need and want. Therefore, we use different commands and tools to print out information about a directory or a file and can use advanced options to optimize the output to our needs.
One of the best ways to learn something new is to experiment with it. Here we cover the sections on navigating through Linux, creating, moving, editing, and deleting files and folders, finding them on the operating system, different types of redirects, and what file descriptors are. We will also find shortcuts to make our work with the shell much easier and more comfortable. We recommend experimenting on our locally hosted VM. Ensure we have created a snapshot for our VM in case our system gets unexpectedly damaged.
Let us start with the navigation. Before we move through the system, we have to find out in which directory we are. We can find out where we are with the command pwd.
Navigation
cry0l1t3@htb[~]$ pwd/home/cry0l1t3
Only the ls command is needed to list all the contents inside a directory. It has many additional options that can complement the display of the content in the current folder.
Navigation
cry0l1t3@htb[~]$ lsDesktop Documents Downloads Music Pictures Public Templates Videos
Using it without any additional options will display the directories and files only. However, we can also add the -l option to display more information on those directories and files.
Navigation
cry0l1t3@htb[~]$ ls -ltotal 32
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:37 Desktop
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Documents
drwxr-xr-x 3 cry0l1t3 htbacademy 4096 Nov 15 03:26 Downloads
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Music
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Pictures
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Public
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Templates
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Videos
First, we see the total amount of blocks (1024-byte) used by the files and directories listed in the current directory, which indicates the total size used. That means it used 32 blocks * 1024 bytes/block = 32,768 bytes (or 32 KB) of disk space. Next, we see a few columns that are structured as follows:
However, we will not see everything that is in this folder. A directory can also have hidden files that start with a dot at the beginning of its name (e.g., .bashrc or .bash_history). Therefore, we need to use the command ls -la to list all files of a directory:
Navigation
cry0l1t3@htb[~]$ ls -latotal 403188
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:37 .bash_history
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:37 .bashrc
...SNIP...
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:37 Desktop
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Documents
drwxr-xr-x 3 cry0l1t3 htbacademy 4096 Nov 15 03:26 Downloads
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Music
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Pictures
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Public
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Templates
drwxr-xr-x 2 cry0l1t3 htbacademy 4096 Nov 13 17:34 Videos
To list the contents of a directory, we do not necessarily need to navigate there first. We can also use “ls” to specify the path where we want to know the contents.
Navigation
cry0l1t3@htb[~]$ ls -l /var/total 52
drwxr-xr-x 2 root root 4096 Mai 15 18:54 backups
drwxr-xr-x 18 root root 4096 Nov 15 16:55 cache
drwxrwsrwt 2 root whoopsie 4096 Jul 25 2018 crash
drwxr-xr-x 66 root root 4096 Mai 15 03:08 lib
drwxrwsr-x 2 root staff 4096 Nov 24 2018 local
We can do the same thing to navigate to the directory. To move through the directories, we use the command cd. Let us change to the /dev/shm directory. Of course, we can go to the /dev directory first and then /shm. Nevertheless, we can also enter the full path and jump there.
Navigation
cry0l1t3@htb[~]$ cd /dev/shmcry0l1t3@htb[/dev/shm]$
Since we were in the home directory before, we can quickly jump back to the directory we were last in.
Navigation
cry0l1t3@htb[/dev/shm]$ cd -cry0l1t3@htb[~]$
The shell also offers us the auto-complete function, which makes navigation easier. If we now type cd /dev/s and press [TAB] twice, we will get all entries starting with the letter “s” in the directory of /dev/.
Navigation
cry0l1t3@htb[~]$ cd /dev/s [TAB 2x]shm/ snd/
If we add the letter “h” to the letter “s,” the shell will complete the input since otherwise there will be no folders in this directory beginning with the letters “sh”. If we now display all contents of the directory, we will only see the following contents.
Navigation
cry0l1t3@htb[/dev/shm]$ ls -la /dev/shmtotal 0
drwxrwxrwt 2 root root 40 Mai 15 18:31 .
drwxr-xr-x 17 root root 4000 Mai 14 20:45 ..
The first entry with a single dot (.) indicates the current directory we are currently in. The second entry with two dots (..) represents the parent directory /dev. This means we can jump to the parent directory with the following command.
Navigation
cry0l1t3@htb[/dev/shm]$ cd ..cry0l1t3@htb[/dev]$
Since our shell is filled with some records, we can clean the shell with the command clear. First, however, let us return to the directory /dev/shm before and then execute the clear command to clean up our terminal.
Navigation
cry0l1t3@htb[/dev]$ cd shm && clearAnother way to clean up our terminal is to use the shortcut [Ctrl] + [L]. We can also use the arrow keys (↑ or ↓) to scroll through the command history, which will show us the commands that we have used before. But we also can search through the command history using the shortcut [Ctrl] + [R] and type some of the text that we are looking for.
Working with Files and Directories
The primary difference between working with files in Linux, as opposed to Windows, lies in how we access and manage those files. In Windows, we typically use graphical tools like Explorer to find, open, and edit files. However, in Linux, the terminal offers a powerful alternative where files can be accessed and edited directly using commands. This method is not only faster, but also more efficient, as it allows you to edit files interactively without even needing editors like vim or nano.
The terminal's efficiency stems from its ability to access files with just a few commands, and it allows you to modify files selectively using regular expressions (regex). Additionally, you can run multiple commands at once, redirecting output to files and automating batch editing tasks, which is a major time-saver when working with numerous files simultaneously. This command-line approach streamlines workflow, making it an invaluable tool for tasks that would be more time-consuming through a graphical interface.
Next, we will explore working with files and directories to effectively manage the content on our operating system.
Create, Move, and Copy
Let us begin by learning how to perform key operations like creating, renaming, moving, copying, and deleting files. Before we execute the following commands, we first need to SSH into the target (using the connection instructions at the bottom of the section). Now, let's say we want to create a new file or directory. The syntax for this is the following:
Syntax - touch
Working with Files and Directories
sasorirose@htb[/htb]$ touch
Syntax - mkdir
Working with Files and Directories
sasorirose@htb[/htb]$ mkdir
In the next example, we will create a file called info.txt and a directory called Storage. To create these, we follow the commands and their syntax as shown above.
Create an Empty File
Working with Files and Directories
sasorirose@htb[/htb]$ touch info.txtCreate a Directory
Working with Files and Directories
sasorirose@htb[/htb]$ mkdir StorageWhen organizing your system, you may need to create multiple directories within other directories. Manually running the mkdir command for each one would be time-consuming. Fortunately, the mkdir command has the -p (parents) option, which allows you to create parent directories automatically.
Working with Files and Directories
sasorirose@htb[/htb]$ mkdir -p Storage/local/user/documentsWe can look at the whole structure after creating the parent directories with the tool tree.
Working with Files and Directories
sasorirose@htb[/htb]$ tree ..
├── info.txt
└── Storage
└── local
└── user
└── documents
4 directories, 1 file
You can create files directly within specific directories by specifying the path where the file should be stored, and you can use the single dot (.) to indicate that you want to start from the current directory. This is a convenient way to work within your current location, without needing to type the full path. Therefore, the command for creating another empty file looks like this:
Create userinfo.txt
Working with Files and Directories
sasorirose@htb[/htb]$ touch ./Storage/local/user/userinfo.txtWorking with Files and Directories
sasorirose@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:
Syntax - mv
Working with Files and Directories
sasorirose@htb[/htb]$ mv
First, let us rename the file info.txt to information.txt and then move it to the directory Storage.
Rename File
Working with Files and Directories
sasorirose@htb[/htb]$ mv info.txt information.txtNow 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.
Create readme.txt
Working with Files and Directories
sasorirose@htb[/htb]$ touch readme.txtMove Files to Specific Directory
Working with Files and Directories
sasorirose@htb[/htb]$ mv information.txt readme.txt Storage/Working with Files and Directories
sasorirose@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.
Copy readme.txt
Working with Files and Directories
sasorirose@htb[/htb]$ cp Storage/readme.txt Storage/local/Now we can check if the file is thereby using the tool tree again.
Working with Files and Directories
sasorirose@htb[/htb]$ tree ..
└── Storage
├── information.txt
├── local
│ ├── readme.txt
│ └── user
│ ├── documents
│ └── userinfo.txt
└── readme.txt
4 directories, 4 files
In addition to basic file management commands, there are many other powerful ways to work with files in Linux, such as using redirection and text editors. Redirection allows you to manipulate the flow of input and output between commands and files, making tasks like creating or modifying files faster and more efficient. You can also use popular text editors like vim and nano for more interactive editing.
We will explore and discuss these methods in greater detail in later sections. As you become familiar with these techniques, you will gain more flexibility in how you create, edit, and manage files on your system.