Basic Linux & Terminal Commands

Basic Linux & Terminal Commands

This is an introduction to Linux & Terminal commands to get you started with various command-line tools with a hands-on approach.

What will we be doing in this tutorial?

In this tutorial, we'll be looking at an introduction to Linux and see how these commands work in the terminal and we'll be focusing on some basic and most used terminal commands.

This will be more about practical stuff - application-based and less on theoretical knowledge or any deep dive into it. We'll be learning the basics:

  • Introduction to Linux
  • What are terminals
  • How to use terminals ( Extremely Important )
  • Working with like the modifications of terminals and stuff and like writing your terminal programs and things like that

Introduction to Linux

Linux is a family of free and open-source operating systems based on the Linux kernel. The Linux kernel has been under active development since 1991 and has proven to be extremely versatile and adaptable. However, newcomers to Linux may find it somewhat difficult to approach, as Linux filesystems have a different structure than those found on Windows or macOS. Additionally, Linux-based operating systems depend heavily on working with the command-line interface, while most personal computers rely on graphical interfaces.
In Linux, the software updates are much faster than other operating systems. And it really helps developers using Linux.

image.png The terms “terminal,” “shell,” and “command-line interface” are often used interchangeably, but there are subtle differences between them. We will cover these in the upcoming sections!

Well, before going onto the terminal, let me make it clear that they are called terminal emulators -the thing which you call terminal

Terminal & Terminal Emulator

A Terminal is a command-line or console, which allows us to accomplish and automate tasks on a computer without the use of a graphical user interface. and a Terminal Emulator is the GUI of a terminal or in other words - A Terminal Emulator is a program that will let us graphically use the terminal.

Use? - It will let you be able to control your operating system just by using some commands. This is done by using Shell - so now what is Shell??

Shell

The shell helps in interpretitiing the commands that I give in the terminal emulator
A Shell is a command-line interface that will interpret our commands and tell the operating system what to do- eg: bombshell, seashell, zsh, bash, etc. In Linux systems, the shell presented in a terminal is a command-line interpreter.

Another way in which we can define terminals is - a terminal is an input and output environment that presents a text-only window running a shell.

A command-line interface is a user interface (managed by a command line interpreter program) that processes commands to a computer program and outputs the results. and the commands which we're gonna type are nothing but a readable file that your system will run when asked by the shell. For eg:

image.png In this example, I'm trying to run python3 in my terminal - which as you can see is currently running right. But did you ever wonder how is this happening?? How is my system doing this?
Well, this is not a difficult task- This is just your system running a file named python3 which is located somewhere in your system. let us try to know the location of our python3 file and try to run it specifying the path in our terminal without using the python3 command

  • Know the file path -we know that python is listed somewhere in the system so I can say Where python3 image.png
  • now let's execute the file again, but this time by using its path image.png

Voila! - the same result -look, I said
so you can see if I write the whole path or if I just write python3 it will run the same program. Internally writing the whole path is doing the same thing as writing the python3 command.

Environment Variables

But now the question is how does my terminal know that python3 is stored in some file in my computer. why is the computer not asking us to provide the entire path? -This is because of something that is known as an environment variable.
So it is the environment variable that you need to set, basically, these are just named values that are used to change how you know commands and like processes ( what is a process? - any instance of a running command will be known as a process), and everything else is executed in your terminal-emulator.

image.png ( Workflow of command when Entered )- so that was some basics of it I guess we are clear till here and you've got a really nice insight of what a terminal, command-line interface, and a shell is. Now we'll look at the more practical stuff - how you work with the terminal and some of the important commands

Commom Terminal Commands

pwd

pwd stands for Print Working Directory. As the name states, command pwd prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from the root (/).

image.png

ls

ls stands for List. The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File Explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

image.png

ls -a

This command is used to list all the files, even the hidden files are listed with thels -a command. Note- dot is shown for the current directory and double dot for the previous directory.

Files starting with a dot are always hidden files

image.png

ls -l

This command shows more information about the files in the directory we are currently like its Users, Grop Owner, Size, Date Information, Access type, etc.

image.png ( we'll talk more about each of the terms later on)

ls -al

This command is just a combination of both ls -a and ls -l, which means this command shows hidden files and normal files with more information.

image.png

cd

cd stands for Change Directory. It is used to change current working directory.

image.png ( As you can see, I'm in the videos folder now)

cd .

This won't do anything- this is the same directory, we are currently in

cd ..

This command is used to move to the parent directory of the current directory, or the directory one level up from the current directory. “..” represents the parent directory.

image.png

ls -R

It will display the content of the sub-directories also.

cat

cat stands for concatenate. One of its most commonly known usages is to print the content of a file onto the standard output stream. Other than that, the cat command also allows us to write some texts into a file.

So I've created a file in my folder for this one, a file named file.txt, and written some stuff in it

image.png

as you can see I'm able to view the file content in my terminal itself - by using the cat command.

cat > basic.txt

This command creates a file named basic.txt and in the very next line, you'll be prompted to write something in it. ( '>' is known as redirection - it will redirect the file )

image.png

I'm getting a prompt in the very next line to give some input

image.png

I've added some content in it and the edit prompt can be exited by 'Ctrl + C'

cat file1.txt file2.txt > file3.txt

Creates a file3.txt which will contain content of both file1.txt and file2.txt

image.png

As you can see I've file1 and file2.txt created with some content in it and let's concatenate them and make a new file that contains elements of both

image.png

As you can see the file3 contains both file1 and file2 content

man & --help

man command in Linux is used to display the user manual of any command that we can run on the terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORSand SEE ALSO.

For Eg: let's look for man ls

image.png

image.png

mkdir & rmdir

Use the mkdir command when you need to create a folder or a directory. For example, if you want to make a directory called “DIY”, then you can type “mkdir DIY”. Remember, as told before, if you want to create a directory named “DIY Hacking”, then you can type “mkdir DIY\ Hacking”. Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm.

image.png

As you can see, I just added a new folder in my folder list using the mkdir command

image.png

You can see that I just deleted the folder which I made just above using rmdir.

rm

Use the rm command to delete a file. Use "rm -r" to delete a directory and the files inside it.

image.png

Here we deleted a file named file1.txt using the rmcommand - but we are not able to delete a folder, which I tried here

image.png

Here, I deleted the Videos folder using rm -r command - which I wasn't able to do while using rm

touch

The touch command is used to create a file. It can be anything, from an empty text file to an empty zip file. For example

image.png

( I'll be updating this in a few hours, Thank you for reading till here! I hope you learned something from this tutorial, do let me know in the comment section. )