Working with Basic Linux Daniel Balagué
How Linux Works? Everything in Linux is either a file or a process. A process is an executing program identified with a PID number. It runs in short or long duration or even indefinitely. A file is a collection of data, generated by text editor, compiler, running application, etc. A file can be in plain text or in binary, and there are many file formats associated to the application-types. The Linux kernel is responsible for organizing the processes and interacting with files. It allocates time and memory to the processes and handles the file system and communication with files after the system calls.
What is the Shell? HUMAN English: I want to delete this.file Human types on a keyboard: rm this.file MACHINE Shell: translates human to machine & communicates instructions to the Kernel (which performs the actual work)
What is the Shell? The shell is the how you invoke the commands by typing into the terminal. It is interactive but not graphic-based. The shell is really powerful: it allows you to do a lot in just one line. The kernel's job is to talk to the hardware and software, and to manage the system's resources as best as possible.
Warning! Often there is no "undo" option or warnings in the shell. Source: http://www.quickmeme.com/meme/3prykv
What the Linux Shell Does 4 Tasks! 1. Display prompt 2. Read command 3. Process command 4. Execute command / display output And back to 1!
The BASH Shell We have a few shells available, but we are going to work with the BASH shell.
Getting Started! In the next few slides, we will learn: Navigating the file system Managing files and directories Permissions and Groups
Before We Start: There is Always Help If you want to know how a tool works, we can look for more information in the man pages. Just run the command man followed by the tool name. For example, run man man
Files and File Names A file is a basic unit of storage. The name can be long and contain any characters, but typically just choose characters and numbers and - or _ Avoid special characters like., / : ;! * & " and spaces! Linux is case sensitive! There are many types of files: We will explore the file command later.
Some File Commands Command Description ls cp mv clear less grep rm mkdir pwd List out the directory Copy files Move files or rename files Clean the screen Visualize a file (do not open large files with text editors!) searching plain-text data sets for lines matching a regular expression. Remove files (be careful with this command: rm -rf * -- it is one of the most dangerous!) Create directory Know where you are
Hint When you start writing the name of a file, you can press the Tab key to autocomplete it. If there are more than one file that start with the same chain of characters they will be listed.
Example We used pwd to know where we are and then ls to know what we could find in the presentation directory.
More Options for ls Check the output of the following command: ls -lh What does it do? HINT: use man ls
Your Home Directory When you log into the HPC cluster or when you start a terminal session, you are usually starting at your home directory. Since the home directory is so important, there are many ways to go back into it. Here are some commands that will bring you back to home: cd cd ~ cd $HOME cd /home/username wherever you go, you can always come home ;)
Create Directories To create a directory, we use the mkdir command: mkdir directory_name Exercise: create a directory called presentation in your home folder. Solution: cd mkdir presentation
Change the Working Directory to a Different Directory Assume we are in $HOME. We can move into a different directory in two ways. Relative: to change from the home directory into presentation we do cd presentation We are assuming that presentation is inside the home directory Absolute: to change from the current directory into presentation we specify the full path: cd /home/username/presentation
Moving and Renaming Files and Directories Let s create a directory called Section1 into our home folder: cd mkdir Section1 To move the directory Section1 into presentation, we use the command mv mv Section1 presentation To rename a file or directory we use the mv command as well. mv presentation Presentation
Copying Files cd Presentation cp Section1../ Copies the Section1 subdirectory into the parent directory. You could add the flag -r to copy directories recursively.
Permissions (I) As a regular user, try to get into the root s home directory with cd /root We do not have permission to access the root directory. Image source: https://www.memecenter.com/fun/99372/you-shal L-NOT-PASS
Permissions (II) Check the following example: In this example we can observe: Three access types: Read, Write, and execute. The Owner and Group for the the files listed: dxb507 (owner), hpcadmin (group).
Permissions (III) The first column of the output of ln -lh has a meaning. Let s look into more detail at the helloworld.c file: - (rw-) (r--) (r--) Type of file User Group Others The user has granted permission to read and write to the file. The group has permission to read the file as well as the other users in the system. The - (dash) means permission has not been assigned. Types of files: d - a directory. l - a symbolic link to another program or file elsewhere on the system. - (dash) - a regular file (other than directory or link).
Changing Permissions: chmod Sometimes you may not want the other members of your group to read the files. To convert a file into a file just readable and writable by you, use: chmod 600 my_file If you want the whole directory and its contents: chmod -R 600 my_file The -R option stands for recursive. The numbers explained (with an example): User Group Others rwx rw- r-- 111 110 100 These are binary numbers: 111 in binary is 7 in decimal, 110 in binary is 6 in decimal, 100 in binary is 4 in decimal. 764 For 110, you make the conversion like this: 1x2 2 + 1x2 1 + 0x2 0 = 4 + 2 + 0 = 6
Quick Command Using chmod 740 my_file you can rapidly give yourself permission to execute your file.
Groups All users are part of (at least) one group. If you are in multiple groups, you might want to change groups. Sometimes you need to specify your group when submitting a job. Check your current group: Check in which groups you belong: Changing groups: id -ng groups newgrp <different_group_name>
Changing User and Group Ownership: chown Similarly, we can change the user and keep the group for a particular file: chown new_user:same_group my_old_file To change the user and the group: chown new_user:new_group my_old_file (find more options in the man pages).
Thank you! Questions?