Linux Unit 2 notes
Linux Unit 2 notes
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.
• 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.
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.
• 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.
File names:
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,
1 d day of month
2 h month name
3 m month number
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
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
➢ who am I
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
➢ 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
➢ 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
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:
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:.
➢ 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 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.
The following command displays the absolute path of the current working
directory.
15 | UNIT 2: LINUX Commands I
$pwd
/user/pramod
• 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
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