DEV Community

lou
lou

Posted on

Command Line Fundamentals: A Quick Reminder

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:

prompt

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
Enter fullscreen mode Exit fullscreen mode

It moves you to your home directory.

If you want to move to a directory inside your home directory:

cd your_directory
Enter fullscreen mode Exit fullscreen mode

If you have another directory inside it, you can go deeper:

cd child_directory
Enter fullscreen mode Exit fullscreen mode

Now letโ€™s say you want to go back, but not to home, just one level:

cd ..
Enter fullscreen mode Exit fullscreen mode

If you want to go back two levels:

cd ../..
Enter fullscreen mode Exit fullscreen mode

cd command

If you want to see the current directory you are in (the path), you run:

pwd
Enter fullscreen mode Exit fullscreen mode

Creating Directories and Files

mkdir creates a new directory.
(In GUI you right click and choose "Create new folder".)

mkdir myfolder
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To list the files and directories you just created, you can run:

ls
Enter fullscreen mode Exit fullscreen mode

It also takes options:

ls -l   # shows file permissions
ls -lh  # shows file sizes
ls -a   # shows hidden files
Enter fullscreen mode Exit fullscreen mode

Variables and Echo

We can create variables like this:

dummyvar="my dummy variable"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To save:

Ctrl + O
Enter fullscreen mode Exit fullscreen mode

To exit:

Ctrl + X
Enter fullscreen mode Exit fullscreen mode

To make your script file executable, you need to give it execution permission using chmod

chmod +x myscript.sh
Enter fullscreen mode Exit fullscreen mode

Otherwise you will get a permission denied error:

chmod


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
Enter fullscreen mode Exit fullscreen mode

Cron takes 5 fields:

Minute  Hour  Day-of-month  Month  Day-of-week
Enter fullscreen mode Exit fullscreen mode

Example:

5 14 * * 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add this inside:

echo "salam! this is my dummy script!"
Enter fullscreen mode Exit fullscreen mode

Save and exit.

Make it executable:

chmod +x myscript.sh
Enter fullscreen mode Exit fullscreen mode

Run the script:

./myscript.sh
Enter fullscreen mode Exit fullscreen mode

You should see:

salam! this is my dummy script!
Enter fullscreen mode Exit fullscreen mode

shell 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
Enter fullscreen mode Exit fullscreen mode

This will display the content of the file directly in the terminal.

If your script contains:

echo "salam! this is my dummy script!"
Enter fullscreen mode Exit fullscreen mode

Running:

cat myscript.sh
Enter fullscreen mode Exit fullscreen mode

Will print:

echo "salam! this is my dummy script!"
Enter fullscreen mode Exit fullscreen mode

cat command


If you want to see only the first 10 lines of the file, use:

head myscript.sh
Enter fullscreen mode Exit fullscreen mode

If you want to see only the last 10 lines, use:

tail myscript.sh
Enter fullscreen mode Exit fullscreen mode

You can also specify how many lines you want.

For example, to show only the first line:

head -n 1 myscript.sh
Enter fullscreen mode Exit fullscreen mode

And to show the last 3 lines:

tail -n 3 myscript.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This moves myscript.sh into the directory myfolder.

If you run:

ls myfolder
Enter fullscreen mode Exit fullscreen mode

You should see:

myscript.sh
Enter fullscreen mode Exit fullscreen mode

Renaming a File

The mv command can also be used to rename a file.

For example:

mv myscript.sh 

Enter fullscreen mode Exit fullscreen mode

This does not move the file, it just renames it.

The syntax is always:

mv source destination
Enter fullscreen mode Exit fullscreen mode

If the destination is a directory, it moves the file.
If the destination is a new name, it renames the file.

mv command


Removing a File

To remove a file, we use the rm command.

Example:

rm newscript.sh
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Important:

The rm command deletes files permanently.
There is no recycle bin in the terminal so please be careful while playing with it.

rm command


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
Enter fullscreen mode Exit fullscreen mode

Content:

apple
banana
orange
apple
grape
Apple
apple
APPLE
Enter fullscreen mode Exit fullscreen mode

Filter by Word

If we want to filter only the lines that contain apple:

grep apple data.txt
Enter fullscreen mode Exit fullscreen mode

Output:

apple
apple
apple
Enter fullscreen mode Exit fullscreen mode

grep scans the file and prints only the matching lines.


Case Insensitive Filter

You can ignore case with -i:

grep -i apple data.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now apples.txt contains only the filtered lines.


Filter Using Pipe

You can combine commands:

cat data.txt | grep apple
Enter fullscreen mode Exit fullscreen mode

The pipe | sends the output of the first command to the second command.

filtering

Top comments (0)