files inside cabinet
Tue Nov 22

Navigation, Manipulation, And Redirection of Files Using The Terminal

In this modern day, the Linux community is growing bigger. Unlike before, the GUI (Graphical User Interface) offered by Linux distributions is getting better, which is not inferior to Windows or MacOS.

Maybe this is the main attraction of Linux besides the open source license that are offered in the many applications that commonly be used by this system. However, do you know that the real power of the Linux system lies inside its CLI (Command Line Interface).

Why use CLI if I have GUI?

For those who are just moving from a system like Windows, this is a very fair question to ask. On Linux systems, there are times when we need to reduce dependence on the GUI. Why is that? Let’s take an example on the server.

Today, servers that are generally used are Linux-based servers. The communication to the server is often established using certain protocols such as SSH which can be done directly using CLI. Thus, neither keyboard nor monitor screen is needed.

On the other side, the GUI consumes quite a lot of resources, while the server generally operates 24/7 so it’s better not to use the GUI.

But, I’m not using the server …

Having computer skills never hurts! It is a powerful skill set to have in this information age. By mastering the terminal you can do everything that your computer has to offer, from defending yourself to cyber threats until taking over someone else’s whole system.

Well, I’m not encouraging you to do that, but yes, it is all possible just by using a terminal.

But first, let’s go to the basics.

There are some commands that you need to know. To show your current working directory you can type

pwd

To enter a child directory you can use

cd <directory_name>

To go to the parent directory, type

cd ..

To list everything inside you current working directory (files and subdirectories), you can type

ls

Because of its function, the ls is one of the most commonly used commands. ls can be followed with some flags to show the output based on what you need. The flag that you might have used often such as -a (list all files and subdirectories including the hidden ones) and -l (list files in long listing format). Both flags can be used as a combination like this.

ls -la

By using the -l flag, the displayed output will be in a long list format. By using this command, some informations are added to the output.

Example

drwxr-xr-x   5 root root  360 Nov 22 05:43 dev
OutputExplanation
dtype
rwxr-xr-xfile permissions, see more details
here
5number of links
rootowner
rootgroup
360size
Nov 22 05:43modification date and time
devname of file or directory

Actually there are many more variations available . To see more detailed information about the ls and its flags, you can use a command like the following.

man ls

Another thing you can do is making a link between file.

ln -s <full_path_to_source_file> <full_path_to_destination_file>

By using -s flag, it will create a symbolic link. If not, it will make a hard link.

Files and Directories Manipulation

There are many commands involved in files and directories manipulations. Some of them are listed below.

CommandWhat it does
touch Create a file.
rm Remove file.
rm -rf <folder_name>Recursively and forcefully remove a file or folder. Can
also be used to remove a non-empty folder.
rmdir <folder_name>Remove an empty directory.
cp <target_directory>Copy file or folder to a particular path.
mv <target_directory>Move file to certain target directory.
mv <new_name>Rename a file or folder.

There are also some helpful commands to edit your file.

CommandWhat it does
nano Edit the content of file using nano editor.
vim Another file editor which is more advanced.
file Determine file’s type .

Important!

While using the rm command, you have to be very careful, especially if you are also using the wildcards (*). Linux will consider the user to be smart enough to do so. A small mistake could lead to disaster.

Take a look at these two examples below.

rm * .txt


rm *.txt

The first one will remove all files in the current directory and the second one will only remove files with txt extension. As a single space mistake will be interpreted very differently , it is always recommended to check the files first using ls command to avoid accidental removal.

Redirection

Redirection on terminal also known as “I/O Redirection”. Before going further into redirection, it is better to know about stdin, stdout, stderr.

What are stdin, stdout, and stderr streams in Linux?

In programming, a stream is basically the flow of the information or data. In this context, stdin (standard input) is where the data is coming from and most programs get the input from the keyboard.

Let’s say we use ls command. The terminal and ls command are programs. As the program (ls command) gets the input from the terminal (which is also a program), we can consider the terminal as the stdin of ls. The terminal also works as a stdout, because we can see the result (output) of ls command on the terminal.

What about stderr? Let’s say you unintentionally mistype a command, like sudo aot update (has to be sudo apt update). You may get sudo: aot: command not found. Instead of showing you the output, the terminal will throw an error, complaining that the command is not found. This error is stderr.

There is also a concept called file descriptor. In Unix and Unix-like systems (Linux), these streams are also identified by integer value.

Integer ValueStream
0Standard Input
1Standard Output
2Standard Error
File Descriptor

What are these values used for? In the terminal, these values can be used for redirection. See the example command below.

find /sys -type f

You can type another command.

echo $?

Try this command by yourself and see what happened. If you are not using a root account, it will throw some result and you may see value 1 after entering the second command. This command actually tells you whether a command is successful or not by showing you 0 if the command was executed successfully and showing anything else if it was not. Since we got 1, we had caught some error.

If you scroll up your terminal, you might identify these errors that might looks like this.

find: ‘/sys/kernel/tracing’: Permission denied
find: ‘/sys/kernel/debug’: Permission denied
find: ‘/sys/fs/pstore’: Permission denied
find: ‘/sys/fs/bpf’: Permission denied

If you are doing some scripting, you might want to handle this error. Say that we want to catch this error and redirect it to a file called log.txt. You can use a command like this.

find /sys -type f 2> log.txt

What it does is making a file called log.txt in your current directory and using this file to save the stderr to that file. The number 2 on the above command represent the stderr which is telling the terminal to redirect it to a file called log.txt (see the file descriptor above). In other case, you might want to do redirection for both stdout and stderr to two different files. Therefore, you can use something like this.

find /sys -type f > result.txt 2> log.txt

By default, the redirection is used for stdout. Therefore, the above command is similar to this.

find /sys -type f 1> result.txt 2> log.txt

What if you want to see the stderr only and not the stdout. Also you don’t want to print the stdout into a file. You can simply do this by using this command.

find /sys -type f > /dev/null

By doing this, we are basically dumping the stdout into the void. This is a very useful command, as it can filter the result we want. If you want to add a new line instead of overwriting the existing file, you can replace > with > >.

Conclusion

The aforementioned commands may be an oversimplification of what you can do with a terminal. In fact, there are tons of them. Describing each one in a very detailed manner might take an entire article, which is likely to bore you to death. I think it is better to make things simple for you so you won’t be overwhelmed by it. In the end, I hope this article will provide you with a good understanding of how to use a terminal on a regular basis.