0% found this document useful (0 votes)
12 views

Linux Unit 2 notes

This document provides an overview of basic LINUX commands, including internal and external commands, navigating the file system, and using general purpose utilities. It explains the structure of commands, the use of arguments and options, and how to access online help through the 'man' command and '--help' option. Additionally, it covers specific commands like 'cal', 'date', 'who', 'tty', 'uname', 'passwd', and 'echo', detailing their functionalities and usage.

Uploaded by

rekhadhone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Linux Unit 2 notes

This document provides an overview of basic LINUX commands, including internal and external commands, navigating the file system, and using general purpose utilities. It explains the structure of commands, the use of arguments and options, and how to access online help through the 'man' command and '--help' option. Additionally, it covers specific commands like 'cal', 'date', 'who', 'tty', 'uname', 'passwd', and 'echo', detailing their functionalities and usage.

Uploaded by

rekhadhone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit 2: LINUX Commands I

2.1 Locating Commands, Internal & External Commands, Arguments, Options &
Filenames, Online help
2.2 General Purpose Utilities
cal, date, who, tty, uname, passwd, echo, tput, bc, script
2.3 Navigating the File System Concepts- Files, Directories, Paths, Home
Directory, Parent- Child Relationships.
Commands- pwd, cd, mkdir, rmdir,ls

Locating commands:
• The system was designed to see that a minimum number of keystrokes
achieve the maximum amount of work.
• That’s why UNIX commands are seldom more than four character long.
• All UNIX commands are single words like ls, cat, who etc. These
commands are all in lowercase, and you must start shedding your Dos habits
of being indifferent to case.
• For Instance, if you enter LS instead of ls, this is how the system will
respond:
$ LS
LS: not found
• There is obviously no command named LS in the UNIX system. This seems
to suggest that there's a predetermined list of such commands that the
system first searches before it flashes the message above.
• These commands are essentially files containing some programs, mainly
written in C.
• These files are stored in certain directories. For instance, the ls command is
a file (or program) to be found in the directory /bin.
$type ls
Ls is /bin/ls
• When you execute the ls command, the system fetches this from the /bin
directory and executes it.

1 | UNIT 2: LINUX Commands I


The PATH

• The sequence of directories that the system will search to look for a command
is specified in its own PATH variable.
• If you evaluate the value of this variable, you will find a directory list
separated by colons:
$ echo $PATH
/bin: /usr /bin: /home/local/bin:/usr/bin/Xll: /oracle/bin: .
• There are six directories in this list, and when you issue a command, the
system will search this list in the sequence specified to locate and execute it.
Note that this list also includes the current directory indicated by a singular
dot at the end.
• Note: The essential UNIX commands for general use are located in the
directories /bin and usr/bin.in SCO UnixWare, they are all in /usr/bin.

ARGUMENTS, OPTIONS AND FILENAMES


• You have already entered commands that appeared to have multiple words
(like who am I, cat README).
• The first word (viz., who and cat) is actually the command; the addition
words are called arguments.
• That is to say “am” and “i” are arguments of the who command. Because a
command often accepts several types of arguments, it can be made to behave
in numerous ways.

• Commands and arguments have to be separated by spaces or tabs to enable


the system to interpret them as words.
• You can use any number of them to separate the words that forms a
command. A contiguous string of spaces and tabs together form what is
knows as white space.
• Where the system permits the use of one white space character to separate
words, it generally permits several, as in this command:
cat README
• The UNIX system (rather, the shell) possesses a special mechanism of
compressing these multiple consecutive spaces Or tabs into a single space.
• UNIX arguments range from single to the exceedingly complex.
• They consist of options, expressions, instructions, filenames, etc.
• The vast majority of commands have simple arguments.
• Note: White space refers to a contiguous set of spaces, tabs and the newline
characters. The tab character is generated by hitting the tab key on your
keyboard, or using in <Ctrl+i> in case there's none.
2 | UNIT 2: LINUX Commands I
Options
• There is a special type of argument that is mostly used with a - sign, For
instance, when you use
ls -l note
• -l is an argument to ls by definition, but more importantly it's a special
argument that is as an option.
• An option is normally preceded by minus sign(-) to distinguish it from
filenames. There must not be any white space between - and l.
• If you provide that, many commands could land you in a great deal of
trouble!
• While most UNIX options use a single letter with the - (e.g. -1), Linux also
offers options using two hyphens and multi-character word.
• For instance, it offers the synonym ls –all in addition to ls -a.
• Options are also arguments, but given a special name because their list is
predetermined.
• When we used banner larry wall, "larry" and "wall" were also arguments,
but they were not predetermined. In fact, you can use millions of words
legitimately with banner and they will still remain arguments and not
options.
• If you use a command with a wrong option, the shell locates the command
alright, but the command this time finds the option to be wrong:
$ ls -z note
ls: ERROR: Illegal option --z
usage: ls -IACFLRabcdfgi1mnopqrstux -W[sv][files]
• The above message has been generated by the command, and not by the shell.
• ls does have a large number of options, but it seems that -z is not one of them.
Many of the commands also provide the right syntax and options to use when
you use them wrongly.
Filename Arguments
• Many UNIX commands use a filename as an argument so that the
command can take input from file, if at all a command uses a filename, it
will always be its last argument, and certainly after the option.
• It is also a general feature of UNIX that some commands work with more
than one filename:
Is -lat chap01 chap02 chap03
cp chap01 chap02 progs
rm chap01 chap02
• The command with its arguments is entered in one line that is referred
to as the command line.
3 | UNIT 2: LINUX Commands I
• This line can be considered complete only after the user has hit the
<Enter> key. The complete line is then fed to the shell as its input for
interpretation and execution.

Exceptions
• There are, of course, exceptions to the general syntax of commands that has
been mentioned above.
• There are some commands (clear) that don't accept any arguments, and
some (who) that may or may not be specified with arguments.
• The ls command can also be executed without any arguments (ls), by
using only options (ls -l), with only filenames (ls chap01 chap02), or
using a combination of both (ls -la chap01 chap02).
• The word option turns out to be a misnomer in some instances; some
commands compulsorily have to use one (cut).

ON-LINE HELP
• All said and done, the syntax of some UNIX command can still be extremely
confusing, even to the expert.
• You may not remember all the options, or may even be using the wrong
command to do the job.
• This is all the more likely because the UNIX command family is quite large.
In fact, there are nearly 600 commands in /bin and user/bin alone.

➢ man: Browsing the Manual Pages


UNIX offers an on-line help facility with the man command.
For example, for help on the command we, you simply have to enter
Man wc
The entire man page of the UNIX manual pertaining to the wc command is
dumped on the screen. man presents the output one page at a time. Press either
the <Enter> key or the spacebar to view the next page.
This facility is indeed quite helpful as the user can obtain the correct syntax of
the command, and have a fair idea about its objectives as well. The limitations of
the command are quite often specified explicitly, an indication of the honesty
and integrity of the designers.
Note : If output of a command is larger that one page, it can be redirected to more
or less command for slower display as shown below :
$man ls | less

4 | UNIT 2: LINUX Commands I


➢ The - -help Option (Linux)
Some linux commands don’t have man pages, but almost all of them offer --help
option that display a compact listing of all the options. You can spot the find option
you are looking for by using this:
$find - -help

• A Linux command offers far more options than its UNIX counterparts, which
means that a part of the output would scroll off the screen.
• This facility is quite useful for the user who knows the usage of the options,
but doesn’t remember the exact one.
• One can avoid browsing the manual pages by using this simple lookup
facility.

Internal & External Commands


Unix supports huge set of commands. Unix commands are very powerful.
Two categories of commands
1. Internal commands
• Set of built-in commands,
• No separate file exits for these commands e.g., echo
2. External commands
• Separate executable file exists for each of these commands e.g., ls
• General form/Syntax of Unix command -
command [options] [argument]

5 | UNIT 2: LINUX Commands I


• Options and arguments help the user to build more specific commands

File names:

• UNIX allows filenames upto 255 characters in length


• Filenames can contain any ASCII character except /
• Control characters are also allowed
• eg –list, nos., @name, .abc

2.2 General Purpose Utilities


➢ cal
It displays calendar of current month if no argument is specified. Displays
calendar of specific year if specified.

Option Function
-1 Display single month output
-3 Display prev/current/next month output.
-s Display Sunday as first day of week.
-m Display Monday as first day of week.
-j Display julian dates
-y Display a calendar for current year.
• Julian dates (abbreviated JD) are simply a continuous count of days and
fractions since noon Universal Time on January 1, 4713 BCE (on the Julian
calendar).
• Almost 2.5 million days have transpired since this date.
• Julian dates are widely used as time variables within astronomical software.
• Typically, a 64-bit floating point (double precision) variable can represent an
epoch expressed as a Julian date to about 1 millisecond precision.
• Note that the time scale that is the basis for Julian dates is Universal Time,
and that 0h UT corresponds to a Julian date fraction of 0.5.

➢ date
Following command displays current date and time if no argument is
given.
$date
Tue May 6 02:45:40 IST 2003
Note : The command can also be used with format specifiers. The format
specifier is in +%ch format, where ch can be a valid character like d, h, m, y,

6 | UNIT 2: LINUX Commands I


S, M, H for day of month, month name, month number, last two digits of the
year, seconds, minutes and hours respectively.

Sr.no. character meaning

1 d day of month

2 h month name

3 m month number

4 y last two digits of the year

5 S Seconds

6 M minutes

7 H hours

To use multiple format specifiers, enclose them within quotes and use +
symbol before it.
To print only month, using the format +%m
$ date +%m
05
To print Month name
$ date +%h
May
Combining both of them in one command
$ date +”%h %y”
May 03

➢ who: Login details

The following command displays the information about all users who are
currently logged in.
$who
dinesh tty02 Feb 2 10:30
pramod tty03 Feb 2 9:40

7 | UNIT 2: LINUX Commands I


anita tty01 Feb 2 9:00

➢ who am I

The following command displays the information about the currently


logged in user.
$who am I
pramod tty03 Feb 2 9:40

➢ tty: KNOWING YOUR TERMINAL


• Since UNIX treats even terminals as files, it is reasonable to expect a
command that tells you the filename of the terminal you are using.
• It is the tty (teletype) command.
• The command is simple and needs no arguments:
$ tty
/dev/tty01
• The terminal filename is tty01 resident in the /dev directory.
• If kumar logs in from another terminal next time, his terminal device name will
be different.

➢ uname: KNOW YOUR MACHINE'S NAME


• If your machine is connected to a network, then it must have a name.
• If your network is connected to the Internet, then this name is required to
frame your machine's domain name (19.1).
• The uname command with the -n option tells you the machine name in a
network:
$uname -n
Jack

• Many UNIX networking utilities use the machine's name as an argument.


• In order to copy files from some machine, one has to know that
machine's name.
• Note that the same command, when used with -r, shows the operating
system's version number (3.4.4).
uname –n : displays the machine name
uname –r : displays the version of OS

➢ passwd: CHANGE YOUR PASSWORD

8 | UNIT 2: LINUX Commands I


• If you really care about protecting your files, then make sure that no one else is
able to use your account without your consent.
• If your account still doesn't have a password, or has one that is already known to
others, you should change it immediately.
• passwd command is used to change passwords. The behaviour of this command
is heavily System-dependent.
• When used without arguments, it sets the user's own password

This command changes the old password of the current user to the newer one.
[pramod@localhostparam]$passwd_
Changing password for user pramod.
Changing password for pramod
(current) UNIX password:
New password :
Retype new password :
passwd: all authentications tokens updated successfully.
[pramod@localhostpramod] $_

• passwd, when invoked by an ordinary user, asks for the old password, after
which it demands the new password twice.
• What you enter here isn't displayed on the screen for obvious reasons; you
won't like anyone to see what you have entered.
• If everything goes smoothly, the new password is registered by the system,
and the $ prompt is returned. Henceforth, you are expected to use this new
password till you change it again.
• The system doesn't let you choose the old password, nor can the password be
wholly alphabetic or wholly numeric.
• The system checks for at least two alphabetic and one numeric special
character before it treats any string as suitable.
• In any case, make sure the password is most unmeaningful so that others can't
guess it.
• When you enter a password, the string is encrypted by the s system, and the
encryption is stored in the file /etc/shadow (i.e. a file named shadow in the
/etc directory).
• There are also elaborate rules governing the use of passwords and their
expiry.

➢ echo
The following command without specifying its arguments within quotes squeezes
tabs to a single space as below.
$echo Hello Dinesh

9 | UNIT 2: LINUX Commands I


Hello Dinesh
To preserve spaces, include the arguments within quotes as given below :
$echo “Hello Dinesh”
Hello Dinesh
Note : By default, echo command does not interpret the escape characters like \n,
\t when included in text to display. You can enable their interpretation by –e
option, and disable by –E option.
$echo –e “hello\n”
Hello
As shown in above command new line has been inserted after hello string.

➢ clear
It clears the screen.
$ clear

➢ tput
The following command clears the screen.
$tput clear
This command moves cursor and login prompt to a row no. 10 & column
no. 10.
$ tput cup 10 10

➢ bc

If arguments are not specified, it takes input from keyboard and terminates
each line by <Enter> key. Press Ctrl-D for terminating input.
$bc
3*5
15
Ctrl-D

10 | UNIT 2: LINUX Commands I


➢ script
• Allows you to record your login session in a file typescript in the current
directory.
• All commands, their output and error messages are stored in a file for later
references. For recording the session, invoke the script command in the
beginning of session as
$script
$_
• At the end of session
$exit

➢ calendar: message to self

• Provides a useful remainder mechanism for as user.


• Sort of engagement diary
• The command searches for the file calendar in current directory for the lines
containing current date or ‘Tomorrow’ and displays those lines on the
terminal.
$ calendar
April 29, 2008 Submission
CC Meeting scheduled on April 30
• Calendar uses various formats of date.
• Calendar is a shell script. Your calendar file should be regularly updated and
executed at least once a day.

➢ lock
• It asks you for password, this need not be the same as your login password.
After executing this command $ prompt disappears.

• The terminal is locked for 30 minutes. You can unlock your terminal any
time within 30 min.

• If you don’t then you will be logged out by the system automatically.

$lock
11 | UNIT 2: LINUX Commands I
Password:
Re-enter password:
Terminal locked by pramod 0 minutes ago
You may also specify the time for lock (below 60 min) as follows. It locks
your terminal for 55 min.
• $lock -55

➢ spell and ispell

• Used for spelling checking


$spell file_name
$spell Facility.txt
utilise
• ispell is similar tool for Linux
• It highlights the line and the word and suggests some alternatives

2.3 Navigating the File System Concepts


UNIX File System
Introduction:
• A simple description of the UNIX system, also applicable to Linux, is this:
“On a UNIX system, everything is a file; if something is not a file, it is a
process.”
• Files are collection of data items stored on disk, such as data, music (mp3),
picture, movie, sound, book etc. In fact whatever you store in computer it must
be in the form of file. Files are also associated with devices like hard disk,
floppy disk etc.
• File is the last object in your file system tree.
• All UNIX file systems are organized within a single tree structure underneath
one root directory.
• Although everything is a file in UNIX, it is necessary to divide these files into
three categories-
i. Ordinary files-contain only data
ii. Directory files-contains other files and directories
iii. Device files-represents all hardware devices.
12 | UNIT 2: LINUX Commands I
• Ordinary files:

Ordinary files are the most common type of file on a UNIX system and are the
kind of file that contains data or software. It also contain source programs,
object and executable code, all unix commands and any file created by user.
The commands like ls, cat etc are also Ordinary files. Also referred as
Regular files.
• Directories:

• A directory file contains listing of all the files and subdirectories within it with
some details.
• The UNIX file system is organized with number of directories and
subdirectories.
• Users cannot update a directory directly.

• Device Files:

• The UNIX system treats devices as files. This definition includes disks,
terminals, printers etc.
• Typically, they are found below the /dev directory.

• File names:

• UNIX allows filenames upto 255 characters in length


• Filenames can contain any ASCII character except /
• Control characters are also allowed

• eg –list, nos., @name, .abc

The PATH
The sequence of directories that a system will search to look for a command is specified in its
own PATH variable
If you evaluate the value of this variable, you will find a directory list separated by colons:
$echo $PATH
/bin:/usr/bin:/home/local/bin:/usr/bin/X11:/oracle/bin:.

13 | UNIT 2: LINUX Commands I


There are 6 directories in this list, and when you issue of command, the system will search this list
in the sequence specified to locate and execute it. Note that this list also includes a current
directory indicated by a singular dot at the end.

➢ Absolute Pathnames
• The location of a file or directory can be specified by either of two
methods:
by its absolute pathname or its relative pathname.
• Absolute pathnames begin with a slash ( / ).
• It’s a complete path to file location.
• It can be used anytime you wish to specify a file name.

➢ Relative Pathnames
• Relative pathnames do not begin with /.
• Specifies location relative to your current working directory.
• Can be used as a shorter way to specify a file name.
➢ The Home Directory
• The system places a user in his home directory upon login.
• Every user has a home directory, typically under /home.
• Contains user-specific configuration files as well as user data

➢ THE PARENT-CHILD RELATIONSHIP


• All files in UNIX are "related" to one another.
• The file system in UNIX is a collection of all these related files (ordinary,
directory and device files) organized in a hierarchical (an inverted tree)
structure.
• This system has also been adopted by DOS and Windows, and is visually
represented in Fig.

14 | UNIT 2: LINUX Commands I



• Fig. 5.1 The UNIX File system

• The implicit feature in every UNIX file system is that there is a supremo,
which serves as the reference point for all files.
• This top is called root, and is represented by a / (front slash). root is actually
a directory file, and it has a number of sub-directories under it.
• These sub-directories, in turn, have more sub-directories and other files
under them.
• For instance, bin and usr are two directories y under root, while a second bin
and kumar are sub-directories under usr.
• Every file, apart from root must have a parent, and it should be possible to
trace the ultimate parentage of a file to root.
• If the ancestry of a file can’t be traced to root, the file is simply not part of the
file system.
• This should be easy for you to understand as we all have our own families
with similar grandparent—parent—child relationships.
• Thus, the usr directory is the parent of kumar, while root is the parent of usr,
and the grandparent of kumar.
• If you create a file login.sql under the umar directory, then kumar will be the
parent of this file.
• It is also obvious that, in these parents—child relationships, the parent are
always a directory.
• usr and kumar are both directories as they are both parents of at least one file
or directory.
• login.sql is simply an ordinary file; it can't have any directory under it.

➢ pwd ( print working directory )

The following command displays the absolute path of the current working
directory.
15 | UNIT 2: LINUX Commands I
$pwd
/user/pramod

➢ cd: CHANGING DIRECTORIES


• You can move around in the file system by using the cd (change directory)
command, when used with an argument, it resembles its DOS counterpart.
• It changes the current directory to the directory specified as the argument, for
instance, progs:
$ pwd
/usr/kumar
$ cd progs # progs must be in the current directory
$ pwd
/usr/kumar/progs

• Though pwd displays the absolute pathnames. Cd doesn’t need to use one.
• The command cd progs here means: “Change your sub-directory to progrs under
the current directory.”
• Using an absolute pathname causes no harm either; use cd /usr/kumar/progs for the
same effect,
• When you need to switch to the /bin directory where most of the commonly used
UNIX commands are kept, you should use the absolute pathname:

$ pwd
/usr/kumar/progs
$ cd /bin # Absolute pathname required here because
$ pwd # bin isn 't in current directory
/bin
cd can also be used without any arguments:
$ pwd
/usr/kumar/progs
$ cd # cd used without arguments
$ pwd # reverts to the home directory
/usr/kumar
This command invoked without an argument doesn't indicate the current directory.
It simply switches to the home directory, i.e., the directory where the user originally
logged in. Therefore, if you wander around in the file system, you can force an
immediate return to your home directory by simply using cd:
$ cd /usr/sharma
16 | UNIT 2: LINUX Commands I
$ pwd
/usr/sharma
$ cd # Returns to home directory
$ pwd
/usr/kumar
The cd command can sometimes fail if you don't have proper permissions to access
the directory. This doesn't normally happen unless you deliberately tamper with the
permissions of the directory.

Note: Unlike DOS, when cd is invoked without arguments, it simply reverts to its
home directory. It doesn't show you the current directory!

➢ mkdir
Use this command to create a directory.
$ mkdir essays
To get "into" this directory,
$ cd essays
To see what files are in essays :
$ ls
Number of directories can be created using single mkdir command
$ mkdir dcomp1 dcomp2
➢ Directory tree can be created using mkdir command
$ mkdir science science/phy science/maths
$ mkdir test
mkdir : can’t make directory test

➢ rmdir

• Use this command to remove a directory.


For example, to remove a directory called "essays", do this:
$ rmdir essays
A directory must be empty before it can be removed. To empty a directory,
use rm

17 | UNIT 2: LINUX Commands I


➢ ls - list directory contents
The command to list directories and files is ls. With options it can provide
information about the size, type of file, permissions, and dates of file
creation, change and access.
• Syntax

ls [options] [argument]
When no argument is used, the listing will be of the current directory. There are
many very useful options for the ls command.
When using the command, string the desired options together preceded by "-".

Options Meaning
1. -a lists all files, including those beginning with a dot (.).
2. -d lists only names of directories, not the files in the directory
3. -F indicates type of entry with a trailing symbol:
d directories / sockets = symbolic links @
executables *
4. -l long listing: lists the mode, link information, owner, size, last
modification (time).
5. -x displays multicolumn output
6. -r sorts files in reverse order
7. -R Recursive listing of all files in subdirectori

18 | UNIT 2: LINUX Commands I

You might also like