Reminder of Some Command Lines
The terminal is the application that you run that allows you to give commands to your computer.
The prompt is the text that you see right before the command you write in your terminal. You can customize it in your current session in zsh like this:
The tilde ~ means your home directory.
A command line is the text you write in your terminal after the prompt to give your computer a command.
GUI stands for Graphical User Interface. You can interact with it using your inputs (mouse, keyboard, etc.).
Navigation
cd
Every time you run:
cd
It moves you to your home directory.
If you want to move to a directory inside your home directory:
cd your_directory
If you have another directory inside it, you can go deeper:
cd child_directory
Now letโs say you want to go back, but not to home, just one level:
cd ..
If you want to go back two levels:
cd ../..
If you want to see the current directory you are in (the path), you run:
pwd
Creating Directories and Files
mkdir creates a new directory.
(In GUI you right click and choose "Create new folder".)
mkdir myfolder
To create a new file, you use the touch command.
It creates a new empty file in the current directory you are in.
So if you are in the home directory, the file will be created there.
touch myfile.txt
To list the files and directories you just created, you can run:
ls
It also takes options:
ls -l # shows file permissions
ls -lh # shows file sizes
ls -a # shows hidden files
Variables and Echo
We can create variables like this:
dummyvar="my dummy variable"
echo is like the console log of the terminal.
It prints whatever you pass to it, whether it is a variable or a string.
echo $dummyvar
Shell Scripts (.sh)
If you want to run a list of commands, you can leverage shell scripts in .sh files.
The system will execute the commands one after another.
You can read and edit the content of a file using the nano editor:
nano myscript.sh
To save:
Ctrl + O
To exit:
Ctrl + X
To make your script file executable, you need to give it execution permission using chmod
chmod +x myscript.sh
Otherwise you will get a permission denied error:
Scheduling with Cron
We can schedule scripts to run at a specific time using cron.
You define when you want the script to run (for example, every day at 2 PM), and cron will execute it automatically.
Open the cron editor:
crontab -e
Cron takes 5 fields:
Minute Hour Day-of-month Month Day-of-week
Example:
5 14 * * 1
This means:
Every Monday at 2:05 PM, run the script.
Quick Exercise
Create a script file named myscript.
touch myscript.sh
nano myscript.sh
Add this inside:
echo "salam! this is my dummy script!"
Save and exit.
Make it executable:
chmod +x myscript.sh
Run the script:
./myscript.sh
You should see:
salam! this is my dummy script!
Reading the Content of a File
To read the content of our script file (or any other file), we use the cat command.
Example:
cat myscript.sh
This will display the content of the file directly in the terminal.
If your script contains:
echo "salam! this is my dummy script!"
Running:
cat myscript.sh
Will print:
echo "salam! this is my dummy script!"
If you want to see only the first 10 lines of the file, use:
head myscript.sh
If you want to see only the last 10 lines, use:
tail myscript.sh
You can also specify how many lines you want.
For example, to show only the first line:
head -n 1 myscript.sh
And to show the last 3 lines:
tail -n 3 myscript.sh
Moving and Renaming Files
Now letโs move our executable file into the directory myfolder that we created earlier with mkdir.
To move the file, we use the mv command:
mv myscript.sh myfolder
This moves myscript.sh into the directory myfolder.
If you run:
ls myfolder
You should see:
myscript.sh
Renaming a File
The mv command can also be used to rename a file.
For example:
mv myscript.sh
This does not move the file, it just renames it.
The syntax is always:
mv source destination
If the destination is a directory, it moves the file.
If the destination is a new name, it renames the file.
Removing a File
To remove a file, we use the rm command.
Example:
rm newscript.sh
This will delete the file newscript.sh.
If the file exists, it will be removed permanently.
If you want confirmation before deleting the file, use:
rm -i newscript.sh
Important:
The rm command deletes files permanently.
There is no recycle bin in the terminal so please be careful while playing with it.
Filtering Data
To selecting only the lines that match a condition, the most common command for filtering text is grep.
Example file:
cat data.txt
Content:
apple
banana
orange
apple
grape
Apple
apple
APPLE
Filter by Word
If we want to filter only the lines that contain apple:
grep apple data.txt
Output:
apple
apple
apple
grep scans the file and prints only the matching lines.
Case Insensitive Filter
You can ignore case with -i:
grep -i apple data.txt
This will match all of them.
Filter and Save to Another File
You can also filter and save the result:
grep apple data.txt > apples.txt
Now apples.txt contains only the filtered lines.
Filter Using Pipe
You can combine commands:
cat data.txt | grep apple
The pipe | sends the output of the first command to the second command.







Top comments (0)