0% found this document useful (0 votes)
9 views10 pages

Linux Commands - Imran

Uploaded by

ratherimran99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Linux Commands - Imran

Uploaded by

ratherimran99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

‭Linux Commands:‬

‭Lesson1: Some commands‬

c‭ d ~‬
‭ls -la‬

‭1. bashrc file‬

l‭ess‬‭.‭b ‬ ashrc‬ ‭command‬


‭(Takes us to the less environment where we can write bash‬
‭scripts for configurations like setting color of terminal , shell‬
‭history etc etc)‬
‭[The .bashrc file is mainly for configurations, It is esentially the‬
‭script file that is executed when we first open the terminal‬
‭window. What is in their will be the configurations for the shell‬
‭itself]‬

‭2. .profile file‬

l‭ess .profile command‬


‭This tends to be used for mainly the environment variables.‬
‭ or example, I can use it for setting environment‬‭variables for my Java‬
F
‭home directory or my Python home directory‬

‭We press‬‭q‬‭to come out of the less environment.‬

‭3. Now let’s create a simple shell script‬

‭A script is nothing but a file with lines of commands or code.‬


‭ e use VIM which is an editor (in the Unix world) which accepts‬
W
‭input.‬

‭vim testshell .sh‬

‭Then click‬‭i‬‭on keyboard to go into the INSERT MODE.‬

‭Then we go into what is know as the Shebang mode.‬‭#!‬

‭Shebang Explanation‬

‭●‬ ‭
#!‬
‭: This is the shebang.‬
‭●‬ ‭
/bin/bash‬
‭: This specifies the path to the Bash interpreter.‬

‭What These Commands Do‬

‭1.‬‭Shebang (‬‭
#!/bin/bash‬
‭)‭:‬ When you create a shell script‬
‭ nd add‬‭
a #!/bin/bash‬‭at the top, it tells the operating‬
‭system to use the Bash interpreter to run the script. This‬
‭makes your script portable and ensures it runs correctly‬
‭regardless of the shell environment of the user running it.‬

‭Then do:‬

‭#!/bin/bash‬

‭echo "Hello, World!"‬

‭Then press esc to get out of the INSERT mode‬

‭Finally‬‭:wq!‬‭To save the file. Now the file‬‭testshell.sh‬‭is saved.‬


‭ ow if we do‬‭ls -la‬‭we’ll see that our‬‭testshell.sh‬‭script file has‬
N
‭been created.‬

‭ owever, script files need to be run and at this moment this‬


H
‭cannot be run. It is just a read white file at this moment.‬

‭ ut I want it to be executable, which requires that I have an‬‭x‬


B
‭being set on it. In order to do that, I have to use another‬
‭command which is called‬‭chmod‬‭.‬
‭ fter using this command, I type in the type of permissions that I want. I‬
A
‭type in 755. Then I want to set the file that I want to add the permissions to,‬
‭which is testshell.sh.‬

‭chmod 755 testshell.sh‬

I‭f I use the‬‭ls -la‬‭command again, I notice that‬‭the file has now been‬
‭turned into an executable file. This means that I can now run the file from‬
‭the command line. To run the file, I press./testshell.sh followed by the Enter‬
‭key. Now you notice the words hello world are printed out on the screen.‬
‭This is how you can create simple scripts and make them executable within‬
‭the bash shell.‬
‭TO RUN the shell script‬

‭./testshell.sh‬

‭Lesson 2: More Commands‬


‭a)‬‭pwd:‬‭Print Working Directory‬
‭b)‬‭ls‬‭: List command - To check the Contents of directory‬

‭Setting Flags:‬

‭ls -l :‬‭Returns list of files and directories in the‬‭list structure‬

‭Exercise:‬

Instructions‬

Step 1: Open Terminal in your VS code editor.‬


Step 2: Type the command mkdir lab and press Enter‬


Step 3: Change to the lab directory by typing cd lab and pressing Enter‬

Step 4: Type the command touch file1.txt and press Enter to create a file named‬

file1.txt‬

Step 5: Type the command mkdir dir1 and press Enter‬


Step 6: Type the command mv file1.txt dir1/ and press Enter to move file1.txt to‬

the dir1 directory‬

Step 7: Type the command touch file2.txt and press Enter to create a file named‬

file2.txt‬

Step 8: Type the command mkdir -p dir2/dir3 and press Enter. We're using the -p‬

flag to create the parent directories if they do not exist. In this case it will‬

create the dir2 directory and then create the dir3 directory inside of dir2.‬

Step 9: Type the command mv file2.txt dir2/dir3/ and press Enter to move file2.txt‬

to the dir3 directory‬

Step 10: Change to the dir2 directory by typing cd dir2‬

Step 11: Type the command touch file3.txt and press Enter to create a file named‬

file3.txt‬

Step 12: Type the command mv file3.txt ../ and press Enter to move file3.txt to the‬

lab directory‬

Step 13: Type the command cd .. and press Enter to navigate back to the lab‬

directory‬

Step 14: Type the command cd dir1 and press Enter.‬


Step 15: Type the command ls -l and press Enter. Note how many files and‬

directories are in the dir1 directory.‬

Step 16: Type the command cd ../dir2 and press Enter.‬


Step 17: Type the command ls -l and press Enter. Note how many files and‬

directories are in the dir2 directory.‬

Step 18: Type the command cd dir3 and press Enter.‬


Step 19: Type the command ls -l and press Enter. Note how many files and‬

directories are in the dir3 directory.‬

‭Exercise completed.‬

‭ esson-3: Pipes and Redirection‬


L
‭Pipes:‬

‭ at text1.txt‬
c
‭To check the contents of a file which is some simple text.‬

‭ c : Word Count‬
w
‭wc text.txt -w‬‭(w flag tells the wc command to return‬‭the word‬
‭count)‬
‭ ipes: A coding tool‬‭that allows the output of one command to‬
P
‭be used as the input for another command‬

‭ls | wc -w‬

‭cat text1.txt | wc -w‬

[‭Here we first got the text1.txt file and then we used pipe to make‬
‭sure the output of the first command (which is text in a text1.txt‬
‭file), is used by wc(word count command to count the total words‬
‭in the file text1.txt)‬

‭ etting a combined word count of two files in one command.‬


G
‭cat text1.txt text2.txt | wc -w‬

‭Lesson 4: REDIRECTION:‬

‭ td Input < (less than sign‬


S
‭Std Output > (less than sign)‬

‭We use cat command to insert content to a file.‬

‭ at > file1.txt (then Enter) [Then add the content]‬


c
‭We live in the world of algorithms. …‬
‭[Then we hit cmd+D to tell the cat command that it is the end of a‬
‭file.]‬
‭Lesson 5: Standard Error‬

‭ rrors happen when things go wrong.‬


E
‭We use‬‭2 >‬‭after the command to highlight the errors.‬

‭2 >‬‭[For Error]‬

‭2 > &1‬‭[To combine the error with the standard output]‬

‭ ifferent streams for input/output and error:‬


D
‭Each uses their own streams‬

‭ hen we are trying to access a wrong directory, which doesn’t‬


W
‭exist.‬
‭ls -l /bin/user > error.tx​​t‬

‭No such file or directory Error:‬

‭Now let’s use error flag‬

‭ls -l /bin/user 2> error.txt‬

‭Now the error stream has been used.‬

‭To check the contents of the file, we use:‬

‭less error.txt‬

I‭n this case the error message is displayed in the error.txt‬


‭file:‬

‭ his is printed in the file now instead of the terminal giving the‬
T
‭output. Now error.txt is created.‬
‭Now to do the best of both worlds.‬

‭ls -l /bin/usr > error_output.txt 2>&1‬

‭This is the output: Anyways, I need to revisit it.‬

‭Lesson 6:grep‬

‭Grep is used to search for particular patterns and in the file.‬

‭ rep file1.txt‬
g
‭It returns all the contents of a file in a random order as present in‬
‭the file.‬

‭ rep sam file1.txt‬


g
‭Now the file returns all the content starting with sam‬
‭grep is case-sensitive‬

‭So‬‭grep Sam file1.txt‬‭will return different result.‬

‭We can add a flag to avoid the case-sensitivity‬

‭grep -i sam file1.txt‬

‭ ow the command will return all the names with sam at any place‬
N
‭in a name (Partial match in middle or end)‬

‭ e can do an exact match with -w flag‬


W
‭grep -w Sam file1.txt‬

‭We get exact same result‬‭Sam‬‭as the output - the exact‬‭match‬

You might also like