Finally, We are headed towards the heart of Linux or for which Linux is known the most "The Linux Command Line".
The Linux Command Line
It is a text-based interactive interface that helps the user to interact with the Linux system directly.
The Linux Command Line is much faster than the GUI in some aspects.
The Command Line contains a terminal through which we use the Command Line and its commands.
The terminal has a command prompt where we can enter commands and the system will act based on our commands.
Using SSH(Secure Shell) we can remotely perform tasks over a network connection.
We can do various tasks using the Linux Command Line such as: Navigating through the File System, Manipulating Files and Directories, Managing user and user permissions, etc.
3 Elements of the Command Line:
The Command line gets divided into 3 elements:
Command
Options
Arguments
To get a clear image of what they mean:
Command - Suppose we want to list the files of a directory along with the hidden files then we use the
ls -a dir1
command (here,dir1
is a user-defined directory name and you can name your directory whatever you like). Here,ls
is the command.Options - Options make the command perform some extra operation or in simple words they alter their behavior, In
ls -a dir1
, We saw thatls
is the command and the-a
is the option provided to modify the command's operation.In this case, It will list all the files including the hidden files as well so,
-a
option made this change inls
command's behavior.Arguments - Arguments may be described as the elements on which the command is being performed. Like, here in the
ls -a dir1
command, thedir1
is the argument. Therefore, the dir1's files including the hidden files will also be listed.(Note): There may be some commands that do not have any options or arguments.
File System
If you look at your cupboard, It's organized into stuff like jeans, shirts, coats, etc. And dresses for college, office, parties, and regulars in different organized sections.
Similar is the case with the Linux File System, We have files and directories organized logically into different key files and directories.
The Linux File System is a method embodiment of storing and organizing the collection of arbitrary data.
The Filesystem resides at the partition of the hard disk/drive.
File System hierarchy
In Linux, there is a hierarchal directory order/structure in which the files are organized and the base of this structure is called Root also called the Root Directory.
From the Root directory, the other directories emerge and branch out into directories and sub-directories. And it forms more of a tree-like structure.
Some of the Key Directories are:
/
: This is the root directory and all the other files/directories in the file system are under this directory.
/bin
: Contains all the command binaries such as ls, mkdir, mv, and cp for user and admin use (Also useful at the time when no other filesystems are mounted).
/boot
: This directory contains the essential files needed for the boot process.
/dev
: This directory contains device files about the devices that are attached to the local system.
/etc
: This directory contains the system configuration files.
/home
: Contains the user's essential files, settings, and directories which were saved by the user and contains all the home directories.
/lib
: This directory contains the shared library files which are used by the system for its proper functioning.
/media
: This directory acts as a mount point for removable media devices such as CDs, USB drives, etc.
/mnt
: This directory acts as a mount point for the remote file system mounted by the System Administrator.
/opt
: This directory helps install optional software packages which are not present in the base system.
/sbin
: This directory contains the system binaries for the system administration.
/srv
: This directory stores data that is served by the system. Like, data for the network services such as HTTP, FTP, etc, and Web Pages.
/tmp
: The temporary files created by the system/applications are stored in this directory.
/usr
: Contains most of the data in comparison to other filesystems. It contains all the documentation, libraries, etc.
/var
: This directory contains the variable data files.
/root
: This directory is the home directory for a root user.
/proc
: This directory contains information about the processes.
Linux, Unlike Windows, does not have a separate file system tree for each storage device, instead, it has a single file system tree.
And, Storage devices when needed to be mounted are mounted at useful points by the maintainer of the system.
For storing important files, Linux has an FHS standard.
The reason or motive behind its creation was to make a compliant standard for the Linux distros. So that users, admins, and Developers don't have to learn again and again the system organization to switch or move between distros.
Linux does not have drive letters and uses the forward slash
/
to separate paths.Linux filesystem names are case-sensitive.
Important Commands Used in the File System
touch :
The touch command is used to create a new file. This command is also used to update the timestamps of an existing file.
Syntax:
touch [options] [new_file.txt]
Options: (It's Optional)
-c
: This option will not create a new file with the same name if it exists already.-a
: This option will only Change the access time.-d
: This option allows us to add a custom date/time instead of the current date/time.-m
: This will change the time at which the file was modified.Argument: (Mandatory)
new_file
: The argumentnew_file
is the name of the file that will be created.
This will create a new file called simply_linux
.
cat :
It's an Abbreviation of "Concatenate". The cat
command is used to combine two files. It is also used to view file contents.
Syntax:
cat [options] [file_name]
Options: (It's Optional)
-n
: This option will give a number to all the output lines.-b
: This option will give a number to only the non-blank lines.-s
: This option will squash multiple blank lines into a single line.-v
: This option will display only those characters that are usually not printed like a tab character which will be printed as ^I.
Arguments: (It's Optional)
file_name
: The argumentfile_name
is the name of the file whose contents the user wants to view.
mkdir:
It's an abbreviation of "Make Directory". The mkdir
command is used to create new directories.
Syntax:
mkdir [dir_name]
Arguments: (It's mandatory)
dir_name
: The argumentdir_name
is the name of the directory which the user wants to create.
This will create a new directory as simply-linux
.
rm:
It's an abbreviation of "Remove". The rm
command is used to remove files and directories.
Syntax:
rm [options] [dir_name or file_name]
Options: (It's Optional)
-i
: This option prompts/warns the user before removing any file/directory.-f
: This option does not prompt/warn the user before removing any file/directory and forcefully deletes the directory and its contents and files.-d
: This option helps delete empty directories.-r
: This command deletes the directories and files recursively meaning that the directories and their subdirectories will be deleted at once.
Arguments: (It's mandatory)
dir_name
: The argumentdir_name
is the name of the directory which the user wants to delete/remove.file_name
: The argumentfile_name
is the name of the file which the user wants to delete/remove.
Here we removed the simply-linux directory.
rmdir:
It's an abbreviation of "Remove Directory". The rmdir command is used to remove empty directories.
Syntax:
rmdir [dir_name]
Arguments: (It's mandatory)
dir_name
: The argumentdir_name
is the name of the empty directory which the user wants to delete/remove.
ls
It is an Abbreviation of "List". The ls
command is used to list names in the directories.
Syntax:
ls [options] [dir_name]
Options: (It's optional)
1. -a
: This option when used lists the files along with hidden files.
2. -A
: This option when used lists everything except .
(working directory) and ..
(parent directory).
3. -r
: This option when used lists the results in reverse order rather than in the normal order.
4. -t
: This option when used lists the results based on the last modified time.
5. -l
: This option when used lists the results in long listings.
6. -d
: This option when used lists only the directory itself or the name of the directory rather than the contents of the directory.
7. -S
: This option when used lists the files based on the sizes and that too in descending order.
8. -h
: This option when used displays the file size in the human-readable format. Like, The file sizes will be displayed in units like, KB, MB, etc.
9. -F
: This option when used appends a symbol at the end of the name of the file/directory indicating its type.
Like, The symbols indicate:
/
- The/
symbol indicates that the type of the file is a directory.*
- The*
symbol indicates that the type of the file is an executable file.
10. -f
: This option when used disables the output sorting and instead, outputs the files/directories in the normal order.
Arguments: (It's optional)
dir_name
: This argument is used to specify the directory whose contents are to be listed.This will list the contents of the directory
dir_new
.
cp:
It is an Abbreviation of "Copy". The copy command is used to copy directories/files.
Syntax:
cp [options] [source_dir] [destination_dir]
Options: (it's Optional)
-i
: Stands for Interactive mode, And prompts the user or warns the user before overwriting an existing file.-f
: Stands for Force copying, And will not warn/prompt the user and will simply overwrite an existing file.-r
: This option Recursively copies directories and their contents.-v
: It is called Verbose mode, And is used to print the name of each file that is copied.
Arguments: (It's mandatory)
source_dir
: This argumentsource_dir
is the source directory that has to be copied.destination_dir
: This argumentdestination_dir
is the destination directory to which the source directory will be copied.
mv
This command is used to move files/directories from one directory to another directory and also used to rename files/directories.
Syntax:
mv [options] [source_dir] [destination_dir]
Options: (It's Optional)
-i
: This option prompts/warns the user before the file is overwritten.-u
: This option will move the file only if the source file is newer than the destination files or in a case when the destination file does not exist.-f
: This option will forcefully overwrite the file without any prompt/warning.-v
: This option will display the name of the file which is/are moved.
Arguments: (Mandatory)
source_dir
: This argument is used to specify the directory which has to be moved.destination_dir
: This argument is used to specify the directory to which the source directory will be moved.
This will move the simply_linux.txt
file to the dir_new
directory.
cd
This command is used to navigate between the directories in Linux File System.
Syntax:
cd [dir_name]
or
cd [options] [dir_name] //when you want to use options
Options: (It's Optional)
-
: This option when used after the cd command will redirect the user to the previous directory...
: This option when used after the cd command will redirect the user from the current directory to the root directory or parent directory.
Arguments: (It's Optional)
dir_name
: This argument is used to specify the destination directory to which the user has to change the current directory to.
This will change the directory to dir_new
.
pushd
pushd followed by the directory path name will push the current working directory onto the directory stack and will change the directory to the mentioned path.
Syntax:
pushd [directory_path]
Options: (It's Optional)
-n
: This option will not change the directory after pushing the directory onto the stack.-q
: This is called the Quiet mode. This command pushes the current directory onto the directory stack and changes the current directory to the directory path mentioned by the user but does not displays the output of the command.
(Note: The dirs
command can be used to view the entire directory stack. And also works well in quiet mode as well).
Type the following command pushd /usr/bin/
, the current directory will be changed to the /usr/bin
and will push the previous directory onto the stack
After this, you can type in ls
to view the contents of the mentioned directory path.
popd
The popd
command can be used to remove directories from the directory stack and change the current working directory back to a previous directory.
or in simple words, It will place the user back in the directory which was most recently stored by the pushd command.
Syntax:
popd [options]
Options: (It's Optional)
-n
: This option when used makespopd
to not change the current working directory. It removes the directory which was on top of the directory stack.-q
: This option is used to put thepopd
command into quiet mode, It will not display any output even after the execution of the command.-v
: This option is used to put thepopd
into verbose mode. This simply means that the command will now be able to display the directory which was removed from the directory stack.
Type the following command popd
, the current directory will be changed to the previous directory by popping it off the stack.
~
describes the home directory.
After this, you can type in ls
to check if you are back in the home directory or not.
tail:
This command is used to view the last n number of lines of a file.
Syntax:
tail -n 10 file.txt
Like, In this example, We had made the command to print/display the last 10 lines of a file which here is file.txt
.
Options: (It's Optional)
-n
: This option is used to display the n number of lines that the user will specify followed by the-n
flag.Like, In the above code, we used 10 just after the
-n
flag which will print the last 10 lines of the file.
Argument: (Mandatory)
file.txt
: This argumentfile.txt
is the file whose last n number of lines will be displayed.
This will print the last 5 lines of the file.
The above-mentioned file sample_file.txt
has a total of 23 lines.
head:
This command is used to view the first n number of lines of a file.
Syntax:
head -n 10 file.txt
Like, In this example, We had made the command to print/display the first 10 lines of a file which here is file.txt
.
Options: (It's Optional)
-n
: This option is used to display the n number of lines that the user will specify followed by the-n
flag.Like, In the above code, we used 10 just after the
-n
flag which will print the first 10 lines of the file.
Argument: (Mandatory)
file.txt
: This argumentfile.txt
is the name of the file whose first n number of lines will be displayed.
This will print the first 5 lines of the file.
The above-mentioned file sample_file.txt
has a total of 23 lines.
tac:
This command is used to display the last line of the file first and the first line of the file at last or to say it more simply, reverses the file contents.
Syntax:
tac [file_name]
Argument: (Mandatory)
file_name
: This argumentfile_name
is the name of the file whose contents will be displayed in reverse order.
less:
This command is used to view the contents of the file but, one frame/screen at a time (here, by "frame" I mean to say the content that can be displayed on one screen at a time)
Syntax:
less [options] [file_name]
Options: (It's Optional)
/pattern
: This option searches for the same word that the user will provide after the/
.-N
: This option displays the total number of lines in the file.
Argument: (Mandatory)
file_name
: This argumentfile_name
is the name of the file whose contents will be displayed.
After this hit Enter
and you'll be able to view the contents of your file:
And, to quit the editor press q
.
Absolute Pathname
An Absolute Pathname always starts with a forward slash
/
It always starts with Root Directory and follows the entire tree, branch by branch till it reaches the destination directory.
(Note): Due to default settings, the prompt displays the current working directory.
Relative Pathnames
Unlike Absolute Pathnames, Relative Pathnames do not start with
/
Relative Pathnames always start with the Present Working Directory.
Relative Pathnames have certain notations for the relative positions such as
.
and..
where.
means the current working directory and..
means the current working directory's parent directory.
(Note): We use Relative Pathnames more often Since it reduces typing.
Locating Applications
We use the which
utility To Locate any application/program on the File System.
Like, The command mentioned below will print the path to the thunderbird executable file.
And, an Alternative to the which
command is whereis
whereis
command looks for the packages/application/program more broadly from the range of system directories.
Here, the whereis
command provides the location of the binary of Firefox along with the man pages and other sources.
Changing the prompt name
In Linux,
PS1
→ is the Primary Prompt String that the user sees before typing the command in the shell. It is an environment variable
that controls the access as well as the format of the prompt displayed on the terminal.
Example:
PS1=" Prompt_name $"
→ is used to change the primary prompt string name to the new user-defined prompt name.
PS1=" Prompt_name $" // any prompt name
Whatever prompt name you'll enter after the =
sign in the " "
will be set as the prompt name.
Example:
Wrap Up
Okay! So that was it and I'll be wrapping up this blog now, Do follow me on Twitter for more insights into my learnings and contributions to the open source communities, and most importantly, let's connect on our socials Here is the link treasure for you hahaha :)
Thank you for giving your time and having the dedication and passion to learn something new.
:q