Introduction to Linux and Terminal Command

Introduction to Linux and Terminal Command

What is the Linux command?

A Linux command is a program or utility that runs on the command line. A command line is an interface that accepts lines of text and processes them into instructions for your computer.

Simply, the Linux command is used to give instructions to our operating system to perform various operations. A terminal emulator (like an application different in different O.S) is a program that let us use the terminal in a graphical environment in which we use the Linux command.

What exactly converts these commands into operations?

The answer is Shell

The shell is the Linux command line interpreter.

It provides an interface between the user and the kernel ( a core component of an operating system and serves as the main interface between the computer's physical hardware and the processes running on it ) and executes programs called commands. For example, if a user enters ls then the shell executes the ls command.


Who cares about shell behavior?

Environment variables

Environment variables are a way to pass data on to applications. we can set values of different variables, which any application can access. various variables decide how the shell will behave. To see all environment variables use the command printenv. To create new one use export. To print/display environment variable value use echo $

Some useful Linux commands are as follows :

  • pwd : print working directory, it prints the name of the current/working directory.

  • ls: List, it lists all the directory contents available in the folder.

  • ls -a: It lists all the hidden files present in the folder.

  • ls -l: It lists files with the author's username with all the additional information like the date and time of the formation of the file and the size of the file.

  • ls -al: it lists all the hidden files with additional information like the author's username, date, time, size, etc.

  • cd: The change directory command is used to change directories and for navigation purposes. where . means current directory and .. means parent directory.

  • cat: a short form of concatenating files and printing on the standard output.

  • > : it basically redirects the output to a file. like if you want multiple files to merge into a single one then you can basically use this command like

cat files.txt names.txt > totat.txt

files.txt and names.txt now merge into the total.txt file.

  • man: man command is used to display the user manual of any command that we can run on the terminal. like man cat gives you the manual for cat command.

  • echo: It simply displays a line of text. like echo hello will give us the output hello.

  • tr: tr is basically called a Translate character. it copies the standard input to the standard output with substitution and deletion of selected characters. like it is used in changing the uppercase into lowercase of the character and vice versa by using like let's say lower.txt contains the word "hello", now in order to change its lowercase into the uppercase we can use tr here and redirect it to upper.txt file.

    cat lower.txt | tr a-z A-Z > upper.txt
  • | : Pipe, by using this the output for the first command becomes input for the second one.

  • mkdir: Make directory command, used to make folders and directories.

    mkdir hello

    we can add the "hi" folder inside the "hello" folder as :

    mkdir hello/hi

    we can add a namaste folder in between hello and hi where hi will be copied in the namaste folder via using the -p flag -p: A flag which enables the command to create parent directories as necessary.

    mkdir -p hello/namaste/hi
  • touch: The touch command is used to create new files. -cp: The copy command is used to copy files and directories.

for example:

cp hello.txt bob.txt

here hello.txt file is copied as a bob.txt file. the data inside hello.txt is copied into the bob.txt file.

cp -R hello hii

here 'hello' folder is now copied into the 'hii' folder by using the -R tag.

  • mv: The move command is used to move and rename files.

    mv names.txt hello

    here existing names.txt file is moved to the hello folder.

    mv names.txt files.txt

    here existing names.txt file is renamed to files.txt.

  • rm: The remove command is used to remove files or directories.

    rm names.txt

    here names.txt file is now removed.

    rm -R hello

    here hello folder is now removed by using the -R tag.

  • sudo: The sudo (superuser do) command allows a permitted user to execute a command as the superuser or another user, as specified by the security policy.

In simple language, if you wanna extract or want some secret files or admin-controlled files then you have to apply a password for the same by using the sudo command.

  • df: Report file system disk space usage. df displays the amount of disk space available on the file system containing each file name argument. we can also check the usage space in GB MB KB by using useful tags as well.

  • du: It estimates file space usage. Summarizes disk usage of the set of FILEs, recursively for directories.

  • head: It outputs the first part of the files. it prints the first 10 lines of each FILE to standard output.

    head names.txt

    here names.txt file contains 12 lines whose first 10 lines are now displayed using head command.

    head -n 4 names.txt

    Now first 4 lines of the names.txt file are displayed by using the -n flag.

  • tail: It outputs the last part of the files. It prints the last 10 lines of each FILE to standard output. we can change this number by using the -n flag.

  • diff: It compares files line by line. it will compare the content of both the files line by line and output the lines that will not match.

    diff names.txt surnames.txt

    Here the comparison is going to be done line by line between the names.txt and surnames.txt files.

  • locate: it finds files by name.

    locate "*.txt"

    Here all files ended with .txt going to display. * means anything can come under the starting of the file name.

  • find: it searches for files in a directory hierarchy.

find .

It says find in the current directory.

find ..

It says find in the parent/previous directory.

find *folder*

It says find in a particular directory or folder.

find . -type d

It says find in the current directory and the type should be a folder.

find . -type f

It says find in the current directory and the type should be a file.

find . -type f -name "*.txt"

It says find in the current directory and it should be all text files.

find . -type f -mmin +minutes/-minutes

It says find in the current directory and the type should be a file according to the last modified less/more than minutes ago.

find . -type f -mtime +days/-days

It says find in the current directory and the type should be a file according to the last modified less/more than days ago.

find . -type f -maxdepth 1

It says find in the current directory but not in the sub-directory.

find . -size +1k/1M/1G

It says find in the current directory with a size more/less than 1KB or 1MB or 1GB.

find . -empty

It says find in the current directory with empty files.

  • -exec tag: In order to perform an action on multiple files, this -exec tag is used. for removing multiple files from the directory or wanna rename all files from the directory.

    find . -type f -name "*.txt" -exec rm -rf {} +

    It says find in current directory type should be text files of all names and delete them all. for removing the single file, we use rm -rf file name but for all files, we need to add a placeholder {} (as we are getting names of files from find command) and a '+' sign.

  • grep: It stands for "Global regular expression print". grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.

There are some criteria for that while applying the grep command.

  1. when we know the file names and search the string or word in it.


grep "word" files.txt

it says searching for any word in the 'files.txt' file.

grep -w "hash" files.txt

The -w flag is used to search for the full word. the full word might be "hash node".

grep -i "word" files.txt

The -i flag is used to ignore the case-sensitive stuff while searching the word.

grep -n "word" files.txt

The -n flag is used to show the line number of the respective word.

grep -win "word" files.txt

The -win flag is used to combine all the above three flags -w -i -n.

grep -B (any number) "word" files.txt

The -B flag is used to search all the words (according to the number you give) that lie before the "word" you are searching for.

  1. When we don't know the file name in which the word lies.


grep -win "word" ./*.txt

it says search for a word "word" in the current (.) directory and in all text files available.

grep -rwin "word" .

-rwin flag is used to search for words recursive in current or parent directories. it means when the word lies on the other folder.

  1. When we wanna check how many files contain the matches.
grep -wirl "word" .

Here -wirl flag is used to find all the files that contain the "word" matches in the current or parent directory.

grep -wric "word" .

Here -wric flag is used to count how many files are available that contain the match in the current or previous directory.

  • history: The history command contains the History Library. Simply it keeps the track of all commands used during the process and stuff.

we can pipe (pipe = |) the history command as well. piping means the output of the first command acts as the input for the first command.

history | grep "ls"

It means now the grep command will search for the ls command used during the process by taking the output track of all commands used during the process.

  • regex: Regular expressions are the special characters or sets of characters that help us to search for data and match the complex pattern. The most commonly used Linux commands are:- grep, tr etc.
  • alias: The alias instructs the shell to replace a single big command with a shortcut command which has the same functionality as the big command.

  • File permissions: There are three types of file permissions read (r), write (w), and execute (e) for three types of people user (u), group (g), and others (o).

there are two ways to change file permissions:

  1. by changing file mode (using a command called chmod) using numbers including read:4, write:2, execute:1.

for example

chmod 777 files.txt

It says changing file mode by 777, means read-write execute all given to all user, group, and others.

  1. by using characters like
chmod u=rwx, g=rx, u=r files.txt

it means changing the file mode for users to read, write, and execute. for the group to read and execute and for others to read only.

We can change the owner by using

sudo chown root files.txt

here for file files.txt owner changed to root the super owner.

finding the files having permissions by using a command called

find . -perm 777or for any number

It says finding files having the permission to read, write, execute for user, group, and others.

I believe this would definitely help to get started with the Linux environment.

Check out this video "introduction to Linux and terminal commands" by Kunal Kushwaha for in-depth explanations with hands-on learning.

Keep learning and contributing

See you in the next one

Thank you