Linux Command Line Basics

Before anything else, open up your favorite text editor and create a new file called “my_script.sh”. This is where all the action will happen! Now, copy and paste these lines of code into that file (don’t worry if they look like gibberish to you trust me, it’s worth it):

#!/bin/bash
# This is a bash script, indicated by the shebang at the top of the file

echo "Welcome to my Linux script!" # Prints a welcome message to the terminal

sleep 5 # Waits for 5 seconds before continuing to the next command

clear # Clears the terminal screen

ls # Lists all files in the current directory

exit # Exits the script and returns to the shell prompt

Save your file, close it, and then open up a new terminal window. Navigate to the directory where you saved “my_script.sh” (let’s call this directory “/home/user/scripts”) using the following command:

#!/bin/bash # This line specifies the interpreter to be used for executing the script

cd /home/user/scripts # This line changes the current working directory to "/home/user/scripts"

# The following lines create a new file named "my_script.sh" and add some content to it
touch my_script.sh # Creates a new file named "my_script.sh"
echo "Hello World!" > my_script.sh # Adds the text "Hello World!" to the file "my_script.sh"

chmod +x my_script.sh # This line makes the file "my_script.sh" executable

./my_script.sh # This line executes the file "my_script.sh"

# The following lines remove the file "my_script.sh" and print a message to the terminal
rm my_script.sh # Removes the file "my_script.sh"
echo "Script executed successfully!" # Prints the message "Script executed successfully!" to the terminal

Now, make your script executable by running:

#!/bin/bash

# This is a bash script that will print a greeting message to the user.

echo "Hello, user!" # This line prints the greeting message to the user.

# This is a variable that stores the current date and time.
current_date=$(date)

# This line prints the current date and time to the user.
echo "The current date and time is: $current_date"

# This is a variable that stores the current user's username.
username=$(whoami)

# This line prints the current user's username to the user.
echo "Your username is: $username"

# This is a variable that stores the current working directory.
current_dir=$(pwd)

# This line prints the current working directory to the user.
echo "You are currently in the directory: $current_dir"

# This is a variable that stores the number of files in the current directory.
num_files=$(ls | wc -l)

# This line prints the number of files in the current directory to the user.
echo "There are $num_files files in the current directory."

This will give you permission to run it as a program (otherwise, Linux won’t know what to do with it). Finally, execute the script using this command:

#!/bin/bash # This is the shebang line, indicating that the script should be executed using bash

chmod +x my_script.sh # This command gives the script executable permission, allowing it to be run as a program

./my_script.sh # This command executes the script, running the code within it

You should see your message “Welcome to my Linux script!” followed by a 5-second delay (thanks to the sleep function), and then all of the files in that directory will be listed on screen. Pretty cool, right?

But wait there’s more! Let me introduce you to some other essential commands:

#!/bin/bash

# This script lists all directories in the current folder
ls -d */ 

# This command changes the current directory to a specific path
cd /home/user/documents 

# This command creates a new file called "my_new_file.txt"
touch my_new_file.txt 

# This command deletes an existing file called "old_file.txt"
rm old_file.txt 

# This command moves a file from one location to another
mv /home/user/documents/old_file.txt /home/user/archive

These are just the tip of the iceberg there’s so much more you can do with Linux! But for now, let’s wrap up this article and leave you with a final piece of advice: don’t be afraid to experiment and try new things. The command line is your oyster (or at least, it should be).

SICORPS