Lecture: Introduction to Linux Bash and Python
Linux Bash Basics
- Display the current directory:
- List files and directories:
- Change directory:
- Create a new directory:
- Remove a directory:
- Copy a file:
- Move a file:
- Print the contents of a file:
- Search for a specific string in a file:
- Count the number of lines in a file:
- Find and replace text in a file:
- Check disk usage of a directory:
- Display system information:
- Calendar
- bc
- whatis
- Who
- echo
- head
The term
head
can have multiple meanings depending on the context. It can refer to the part of the body that contains the brain and sensory organs, or it can be used to describe a person who is in charge or a leader of a group. It can also refer to the top or frontmost part of something, such as the head of a bed or the head of a company. - Tail
pwd
ls
cd <directory_name>
mkdir <directory_name>
# if you want to add nested folders
mkdir -p /this/is/nested
rmdir <directory_name>
# or you can remove recursivly using
rm -rf
cp <source_file> <destination_file>
mv <source_file> <destination_file>
cat <file_name>
grep <search_string> <file_name>
wc -l <file_name>
sed 's/<old_text>/<new_text>/g' <file_name>
du -sh <directory_name>
uname -a
# or
cat /etc/os-release
128.4.121.165 ==> internal
?? ==> external
The cal
command is used to display a calendar. When used without any arguments, it displays the current month's calendar. If a specific year is provided as an argument, it displays the calendar for that year.
cal
cal 2019
The bc
command is a command-line calculator that allows you to perform mathematical calculations in the Linux command line. It provides functionalities for basic arithmetic operations, advanced mathematical functions, and variable assignment.
bc
198 * 2
x= 22
y= 33
(x*2)+(y*2)
The whatis
command in Linux is used to display a short description of a given command. It provides a brief explanation of the purpose or functionality of the specified command.
whatis grep
whatis sudo
...
The who
command in Linux is used to display information about the users who are currently logged in to the system. It provides details such as the username, terminal, login time, and IP address of the logged-in users.
who
# or
w
The echo
command is used in Linux to display a line of text or a variable value on the terminal. It is commonly used for printing messages or variables in shell scripts or on the command line. When used with the -e
option, it can also interpret backslash escapes.
For example:
echo "Hello, World!"
This will output "Hello, World!" on the terminal.
You can also use variables with echo
:
name="John"
echo "Hello, $name!"
This will output "Hello, John!" on the terminal.
The term "tail" has multiple meanings depending on the context. In the context of animals, it refers to the elongated rear end of certain animals. In the context of clothing, it refers to a trailing or hanging part at the back of a garment. In the context of data or logs, "tail" refers to a command or function that displays the last few lines or entries.
Some other commands to explore later:
- passwd
- history
- kill
- sort
- touch
- uniq
- wc
- sleep
Python Basics
- Print "Hello, World!":
- Variables and data types:
- Math operations:
- String manipulation:
- Lists and indexing:
- Conditional statements:
- Loops:
- Functions:
- File handling:
- Error handling:
print("Hello, World!")
name = "John"
age = 25
is_student = True
x = 5
y = 3
sum = x + y
difference = x - y
product = x * y
quotient = x / y
message = "Hello, World!"
print(message.upper())
print(message.lower())
print(len(message))
fruits = ["apple", "banana", "orange"]
print(fruits[0])
print(fruits[1:])
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
for i in range(5):
print(i)
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
file = open("data.txt", "w")
file.write("Hello, World!")
file.close()
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")