Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

Linux Commands

Dr. Amiya Ranjan Panda


Unix
•UNIX is a Operating System which is capable of
handling activities from multiple users at the
same time.
•Unix was originated around in 1969 at AT&T
Bell Labs by Ken Thompson and Dennis Ritchie.
Unix . . .
•Several people can use a UNIX computer at the
same time; hence UNIX is called a multiuser
system.
• A user can also run multiple programs at the
same time; hence UNIX is called
multiprogramming.
•There are various Unix variants available in the
market. Solaris Unix, AIX, HP Unix and BSD are
few examples. Linux is also a flavor of Unix
which is freely available.
Linux
•Linux is one of popular version of UNIX
operating System. It is open source as its source
code is freely available. It is free to use. Linux
was designed considering UNIX compatibility.
It's functionality list is quite similar to that of
UNIX.
•Kernel: The kernel is the heart of the operating
system.

•Shell: Users communicate with the kernel


through a program known as the shell.

•The shell is a command line interpreter; it


translates commands entered by the user
and converts them into a language that is
understood by the kernel.

• C Shell, Bourne Shell and Korn Shell are most


•Commands and Utilities: There are various
command and utilities which you would use in
your day to day activities like cp, mv, cat etc.

•Files and Directories: All data in UNIX is


organized into files. All files are organized into
directories. These directories are organized into
a tree-like structure called the file system.
COMMANDS
Calendar
•To display calendar.

$ cal
pwd
•This command is used to find out the path of
the current working directory (folder) that
you’re in.

$ pwd

Example: /home/username
cd
•To navigate through the Linux files and
directories, use the cd command.

$ cd

There are some shortcuts to help you navigate


quickly:
• cd .. (with two dots) to move one directory up
• cd to go straight to the home folder
• cd- (with a hyphen) to move to your previous
directory
• cd ~ quickly return to your home directory
Listing Directories and Files
• You can use ls command to list out all the files or
directories available in a directory.
• By default, this command will display the contents
of your current working directory.
• ls -R will list all the files in the sub-directories as
well
• ls -a will show the hidden files
• ls -al will list the files and directories with
detailed information like the permissions, size,
owner, etc.
$ ls –l
1. First Column: represents file type and permission given on
the file. Below is the description of all type of files.
2. Second Column: represents the number of memory blocks
taken by the file or directory.
3. Third Column: represents owner of the file. This is the
Unix user who created this file.
4. Fourth Column: represents group of the owner.
5. Fifth Column: represents file size in bytes.
6. Sixth Column: represents date and time when this file was
created or modified last time.
7. Seventh Column: represents file or directory name.
Who Are You?

•While you're logged in to the system, you might


be willing to know : Who am I?
•$ whoami
Who is Logged In?
•Sometime you might be interested to know
who is logged in to the computer at the same
time.
•$ who
w command
•The w command lists the currently logged in
users.
•$ w
FILES
Creating Files
•You can use vi editor to create ordinary files on
any Unix system
•$ vi filename
•You would need to press key i to come into edit
mode. Once you are in edit mode you can start
writing your content in the file.
•Press key esc to come out of edit mode.
•Press two keys Shift + ZZ together to come out
of the file completely.
•You can use following keys to move inside a file:
l key to move to the right side.
h key to move to the left side.
k key to move up side in the file.
j key to move down side in the file.
Display Content of a File
•You can use cat command to see the content of
a file.
$ cat filename
cat
•Creates a new file

$ cat> filename

• Joins two files (1 and 2) and stores the output


of them in a new file (3)

$ cat filename1 filename2>filename3


touch
•The touch command allows you to create a
blank new file through the Linux command line.

$ touch filename

• create multiple blank files

$ touch filename1 filename2 filename3


Counting Words in a File
You can use the wc command to get a count of the
total number of lines, and words contained in a file.

$ wc filename

2 19 103 filename

$
head command
• The head command is used to view the first ten
number of lines of any text file.
• By default, it will show the first ten lines, but you
can change this number to your liking.
• For example, if you only want to show the first
five lines, type

head -n 5 filename.ext.
tail command
• The tail command will display the last ten lines of
a text file.
For example:

tail -n filename.ext.
diff command
• The diff command compares the contents of two
files line by line

diff file1.ext file2.ext


Copying Files
•To make a copy of a file use the cp command.
$ cp source_file destination_file
Renaming Files
•To change the name of a file use the mv
command.
$ mv old_file new_file
Deleting Files
•To delete an existing file use the rm command.
$ rm filename

You can remove multiple files at a tile as follows:

$ rm filename1 filename2 filename3


man command
• To show the manual instruction of any
command

man command_name
history Command
• History command shows all the commands
that you have used in the past for the current
terminal session.
•This can help you refer to the old commands
you have entered and re-used them in your
operations again.

•history
clear Command
• This command clears all the clutter on the
terminal and gives you a clean window to work
on, just like when you launch the terminal.

•clear
echo Command
•The echo command prints (echoes) a string of
text to the terminal window.

•The command below will print the words “A


string of text” on the terminal window.

•echo “A string of text”


exit Command
•The exit command will close a terminal
window, end the execution of a shell script, or
log you out the session.

•exit
grep Command
•The grep command can also search the
contents of files. Here we’re searching for the
word “train” in all text files in the current
directory.

•grep train *.txt


ps Command
•The ps command lists running processes. Using ps
without any options causes it to list the processes
running in the current shell.
•ps

•To see all the processes related to a particular user,


use the -u (user) option.
•ps -u dave

•To see every process that is running, use the -e


(every process) option:
•ps -e
Installing Software
•In Linux/UNIX, installation files are distributed as
packages. But the package contains only the program
itself. Any dependent components will have to be
installed separately which are usually available as
packages themselves.
•You can use the apt commands to install or remove
a package.
•sudo apt-get install packagename
•Let's update all the installed packages in our system
using command -
•sudo apt-get update
shutdown
•The shutdown command lets you shut down or
reboot your Linux system.
•Using shutdown with no parameters will shut down
your computer in one minute.

•shutdown

•To shut down immediately, use the now parameter.

•shutdown now
DIRECTORIES
Home Directory
•The directory in which you find yourself when
you first login is called your home directory.
•You will be doing much of your work in your
home directory and subdirectories that you'll be
creating to organize your files.
•You can go in your home directory anytime
using the following command:
$ cd ~
Here ~ indicates home directory.
Creating Directories
•Directories are created by the following
command:
$mkdir dirname
Removing Directories
•Directories can be deleted using the rmdir
command as follows:
$rmdir dirname
Note: To remove a directory make sure it is
empty which means there should not be any file
or sub-directory inside this directory.

$rm -rf dirname


Changing Directories
•You can use the cd command to change
directory.
$cd dirname
Renaming Directories
•The mv command can also be used to rename a
directory. The syntax is as follows:
$mv olddir newdir
FILE PERMISSION
File ownership is an important component of LINUX
that provides a secure method for storing files.
•Every file in UNIX has the following attributes:
•Owner permissions: The owner's permissions
determine what actions the owner of the file can
perform on the file.
•Group permissions: The group's permissions
determine what actions a user, who is a member of
the group that a file belongs to, can perform on the
file.
•Other (world) permissions: The permissions for
others indicate what action all other users can
perform on the file.
Changing Permissions
•To change file or directory permissions, you use
the chmod (change mode) command. There are
two ways to use chmod:
•symbolic mode
•absolute mode.
Using chmod in Symbolic Mode
•The easiest way for a beginner to modify file or
directory permissions is to use the symbolic mode.
With symbolic permissions you can add, delete, or
specify the permission set you want by using the
operators in the following table.
Example
Using chmod with Absolute
Permissions
•The second way to modify permissions with the
chmod command is to use a number to specify
each set of permissions for the file.
•Each permission is assigned a value
Link (ln) Command
•The ln command is a command line utility
for making links between files.
• There are two types of link:
•Hard Link
•Soft Link
Hard link

• A hard link is a direct link to the data on


disk.
•This means data can be accessed directly
via an original filename or a hard link.
•Both the original file and the hard link are
direct links to the data on disk.
•The use of a hard link allows multiple
filenames to be associated with the same
data on disk.
Soft link

• A symbolic link (also sometimes known as


a soft link) does not link directly to the data
on disk but to another link to the data on
disk.
•On most operating systems folders may
only be linked using a symlink.
•Symbolic links can link across file systems
to link a folder on an external hard drive.
How to create a hard link

• To create a hard link using the ln command


pass the full path of the target file and the
link name. This has the effect of creating a
new file that links to the same data on disk
as the target file.
Example target.txt is linked to via link.txt.
ls
cat target.txt
target file

ln target.txt link.txt

ls
target.txt link.txt

cat link.txt
target file
Editing link.txt has the effect of changing the
underlying data on disk.

Vi link.txt
target file
link edit

cat target.txt
target file
link edit
How to create a symbolic link
To create a symbolic link pass the -s option
to the ln command followed by the target file
and the name of link.

ln -s ~/code/notes/notes ~/bin/notes
What is an INODE

INODE is a data structure that keeps track


of all the information about a file.

You store your information in a file, and the


operating system stores the information
about a file in an inode(sometimes called as
an inode number).
What is an INODE

Information about files(data) are sometimes


called metadata. So you can even say it in
another way, "An inode is metadata of the
data.“
Whenever a user or a program needs
access to a file, the operating system first
searches for the exact and unique inode
(inode number), in a table called as an inode
table.
Structure of an inode Table
Structure of an inode
Following contents are stored in the Inode from a file:

User ID of file
Group ID of file
Device ID
File size
Date of creation
Permission
Owner of the file
File protection flag
Link counter to determine number of hard links
Inode Table

The Inode table contains all the Inodes and is created


when file system is created. The df -i command can
be used to check how many inodes are free and left
unused in the filesystem.
Inode Number

Each Inode has a unique number and Inode number


can be seen with the help of ls -li command.

Look at the above snapshot, Directory Disk1 has the


three files and each file has a different Inode number.
Note: The Inode doesn't contain file content, instead
it has a pointer to that data.

You might also like