Linux basics
Tags: linux, codenewbie, learning, beginners
Published: 2023-11-30 18:10:45

Whether you intend to work as a backend developer, automation tester or pentester, there will come a time when you need to know basic Unix commands. In this article, we'll look at the absolute basics needed to use Linux.

## Operators

To begin with, a little about operators. These are special characters that we can use in calling consecutive commands.

- **;** - separate consecutive commands in succession, e.g. `cat file.txt; ls; cd myFolder`.
- **&** - launches commands separated by the & sign immediately, regardless of the previous one
- **&&** - means that if the command before the && operator was successful, the next one is run e.g. `tar -cf myArchive.tar.gz myFileToCompress01.jpg && cat file.txt` results in the creation of the archive myArchive.tar.gz, and if successful, the contents of file.txt will be displayed,
- **||** - the opposite of the above operator i.e. if the previous command fails, the next one will get called,
- **|** - redirects the standard output of the command to the standard output of the next command,
- **>** - redirects the standard output (i.e. what will be displayed in the console after running any of the commands) to the indicated file,
- **_<_** - analogous to the previous operator, instead of redirecting the contents to a file, redirects its contents to the standard output,
- **>|** - as in the case of ">" redirects the standard output to a file, except that if it exists and the shell has been configured not to overwrite files, this will be ignored
- **>>** - as with ">" redirects the standard output to a file, except that if it exists, it will add the contents to it,

## Operations on files and folders

For basic operations, min: like moving between folders or reading the contents of files, we will use the following commands.

- **ls** - we check the contents of a folder. By default, without specifying the path to it, it will show us the contents of the current location. We can define a mask (e.g.: `ls *.txt`) to display files with a given extension. In addition, to display hidden files and those starting with a dot, we will add the parameter "`-a`" to the ls command.
- **cat** - a command that can be used in several ways:
  - to display the contents of a file (` e.g.  `cat myTextFile.txt_)_`,
  - to create a file with a given extension (`cat > myFile.pdf`). "Peck" means to write a given value into the myFile.pdf file. In the above example, it is not present, and therefore an empty file will be created, which we can edit immediately in the console,
  - to merge a file or several files into a new one (`cat fileOne.txt fileTwo.txt > newFile.txt`).
- **cd** - command used to move between directories. To move to the folder "myFolder2" we will use the command "`cd myFolder2`". To move to the previous directory (parent), use the command "`cd ../`". In turn, to go directly to the home directory, we will use the command "`cd`" without arguments and parameters.
- **file** - displays us information about the contents of the file. For example, checking a pdf file (`file myFile.pdf`) we will get a message displayed in the console that reads: `myFile.pdf: PDF document, version 1.`4
- **mkdir** - creates a directory or a collection of them, for example: `mkdir myFirstFolder mySecondFolder`.
- **rmdir** - analogous to the example above, deletes the directory. The important thing is that it will not delete it if it is not empty. So how to delete it?
- **rm** - by default is used to delete files. However, if we decide to delete a folder along with its contents, we will add the parameter `-r` (to denote deletion in a recursive manner) and `-f` (to force the operation - this is not always necessary, but it is worth keeping in mind). This will produce the `rm -rf mFolder` command.
- **cp** - a command used to copy files and directories. Analogous to the command above, when copying a folder with its contents, we will need to add the `-r` parameter. An example command copying the MyFolder01 folder with its contents as MyFolder02 to the location we are currently at would look like: `cp MyFolder01 myFolder02 -r`,
- **pwd** - displays the path to the directory where you are currently located.
- **df** - displays the file systems and free space on them. To make them more readable, add the `-h` parameter.
- **du** - displays the size of the file or files in the directory. In the case of directories, if you do not want to have the size of each successive file listed but their sum, add the "`-s`" parameter. By default, the size is given in kilobytes.
- touch - a command in theory intended to change the date of the last file read/write. In practice, it is used to create empty files.
- tar - a command used to perform operations on tar.gz files. To create an archive named myArchive.tar.gz, the command tar -cf myArchive.tar.gz myFileToCompress01.jpg myFileToCompress02.jpg will be used. The tar -xf myArchive.tar.gz command will be used to unpack this archive.
- gzip - analogous to the above command except that it supports the gzip extension. To pack a folder into the myArchive archive, we will use the command: gzip -r myFolder. In turn, to unzip it: gzip -dr myArchive.zip.
- chmod - is used to grant permissions to specific users to files and directories. It is possible to give permissions in a letter and numeric way.
- chown - changes the owner of a file or directory.
- grep - is used to search for the specified phrase in files. For example, to search for the word "Test" in each file in the directory "testFolder", we would use the command: grep -R "Test" testFolder/.

## Handling and reading processes

- **top** - is something like a task manager that displays information about CPU and memory usage.
- **ps** - shows active processes on a Linux system. To display information about all processes, their owners and processes not connected to the terminal, type: `ps aux`.
- **kill** - command used to send signals to the indicated processes. An example signal could look as follows: `kill -9 32`, where:
  - `9` means the number of the signal (their list can be checked with the `kill -l` command). This is equivalent to the command `kill -s SIGKILL 32`,
  - The `32` is the number of the process to which we send the signal
- **watch** - command used to call the indicated command every given number of seconds. By default, the time between commands is two seconds. For example, "`watch -d ls`" means displaying files and directories from the current folder every two seconds. In addition, adding the "`-d`" parameter will make the terminal highlight the differences from the previous call.

## Miscellaneous

- **who** - displays logged in users,
- **whoami** - displays the name of the currently logged in user,
- **clear** - clears the terminal. Useful when it gets messy:)
- **man** - displays the system manual for the specified command. For example: "`man ls`".

## Sources

<https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators>\
[https://linuxwiki.pl/wiki/](http://linuxwiki.pl/wiki/)