├── tut1 ├── intro.md ├── task1.md └── tut1.md ├── tut2 ├── task.md ├── tools │ ├── Ranger.md │ └── tmux.md └── tut.md └── tut3 ├── images ├── IO.png ├── IO_OPERATIONS.png ├── PIPE.png └── TEE.png ├── part1.md ├── part2.md └── quiz.md /tut1/intro.md: -------------------------------------------------------------------------------- 1 | 2 | # CLI 3 | 4 | 5 | ## Introduction 6 | 7 | 8 | > **We will learn about Linux and its command line 9 | interface**. 10 | 11 | 12 | ## Pre-requisits 13 | 14 | * Basic knowledge what is Linux 15 | 16 | * Have Linux installed on your computer. You can install it using a virtual machine, [Watch This video](https://www.youtube.com/watch?v=IOwlnpWPuj0) 17 | 18 | * You can install any distribution **Ubuntu** is prefered. 19 | 20 | * No programming skills wanted 21 | 22 | * # **Always Search** 23 | ## Pre-requisits 24 | 25 | > **With CLI, you can do a lot of things can't be reached with GUI**. 26 | 27 | > **Faster, with one command you can do the effect of several mouse clicks**. 28 | 29 | > **Enable automation and scripting**. 30 | 31 | ## Linux File-System Layout 32 | 33 | **The difference between Linux and windows file structure.** 34 | 35 | |Linux| Windows| 36 | |-----| -------| 37 | |We hava a single tree(unified file-system)| Each partition of drive will have its own separate tree (C, D, E) 38 | |The head of the tree is called root \" \/ \"| Plugging a flash or portable hard-disk results in new tree 39 | |![Linux file-system](https://upload.wikimedia.org/wikipedia/commons/4/4b/Linux_file_system_foto_no_exif_%281%29.jpg)| ![Windows file-system](https://flylib.com/books/2/97/1/html/2/images/04fig07.jpg) 40 | -------------------------------------------------------------------------------- /tut1/task1.md: -------------------------------------------------------------------------------- 1 | # Task_1 Practice 2 | 3 | > Create a directory called task1. 4 | 5 | > Search for a command to create a file. 6 | 7 | > Create a directory called **subtask2** in another directory called **subtask1** int the **task1** directory with **one** command 8 | 9 | > return back to the parent **task1** with **one** command 10 | 11 | > Search for the command that creats a file. and create a file called **task.txt**. Write anything in this file. 12 | 13 | > Search for a command to display the content of that file on terminal. 14 | 15 | > copy this file in **subtask2** directory 16 | 17 | > create a c++ file called task.cpp 18 | ```c++ 19 | #include 20 | using namespace std; 21 | 22 | int main() { 23 | cout << "Linux Community :)\n"; 24 | return 0; 25 | } 26 | 27 | ``` 28 | > Search for the command that recognizes the type of data contained in a file, run this command on the child directory, the text file and the c++ file and see the differences. 29 | 30 | > how to know the last modification time of a file using **ls** command. 31 | 32 | > After using the last command and seeing the whole format, what these letters stand for [r, x, w, d] 33 | 34 | # Remember, Always Search 35 | # Good Luck :) -------------------------------------------------------------------------------- /tut1/tut1.md: -------------------------------------------------------------------------------- 1 | # Basic concepts & basic commands 2 | 3 | ## Files & Filenames 4 | * **Filenames can use any characters**. 5 | 6 | * **Filenames are case sensetive** 7 | * ahmed.txt 8 | * Ahmed.txt 9 | 10 | 11 | * **Filenames started with "." is called hidden files** 12 | * .bashrc 13 | * .git 14 | * .ignore 15 | * .profile 16 | 17 | * **No concept for file extensions** 18 | * result.txt 19 | * result.txt.old 20 | * result 21 | 22 | ## Paths 23 | 24 | > ### ' ~ ' means the home directory 25 | > ### ' . ' means the current directory 26 | > ### ' .. ' means the parent directory 27 | > ### ' / ' To separate between directories' names. 28 | 29 | 30 | ## Basic commands 31 | 32 | 33 | * ### General form of commands, 34 | `$ [options] [arguments]` 35 | 36 | |Command| Effect| 37 | |-------|-------| 38 | |**ls**| To list files| 39 | |**tree**| To list a tree of files| 40 | |**pwd**| To show the current working directory| 41 | |**cd**| To change the current directory| 42 | |**mkdir**| To make a directory| 43 | |**cp**| To copy files & directories| 44 | |**mv**| To move or renames files & directories| 45 | |**rm**| To remove files & directories| 46 | |**man**| To show the manual page of the program| 47 | |**clear**| To clear the screen| 48 | 49 | --- 50 | ## sudo 51 | 52 | > It stands for **super user do!** 53 | 54 | > The sudo command allows you to run programs with the security privileges of another user 55 | 56 | > It likes (Run as administrator) in windows. 57 | 58 | 59 | > ![Sudo command](https://res.cloudinary.com/acloud-guru/image/fetch/c_thumb,f_auto,q_auto/https://acg-wordpress-content-production.s3.us-west-2.amazonaws.com/app/uploads/2020/06/sandwich-1.png) 60 | 61 | --- 62 | * ### Some commands' options 63 | 64 | |Command| Effect| 65 | |--------|------| 66 | |**ls -a**| To list all files including hidden files| 67 | |**tree -d**| Only show directories| 68 | |**mkdir -p**| Make intermediate folders if needed| 69 | |**cp -r**| Copy the folder with its contents| 70 | |**mv -r**| Move or rename a directory with its content to another directory| 71 | |**rm -r**| Delete a directory with its content| 72 | |**cd -**| changes to the previous directory; where the user was previously to the current directory| 73 | -------------------------------------------------------------------------------- /tut2/task.md: -------------------------------------------------------------------------------- 1 | # Task_2 practice 2 | 3 | > Before you create project files, use the mkdir command with brace expansion to create 4 | empty project planning documents in the **/home/student/Documents/project_plans** 5 | directory. (**Hint: if ~/Documents does not exist, the -p option for the mkdir command will 6 | create it.**) 7 | 8 | > Create two empty files in the **~/Documents/project_plans** directory: 9 | **season1_project_plan.odf and season2_project_plan.odf**. 10 | 11 | > Create a total of 12 files with names **tv_seasonX_episodeY.ogg**. Replace X with the 12 | season number and Y with that season's episode, for two seasons of six episodes each. 13 | 14 | > As the author of a successful series of mystery novels, your next bestseller's 15 | chapters are being edited for publishing. Create a total of eight files with names 16 | **mystery_chapterX.odf**. Replace X with the numbers 1 through 8 17 | 18 | > Use a single command to create two subdirectories named **season1** and **season2** under the 19 | **Videos** directory, to organize the TV episodes. 20 | 21 | > Move the appropriate TV episodes into the season subdirectories. Use only two commands, 22 | specifying destinations using relative syntax. 23 | 24 | > Create a 2-level directory hierarchy with a single command to organize the mystery book 25 | chapters. Create **my_bestseller** under the **Documents** directory, and **chapters** under 26 | the new **my_bestseller** directory. 27 | 28 | > Create three more subdirectories directly under the **my_bestseller** directory using a single 29 | command. Name these subdirectories **editor**, **changes**, and **vacation**. 30 | 31 | > Change to the **chapters** directory. Using the tilde **(~)** home directory shortcut to specify 32 | the source files, move all book chapters to the chapters directory, which is now your current 33 | directory. What is the simplest syntax to specify the destination directory? 34 | 35 | > You sent the first two chapters to the **editor** for review. Move only those two chapters to the 36 | **editor** directory to avoid modifying them during the review. Starting from the chapters 37 | subdirectory, use brace expansion with a range to specify the chapter file names to move and 38 | a relative path for the destination directory. 39 | 40 | > While on **vacation** you intend to write chapters 7 and 8. Use a single command to move 41 | the files from the chapters directory to the **vacation** directory. Specify the chapter file 42 | names using brace expansion with a list of strings and without using wildcard characters. 43 | 44 | > Change your working directory to **~/Videos/season2**, and then copy the first episode of 45 | the season to the **vacation** directory. 46 | 47 | > Use a single cd command to change from your working directory to the **~/Documents/ 48 | my_bestseller/vacation** directory. List its files. Use the previous working directory 49 | argument to return to the **season2** directory. From the **season2** directory, copy the episode 2 file into the vacation 50 | directory. Use the shortcut again to return to the vacation directory. 51 | 52 | > The authors of chapters 5 and 6 want to experiment with possible changes. Copy both files 53 | from the **~/Documents/my_bestseller**chapters directory to the **~/Documents/my_bestseller/changes** directory to prevent these changes from modifying original files. 54 | Navigate to the **~/Documents/my_bestseller** directory. Use square-bracket pattern 55 | matching to specify which chapter numbers to match in the filename argument of the cp 56 | command. 57 | 58 | > After further review, you decide that the plot changes are not necessary. Delete the **changes** 59 | directory. 60 | 61 | > When the **vacation** is over, the **vacation** directory is no longer needed. Delete it using the 62 | **rm** command with the recursive option. -------------------------------------------------------------------------------- /tut2/tools/Ranger.md: -------------------------------------------------------------------------------- 1 | # Ranger 2 | 3 | **What is ranger?** 4 | 5 | Ranger is a small and efficient console-based file manager with VI key bindings, it comes with a minimalistic interface displaying the directory hierarchy allowing you to quickly navigate through different files. 6 | 7 | **Ranger features:** 8 | 9 | Common file operations such as copy, delete, create, etc… 10 | 11 | Renaming multiple files at once 12 | 13 | Uses rifle ( a file launcher that determines automatically which programs to use for each type of files) 14 | 15 | Tabs, Bookmarks, Mouse support 16 | 17 | Multi-column display 18 | 19 | UTF-8 Support 20 | 21 | Preview of the selected file/directory. 22 | 23 | VIM-like console 24 | 25 | **Installing ranger:** 26 | 27 | Ubuntu/Debian 28 | 29 | $ sudo apt install ranger 30 | 31 | CentOs 32 | 33 | $ sudo yum install ranger 34 | 35 | Arch Linux 36 | 37 | $ sudo pacman -S ranger 38 | 39 | **Starting ranger:** 40 | 41 | $ ranger 42 | 43 | **Useage:** 44 | 45 | Appearance: The first column shows the parent directory, the second is the main column and the third column shows a preview of the current file/directory. 46 | 47 | Navigation: using arrows or (h j k l) 48 | 49 | Open a file: Enter 50 | 51 | Close (quitting) a file: q 52 | 53 | **Optional Dependencies:** 54 | 55 | To enable the file preview of images and videos, we need to install the following dependencies : 56 | w3mimgdisplay or ueberzug for images previewsffmpegthumbnailer for video thumbnailspdftoppm bb to preview of PDF 57 | 58 | Installation: 59 | 60 | On Ubuntu/Debian 61 | 62 | $ sudo apt-get install ueberzug ffmpegthumbnailer poppler-utils 63 | 64 | On Arch Linux 65 | 66 | $ sudo pacman -S ueberzug ffmpegthumbnailer poppler 67 | 68 | **For more information about ranger:** 69 | 70 | $ man ranger -------------------------------------------------------------------------------- /tut2/tools/tmux.md: -------------------------------------------------------------------------------- 1 | # Tmux 2 | 3 | **What is tmux?** 4 | 5 | Tmux is a terminal multiplexer which enables a number of terminals to be created, accessed, and controlled from a single screen, this is called tmux session.Tmux sessions are persistent, so it means that tmux will continue to run in the background even if you get disconnected. 6 | 7 | With Tmux you can easily switch between multiple programs in one terminal, detach them and reattach them to a different terminal. 8 | 9 | **How to install tmux on linux?** 10 | 11 | On Ubntu & Debian: 12 | 13 | sudo apt-get install tmux 14 | 15 | On Arch: 16 | 17 | sudo pacman -S tmux --noconfirm 18 | 19 | **How to use tmux ?** 20 | 21 | Starting a tmux session: 22 | 23 | $ tmux 24 | 25 | Creating a named tmux session: 26 | 27 | $ tmux new -s session_name 28 | 29 | Detaching from tmux session (returning back to shell): 30 | 31 | Ctrl+b d 32 | 33 | listing all tmux sessions: 34 | 35 | $ tmux ls 36 | 37 | attaching to a tmux session: 38 | 39 | $ tmux attach-session -t session_number 40 | 41 | useful commands: 42 | 43 | Ctrl+b ? get a list of all commands 44 | 45 | Ctrl+b c create a new window 46 | Ctrl+b w choose window from a list 47 | Ctrl+b , rename the current window 48 | Ctrl+b % split current pane horizontally into two panes 49 | 50 | Ctrl+b " split current pane vertically into two panes 51 | 52 | Ctrl+b o go to the next pane 53 | 54 | Ctrl+b ; toggle between the current and previous pane 55 | 56 | Ctrl+b x close the current pane 57 | -------------------------------------------------------------------------------- /tut2/tut.md: -------------------------------------------------------------------------------- 1 | # Wild Cards 2 | 3 | 4 | 5 | > patterns that work as placeholders in file and directory names 6 | 7 | _**if you forget the actual name of a file or you want to use a command on a group of files**_ 8 | 9 | # \* 10 | 11 | used to replace one char or more (including zero) 12 | 13 | ### Example 14 | 15 | ```bash 16 | touch file{1..1000}.cpp 17 | // try to remove all files that has numbers between 400 and 500 18 | ``` 19 | 20 | # ? 21 | 22 | replace a single char 23 | 24 | ### Example 25 | 26 | in the above example delete all files between 30 and 40 27 | 28 | #### _what if you need a condition on this char?_ 29 | 30 | use [] and put the condition inside 31 | 32 | | example | explain | 33 | | ----------- | --------------------------------------------- | 34 | | [abc] | a or b or c | 35 | | [!abc] | each char except (a,b,c) | 36 | | [2-5] | from 2 to 5 | 37 | | [!2-5] | each char except (2,3,4,5) | 38 | | [a-z] | all lowercase english letters | 39 | | [a-z2-91-5] | all lowerCase english letters and from 1 to 9 | 40 | 41 | ### Example 42 | 43 | delete all files between 35 and 40 44 | 45 | ### useful class names 46 | 47 | | class name | usage | other method | 48 | | ----------- | --------------------- | ------------ | 49 | | [[:alpha:]] | Alphabets | [a-zA-Z] | 50 | | [[:alnum:]] | Alphabets and numbers | [a-zA-Z0-9] | 51 | | [[:digit:]] | numbers | [0-9] | 52 | | [[:lower:]] | lower case | [a-z] | 53 | | [[:upper:]] | upper case | [A-Z] | 54 | 55 | # {} 56 | 57 | very powerful wild card 58 | used to group selections 59 | 60 | ### Example 61 | 62 | touch {name1,name2}.{h,cpp} 63 | touch file{1..100}.cpp 64 | 65 | ### Note 66 | 67 | _what is the difference between rm file[0-9].cpp , file{0..9}.cpp_ 68 | 69 | # \ 70 | 71 | escape any special letter or space 72 | -------------------------------------------------------------------------------- /tut3/images/IO.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedasad236/Linux_Club/565a510f37d75e5a8972174157e31318f8713921/tut3/images/IO.png -------------------------------------------------------------------------------- /tut3/images/IO_OPERATIONS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedasad236/Linux_Club/565a510f37d75e5a8972174157e31318f8713921/tut3/images/IO_OPERATIONS.png -------------------------------------------------------------------------------- /tut3/images/PIPE.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedasad236/Linux_Club/565a510f37d75e5a8972174157e31318f8713921/tut3/images/PIPE.png -------------------------------------------------------------------------------- /tut3/images/TEE.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedasad236/Linux_Club/565a510f37d75e5a8972174157e31318f8713921/tut3/images/TEE.png -------------------------------------------------------------------------------- /tut3/part1.md: -------------------------------------------------------------------------------- 1 | # Coposite commands 2 | 3 | ## I/O streams 4 | |Function|Stream Name|Stream Descriptor|Default Device| 5 | |----|----|----|-----| 6 | Input|stdin|0| keyboard| 7 | Output| stdout| 1| screen 8 | Error| stderr|2|screen 9 | --- 10 | 11 | Composite Commands can be built through: 12 | * Sequential Commands 13 | * Conditional Commands 14 | * Command loops 15 | * Input/Output Redirection 16 | * Pipes 17 | * Command Argument Exception 18 | * Command Argument Quoting 19 | --- 20 | 21 | ## 1- Sequential Commands 22 | 23 | We can have multiple commands in the same line as follows, 24 | `; ; ...; ...; etc` 25 | #### Examples: 26 | ```bash 27 | echo "one"; sleep 5; echo "Two" 28 | ``` 29 | 30 | > Using Sequential commands is useful when the first command takes long time to execute and we don't want to wait until it is complete. 31 | ```bash 32 | g++ -g file.cpp; ./a.out 33 | ``` 34 | 35 | ## Note: 36 | ``` 37 | The seq commands just run after each other, they have independent input & output 38 | ``` 39 | 40 | --- 41 | ## Sequential Commands 42 | * ### Oring ( || ): 43 | * The second command will only execute only if the first command returns false. 44 | 45 | * ```bash 46 | cat || echo "File Not Found" 47 | ``` 48 | 49 | * It is used for error handling 50 | 51 | 52 | * ### Anding ( && ): 53 | * Second command will only execute only if the first command returns true 54 | 55 | * ```bash 56 | mkdir dir1 && cd dir1 57 | ``` 58 | --- 59 | 60 | ## Loop Commands 61 | 62 | > We can build a loop to execute a specific command for several times 63 | 64 | ```bash 65 | for file in *.txt 66 | > do 67 | > mv $file $file.old 68 | > done 69 | ``` 70 | --- 71 | 72 | ## I/O Redirection 73 | 74 | 75 | - ### Output Redirection 76 | 77 | - [` > file`] (Redirect output to file and overwrite it) 78 | 79 | - [` >> file`] (Redirect output to file and append it) 80 | 81 | - #### Example: 82 | ```bash 83 | echo "Linux community" 84 | 85 | echo "Linux community" > file.txt 86 | 87 | echo ls -a tut4 >> file.txt 88 | 89 | cat file1 file2 > mergedFiles.txt 90 | ``` 91 | 92 | - ## Note: 93 | 1. **Error messages still go to screen** 94 | 95 | 96 | 1. **">" Stands for "1>" which means redirect output streams.** 97 | --- 98 | * ### Standard Error Redirection 99 | - [` 2> file`] (Redirect Error to file and overwrite it) 100 | 101 | - [` 2>> file`] (Redirect Error to file and append it) 102 | 103 | - Examples: 104 | (Output to screen & Error to the file) 105 | ```bash 106 | make 2> log 107 | make 2>> log 108 | ``` 109 | --- 110 | 111 | * ### Both Error & Output Redirection 112 | * Output and Error go to different files. 113 | * ` >file1 2>file2` 114 | * ` >>file1 2>>file2` 115 | 116 | * Output and Error go to the same file. 117 | * ` >file1 2>&1` 118 | * ` >> file1 2>>&1` 119 | 120 | * Short version of the previous command 121 | * ` &>file` 122 | * ` &>>file` 123 | 124 | --- 125 | * ## Standard Input redirection 126 | * ` < file` 127 | 128 | * Examples: 129 | ```bash 130 | wc -l file.txt 131 | 132 | sort < log-file > sorted-log-file 133 | 134 | spell < report.txt >> errors.txt 135 | ``` 136 | 137 | -------------------------------------------------------------------------------- /tut3/part2.md: -------------------------------------------------------------------------------- 1 | # Coposite commands: __continued__ 2 | 3 | 4 | ## **What is a pipe ?** 5 | > Mechanism for communication with different processes. It enables one process to send information to other process. 6 | 7 | > ![Linux pipe](./images/PIPE.png) 8 | 9 | > A pipe is a unidirectional mechanism, so if you need data to flow in both directions, you will need two pipes(One for each direction) 10 | 11 | ## **Using Pipes** 12 | --- 13 | `$ | | ` 14 | 15 | ### **Examples:** 16 | 17 | ```bash 18 | 19 | $ cat log-file.log | grep "Error" 20 | 21 | $ man gzip | grep -i "compress" 22 | 23 | $ cat file1 file2 | grep -i "compress" 24 | 25 | $ cat * | grep -i "error" | grep -v "severe" | sort > file.log 26 | 27 | ``` 28 | 29 | ## **More about pipes** 30 | 31 | ### **tee** command: 32 | 33 | > The tee command sends the output to a set of files as well as the stdout 34 | 35 | > ` | tee ` 36 | 37 | > ![Linux tee](./images/TEE.png) 38 | 39 | ### **Examples:** 40 | 41 | > `g++ -g test1.cpp; ./a.out | tee out.txt` (Output to the screen and the file) 42 | 43 | > `g++ -g test2.cpp; ./a.out | tee -a out.txt` (Same, but uses append mode) 44 | 45 | > `date | tee file1 file2 file3 file4` (Output to the screen and the 4 files) 46 | 47 | > `g++ -g test1.cpp; ./a.out | tee out.txt >> file.txt` (output goes to out.txt and then redirect it to append file.txt) 48 | 49 | 50 | --- 51 | ## **Commands Arguments Expansion** 52 | 53 | * ### **Parameter Expansion `$` and `${}`**: 54 | * The parameter Expansion is used to evaluate shell and environment variables 55 | 56 | * **Examples:** 57 | ```bash 58 | echo $PATH 59 | 60 | echo ${PATH} 61 | 62 | MY_NAME=TOM --> No spaces 63 | 64 | echo "My name is $MY_NAME" 65 | ``` 66 | 67 | --- 68 | 69 | * ### **Arithmetic Expansion `$(())`**: 70 | 71 | * The arithmetic expansion is used to evaluate an arithmetic exression 72 | 73 | * **Examples:** 74 | 75 | ```bash 76 | echo $((5 + 6)) --> 11 77 | 78 | echo $((((5**2)) * 3)) --> 75 79 | 80 | echo $((5 % 2)) --> 1 81 | ``` 82 | --- 83 | 84 | * ### **Brace Expansion `{}`** 85 | * to repeat the command for every option in the curly brackets 86 | 87 | * **Examples:** 88 | ```bash 89 | echo abc-{A,B,C}def --> note: 90 | no spaces in the brackets 91 | 92 | 93 | echo Ahmed{A{1,2},B{3,4}} 94 | ``` 95 | --- 96 | 97 | ### **Differences between Single & double quotations** 98 | 99 | |Property|Double `" "`|Single `' '`| 100 | |-----|-----------|--------------| 101 | |_protect strings_|Protect| Protect 102 | |_protect arithmetic Expansion_|Protect|Protect| 103 | |_Variable retrieval_| Doesn't protect varibles| prootect variables -------------------------------------------------------------------------------- /tut3/quiz.md: -------------------------------------------------------------------------------- 1 | # Redirecting Output to a File or Program 2 | 3 | > ## 1. Which answer displays output to a terminal and ignores all errors? 4 | 5 | * a) &>file 6 | * b) 2> &>file 7 | * c) 2>/dev/null 8 | * d) 1>/dev/null 9 | 10 | > ## 2. Which answer sends output to a file and sends errors to a different file? 11 | 12 | * a) >file 2>file2 13 | * b) >file 1>file2 14 | * c) >file &2>file2 15 | * d) | tee file 16 | 17 | > ## 3. Which answer sends both output and errors to a file, creating it or overwriting its contents? 18 | * a) | tee file 19 | * b) 2 &>file 20 | * c) 1 &>file 21 | * d) &>file 22 | 23 | > ## 4. Which answer sends output and errors to the same file ensuring existing file content is preserved? 24 | 25 | * a) >file 2>file2 26 | * b) &>file 27 | * c) >>file 2>&1 28 | * d) >>file 1>&1 29 | 30 | > ## 5. Which answer discards all messages normally sent to the terminal? 31 | 32 | * a) >file 2>file2 33 | * b) &>/dev/null 34 | * c) &>/dev/null 2>file 35 | * d) &>file 36 | 37 | > ## 6. Which answer sends output to both the screen and a file at the same time? 38 | 39 | * a) &>/dev/null 40 | * b) >file 2>file2 41 | * c) | tee file 42 | * d) | < file 43 | 44 | > ## 7. Which answer saves output to a file and discards error messages? 45 | 46 | * a) &>file 47 | * b) | tee file 2> /dev/null 48 | * c) > file 1> /dev/null 49 | * d) > file 2> /dev/null --------------------------------------------------------------------------------