Linux Commands - Imran
Linux Commands - Imran
c d ~
ls -la
Shebang Explanation
●
#!
: This is the shebang.
●
/bin/bash
: This specifies the path to the Bash interpreter.
1.Shebang (
#!/bin/bash
): When you create a shell script
nd add
a #!/bin/bashat 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
If I use thels -lacommand again, I notice thatthe 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
Setting Flags:
Exercise:
Instructions
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 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 15: Type the command ls -l and press Enter. Note how many files and
directories are in the dir1 directory.
Step 17: Type the command ls -l and press Enter. Note how many files and
directories are in the dir2 directory.
Step 19: Type the command ls -l and press Enter. Note how many files and
directories are in the dir3 directory.
Exercise completed.
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 returnthe word
count)
ipes: A coding toolthat allows the output of one command to
P
be used as the input for another command
ls | 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)
Lesson 4: REDIRECTION:
2 >[For Error]
less error.txt
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.
Lesson 6:grep
rep file1.txt
g
It returns all the contents of a file in a random order as present in
the file.
ow the command will return all the names with sam at any place
N
in a name (Partial match in middle or end)