.. _ch:linux: Quick Linux Review =================== Remote Machine Access --------------------- We use `DRACO cluster <https://wiki.uni-jena.de/display/URZ010SD/HPC-Cluster+Draco>`_ at Friedrich Schiller University Jena for this course. Reachable via SSH only accessible from university network or `VPN <https://www.uni-jena.de/vpn-windows-apple-mobile>`_ . To achieve this goal, you'll need to make sure you have access to the DRACO cluster. If you're unsure whether you have access or not: .. admonition:: Task #. Enter your data in the list provided in the labs or thursday 10/17 in Felix or Max office. #. Wait for a reply that you are good to go, then test your access and do step 3. #. Use your URZ username to log into DRACO: .. code-block:: shell ssh -X <username>@login1.draco.uni-jena.de Raspberry Pis ------------- In class we will use a set of Raspberry Pis which act as our Desktops. To access them you need a KSZ account which you might still have to apply for. For further information, please visit KSZ's `homepage <https://www.ksz.uni-jena.de/formulare>`__. The KSZ accounts allows you to log into the Raspberry Pis in the lab room: #. You should see an Ubuntu login screen. **Don't enter your credentials here.** First, press **Ctrl + Alt + F2**. #. Now a shell interface opens. Here, you can provide your username, press enter, put in your KSZ password and press enter again. #. A few lines of text will pop up. You can ignore them. **Press enter one more time.** #. Next, type the command ``startx`` and press enter. Now you are all set up. Have fun! 😀 After finishing your work, you need to log out of the device: #. In the bottom right corner of the screen, press the **red power button**. #. A pop-up will open. Press **Logout**. #. Now you are back in the shell. Just type ``exit``, press enter and you're done! Getting Started with Linux --------------------------- Be aware that Linux system is case sensitive. **pwd (Print Working Directory)** Shows the current directory you are in. .. code-block:: bash pwd **ls (List)** - ``ls``: Lists the contents of the current directory. - ``ls path/to/directory``: Lists the contents of a specific directory. - ``ls -l``: Lists in long format, providing detailed information about files and directories, including permissions, owner, group, size, and modification time. - ``ls -a``: Lists all files, including hidden files (those starting with a dot, like ``.config``). - ``ls -lh``: Lists in long format with file sizes displayed in a human-readable format (e.g., KB, MB). **cd (Change Directory)** Allows you to navigate between directories. .. code-block:: bash cd <directory_name> **mkdir (Make Directory)** - ``mkdir directory_name``: Creates a directory in the current directory. - ``mkdir /path/to/directory``: Creates a directory at an absolute path. **rmdir (Remove Directory)** Deletes an empty directory. .. code-block:: bash rmdir <directory_name> **touch** Creates an empty file or updates the access and modification timestamps of an existing file. .. code-block:: bash touch <filename> **rm (Remove)** Deletes files or directories. .. code-block:: bash rm <filename> rm -r <directory_name> **cp (Copy)** Copies files or directories. .. code-block:: bash cp <source_file_adr> <destination_file_adr> cp -r <source_dir_adr> <destination_dir_adr> **mv (Move)** Moves or renames files and directories. .. code-block:: bash mv <source_file/dir_adr> <destination_file/dir_adr> **cat** Displays the contents of a file. .. code-block:: bash cat <filename> **more or less** View the contents of a file one screen at a time. .. code-block:: bash more <filename> less <filename> **head and tail** Display the beginning or end of a file. .. code-block:: bash head <filename> tail <filename> **grep (Global Regular Expression Print)** Search for text patterns in files. .. code-block:: bash grep <pattern> <filename> **\ -\- help** Access the usage and options of commands to get detailed information. .. code-block:: bash <command> --help **man (Manual)** Access the usage and options of commands to get detailed information. .. code-block:: bash man <command> **Termination** Terminate a shell session or log out of a user session. .. code-block:: bash exit logout **> (Output Redirection)** Redirect the output of a command to a file, overwriting the file if it already exists. .. code-block:: bash <command> > <filename> **>> (Append Output)** Redirect the output of a command to a file, but it appends the output to the end of the file if it already exists. .. code-block:: bash <command> >> <filename> **tee** Display the output of a command on the screen, as well as save it to a file. .. code-block:: bash <command> | tee <filename> <command> | tee -a <filename> # Append to the existed file **echo** Print text or messages. .. code-block:: bash echo "<text>" echo "$(date)" Path Notations and Shortcuts ------------------------------ **Absolute Path** An absolute path specifies the location of a file or directory from the root directory. It starts with a forward slash ("/"). For example: .. code-block:: bash /home/user/documents/file.txt **Relative Path** A relative path specifies the location of a file or directory relative to the current working directory. It does not begin with a forward slash. For example, if your current directory is `/home/user`, then `documents/file.txt` is a relative path. **. (Dot)** Represents the current directory. It can be used to reference files or directories in the current directory. For example, `./script.sh` runs a script in the current directory. **.. (Double Dot)** Represents the parent directory. It is used to navigate up one directory level. For example, `../../file.txt` references a file two levels up in the directory structure. **~ (Tilde)** Represents the home directory of the current user. It can be used to reference files or directories in the user's home directory. For example, `~/documents/file.txt` references a file in the user's home directory. **cd (Change Directory)** - `cd directory_name`: Changes the current directory to the specified directory. - `cd /absolute/path/to/directory`: Changes the current directory to an absolute path. - `cd ..`: Moves up one directory level (parent directory). - `cd .`: Stays in the current directory (useful when you want to execute a command in the current directory). - `cd ~` or `cd`: Takes you to your home directory. **Environment Variables** You can use environment variables in paths to make them more dynamic. For example, `$HOME` represents the home directory, and you can use it like `cd $HOME/Documents`. Command-Line Text Editors ------------------------- Linux offers a wide range of text editors, from command-line options like Vi, Vim, and Nano to graphical editors like gedit and Kate. **Vi** Vi is a popular command-line text editor in Linux. Here are some essential commands for using Vi: Open a file for editing or create a new file with the specified filename. .. code-block:: bash vi <filename> There are two modes in Vi, Editing mode and command mode. To enter editing mode: - `i`: Insert text before the cursor. - `a`: Append text after the cursor. And to enter command mode press ESC and you have following commands: - `Arrow keys`: Move the cursor left, down, up, and right. - `G`: Go to the end of the file. - `x`: Delete the character under the cursor. - `dd`: Delete the current line. - `yy`: Copy the current line. - `p`: Paste the copied text after the cursor. - `u`: Undo the most recent change. - `Ctrl-r`: Redo the most recent undone change. - `w`: Save changes to the file. - `q`: Quit Vi. - `wq` or `:x`: Save changes and quit. - `q!`: Quit Vi without saving changes. .. admonition:: Task #. Create a new directory called <your_first_name> in your home directory. #. Navigate to the created directory. #. Create a new directory inside it called "Original". #. Create a text file called "todo.txt" inside the inner directory. #. Use the vi text editor to open "todo.txt" and add some sample to-do items. Save and exit the file. #. Create another directory called "Backup" inside <your_first_name>. #. Copy only the "todo.txt" file from the "Original" directory to the "Backup" directory. #. Simultaneously display "Backup completed successfully" on the screen and save this text to a file called "backup.log" inside the "Backup" directory. #. Simultaneously display "Backup at <current_date_and_time>" on the screen and append it to "backup.log". #. List the contents of the <your_first_name> directory and save it into a text file called "list_my_dir.txt". File Transfer ------------- While there are various methods available for secure file transfers, in this context, we specifically utilize SFTP, which stands for Secure File Transfer Protocol. It is a secure and encrypted protocol used for transferring files between a local and a remote system. It is commonly used in Linux for securely transferring files to and from remote servers over an SSH connection. Before using SFTP, ensure that an SFTP server is running on the remote host. To initiate an SFTP session from your local Linux system to DRACO Cluster, open a terminal and use the following command: .. code-block:: bash sftp <username>@login1.draco.uni-jena.de You will be prompted for your password or key passphrase to authenticate. **SFTP Commands** Once connected, you can use various commands to navigate and transfer files. Here are some common SFTP commands: - ``ls``: List files and directories on the remote server. - ``cd``: Change the remote directory. - ``get``: Download a file from the remote server to your local system. - ``put``: Upload a file from your local system to the remote server. - ``pwd``: Print the current remote directory. - ``quit`` or ``exit``: Disconnect from the remote server. **File Transfers** To transfer files, use the ``get`` and ``put`` commands. For example, to download a file, use ``get <filename>``, and to upload, use ``put <filename>``. .. admonition:: Task #. Pick two file transfer methods (e.g., SCP, SSHFS, FTPS, ...). #. Transfer a file from your local system to your account on ARA using that two method. #. Give a short summary of their workings and differences. System's Hardware Information ----------------------------- The Linux command-line utility, ``lscpu``, is employed to retrieve system CPU information. This command acquires CPU architecture details from the ``sysfs`` and ``/proc/cpuinfo`` files, then presents the information within terminal. You can also view system memory information using the ``cat /proc/meminfo`` command. This command will display detailed information about the system's memory usage and configuration.