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

Linux Basic Commands

The document discusses Linux commands like echo, date, hostname, arch, uname, uptime, whoami, who and their usage. Echo is used to display messages or values of variables. Date displays or sets the system date and time. Hostname displays or sets the system hostname. Other commands provide system information.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Linux Basic Commands

The document discusses Linux commands like echo, date, hostname, arch, uname, uptime, whoami, who and their usage. Echo is used to display messages or values of variables. Date displays or sets the system date and time. Hostname displays or sets the system hostname. Other commands provide system information.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 26

LINUX ENVIRONMENTS

Basic Commands

1. Echo command

echo is a built-in command in the bash and


C shells that writes its arguments to
standard output.

The syntax for echo is:

echo [option(s)] [string(s)]

The items in square brackets are optional.


A string is any finite sequence of
characters (i.e., letters, numerals, symbols
and punctuation marks).

The program accepts the following


options. Options must precede operands,
and the normally-special argument ‘--’
has no special meaning and is treated like
any other string.

‘-n’ Do not output the trailing newline.

‘-e’ Enable interpretation of the


following backslash-escaped characters in
each string:

‘\n’ newline

‘\t’ horizontal tab

‘\v’ vertical tab

‘\\’ backslash

Examples of echo command

 When used without any options or


strings, echo returns a blank line
on the display screen followed by
the command prompt on the
subsequent line. This is because
pressing the ENTER key is a
signal to the system to start a new
line, and thus echo repeats this
signal.
 When one or more strings are
provided as arguments, echo by
default repeats those stings on the
screen. Thus, for example, typing
in the following and pressing the
ENTER key would cause echo to
repeat the phrase This is a pen. on
the screen:

echo This is a pen.

It is not necessary to surround the


strings with quotes, as it does not
affect what is written on the
screen. If quotes (either single or
double) are used, they are not
repeated on the screen.

it can also show the value of a particular


variable if the name of the variable is
preceded directly (i.e., with no
intervening spaces) by the dollar
character ($), which tells the shell to
substitute the value of the variable for its
name.

For example, a variable named x can


be created and its value set to 5 with
the following command:

x=5

The value of x can subsequently be


recalled by the following:

echo The number is $x.

 Echo is particularly useful for


showing the values of
environmental variables, which
tell the shell how to behave as a
user works at the command line
or in scripts (short programs).
For example, to see the value of
HOME, the environmental value
that shows the current user's
home directory, the following
would be used:

echo $HOME

 Likewise, echo can be used to


show a user's PATH
environmental variable, which
contains a colon-separated list of
the directories that the system
searches to find the executable
program corresponding to a
command issued by the user:

echo $PATH

 echo, by default, follows any


output with a newline character.
This is a non-printing (i.e.,
invisible) character that represents
the end of one line of text and the
start of the next. It is represented
by \n in Unix-like operating
systems. The result is that the
subsequent command prompt
begins on a new line rather than
on the same line as the output
returned by echo.
 The -e option is used to enable
echo's interpretation of additional
instances of the newline character
as well as the interpretation of
other special characters, such as a
horizontal tab, which is
represented by \t. Thus, for
example, the following would
produce a formatted output:

Echo -e “\n Projects: \n\n\


tplan \n\tcode \n\ttest\n”

(The above command should be


written on a single line, although
it may render as two lines on
smaller display screens.)
 The -n option can be used to stop
echo from adding the newline to
output.
 Echo command is also used to
evaluate the expression

X=11
Y=2
E
c
h
o

`
e
x
p
r

$
x
+

$
y
`

1
3
Display message welcome on screen

echo 'Welcome'
Write message File deleted to a file called /tmp/log.txt

echo 'File has been deleted' > /tmp/log.txt


Append message File deleted to a file called /tmp/log.txt

echo 'File has been deleted' >> /tmp/log.txt


Append message and command output on screen

echo "Today's date is $(date)"


Append message and command output to a file

echo "Today's date is $(date)" > /tmp/date.txt

2. Date Command

Linux “date” command returns you the date and time when you call it without any options. Use
the date command to display the current date and time or set the system date / time over ssh
session. You can also run the date command from X terminal as root user.

This is useful if the Linux server time and/or date is wrong, and you need to set it to new values
from the shell prompt.

The first thing to do is to get the current date and time:

#date
Sun Dec 14 11:33:55 IST 2008

This is the simplest use of this command. Now suppose you wanted to just get the date and
nothing more:

# date +”%d”
14

If you want the date, complete with date, month, and year:

#date +”%d%m%y”
141208

To get the day of the week along with the rest of the date:

#date +”%a%d%m%y”
Sun141208

These are a few of the many possibilities that the “date” command offers you. Check out “date –
help for options”. Some interesting ones are:

%% a literal percent sign ("%").

%a The abbreviated weekday name (e.g., Sun).

%A The full weekday name (e.g., Sunday).

The abbreviated month name (e.g., Jan).

%b

%B locale's full month name (e.g., January).

%c The date and time (e.g., Thu Mar 3 23:05:25 2005).

%C The current century; like %Y, except omit last two digits (e.g., 20).

%d day of month (e.g., 01).

%D date; same as %m/%d/%y.

%e day of month, space padded; same as %_d.

%F full date; same as %Y-%m-%d.


%g last two digits of year of ISO week number (see %G).

%G year of ISO week number (see %V); normally useful only with %V.

%h same as %b.

%H hour (00..23).

%I hour (01..12).

%j day of year (001..366).

%k hour, space padded ( 0..23); same as %_H.

%l hour, space padded ( 1..12); same as %_I.

%m month (01..12).

%M minute (00..59).

%n a newline.

%N nanoseconds (000000000..999999999).

%p locale's equivalent of either AM or PM; blank if not known.

%P like %p, but lower case.

%r locale's 12-hour clock time (e.g., 11:11:04 PM).


%R 24-hour hour and minute; same as %H:%M.

%s seconds since 1970-01-01 00:00:00 UTC.

%S second (00..60).

%t a tab.

%T time; same as %H:%M:%S.

%u day of week (1..7); 1 is Monday.

%U week number of year, with Sunday as first day of week (00..53).

%V ISO week number, with Monday as first day of week (01..53).

%w day of week (0..6); 0 is Sunday.

%W week number of year, with Monday as first day of week (00..53).

%x locale's date representation (e.g., 12/31/99).

%X locale's time representation (e.g., 23:13:48).

%y last two digits of year (00..99).

%Y year

%z +hhmm numeric time zone (e.g., -0400).


%:z +hh:mm numeric time zone (e.g., -04:00).

%::z +hh:mm:ss numeric time zone (e.g., -04:00:00).

%:::z numeric time zone with ":" to necessary precision (e.g., -04,
+05:30).

%Z alphabetic time zone abbreviation (e.g., EDT)

You can also do some fancy formatting. You can also use spaces and commas. Here’s a pretty
fancy example:

If you want to add a hyphen or a back-slash in between the different parts of the date
Linux Set Date

Use the following syntax to set new date andtime:

For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
# date -s "2 OCT 2006 18:00:00" OR

# date --set="2 OCT 2006 18:00:00"

You can also simplify format using following syntax:


# date +%Y%m%d -s "20081128"

date -s "11/20/2003 12:48:00"

Set the date to the date and time shown.

Linux Set Time

To set time use the following syntax:


# date +%T -s "10:13:13"
3. Hostname

Hostname is the program that is used to


either set or display the current host,
domain or node name of the system.
These names are used by many of the
networking programs to identify the
machine.
hostname: Print or set system name

With no arguments, hostname prints the


name of the current host system. With one
argument, it sets the current host name to
the specified string. You must have
appropriate privileges to set the host
name. Synopsis:

hostname [name]

The only options are --help and --version.


An exit status of zero indicates success,
and a nonzero value indicates failure.

4. Arch

Command to get architecture type (print


machine architecture)

The arch command will print the type of


computer you are using.
5. Uname

Print name of current system

6. Uptime

uptime - Tell how long the system has been running.

uptime gives a one line display of the following information. The current time, how long the
system has been running, how many users are currently logged on, and the system load averages
for the past 1, 5, and 15 minutes.

7. Whoami

whoami prints the user name associated with the current effective user ID.

Example:
8. who am i

limit the output to describing the invoking user, equivalent to the -m option of who
command. The am and i or I must be separate arguments.

9. who

Displays who is on the system.

Syntax

who [-H] [-m] [am i] [ file ]

-H Output column headings above the regular output.

-m Output only information about the current terminal.

-u List only those users who are currently logged in.

am i In the "C" locale, limit the output to describing the invoking user, equivalent
to the -m option. The am and i or I must be separate arguments.

Examples
10. w command

The w command shows who is logged in


to the system and what they are doing.

A login, logging in or logging on is the


entering of identifier information into a
system by a user in order to access that
system (e.g., a computer or a website). It
generally requires the user to enter two
pieces of information, first a user name
and then a password.

The basic syntax of w is:

w [options] [username1,
username2, . . .]

The square brackets indicated that the


enclosed items are optional. When used
without any options, w sends to standard
output (which is by default the display
screen) a header line followed by a table
that contains a line of data for each user
currently logged in.

The main part of w's output consists of a


table showing eight items of information
for each user currently logged into the
system. The eight columns are labeled
USER, TTY, FROM, LOGIN@, IDLE,
JCPU, PCPU and WHAT.

USER is the login name of the user. TTY


(which now stands for terminal type but
originally stood for teletype) is the name
of the console or terminal (i.e.,
combination of monitor and keyboard)
that the user logged into, which can also
be found by using the tty command.
Every time a user logs in across the
network, a new tty is assigned to that
user.
FROM is the remote host (i.e., the name
of some other computer), if any, that the
user logged into. LOGIN@ is the time at
which the user logged in. IDLE is the
number of hours and minutes since the
user last typed anything at the keyboard.

JCPU is the number of minutes


accumulated by all processes attached to
the tty. It does not include past
background processes (i.e., low priority
processes that operate only in gaps
between higher priority foreground
processes), but it does include currently
running background processes.
PCPU is the time consumed by the
current process, named in the WHAT
column. WHAT lists the name of the
current process along with any options
and arguments (i.e., input files) used
with the command that launched it.

Among the more useful of w's few


options is -h, which tells it to omit the
header header line from its output. The
-s option tells it to use the short format,
which omits the login time, JCPU and
PCPU columns. The -l option creates a
long listing, which is the same as the
default. The - V option displays the
version number of the currently
installed w program.

By default, w reports on all users.


However, it can be made to report on
only a specified set of users by
providing those usernames in a comma-
separated list.
11. cal

Calendar for the month and the year.


cal originally appeared in version 6 of AT&T Unix. Since then there have been versions released
for BSD, Linux, and other Unix variants. You should check your particular installation's manual
for version-specific options. Listed below are the traditional syntax and options for Unix cal.
In general, if no options are given, cal displays the current month at the command line. It's a
quick and convenient way to glance at the dates of the month, and can be useful as part of

a login
script. Syntax

cal [options] [[[day] month] year]

Options

-1 Display a single month. This is the default.

-3 Display three months: last month, this month, and next month.

-s Display the calendar using Sunday as the first day of the week.

-m Display Monday as the first day of the week.


-j Display dates of the Julian calendar.

-y Display a calendar for the entire current year.

Examples

cal

Displays the calendar for this month.


cal 12 2000

Displays the calendar for December of the year 2000.

12. bc

Basic Calculator

Syntax:

bc

13. wc

The wc (i.e., word count) command by default counts the number of lines,
words and characters in text.

wc defines a word as a set of contiguous letters, numbers and/or symbols which are separated
from other characters by one or more spaces, tabs and/or newline characters(which are
generated when the RETURN key is pressed). When counting the number of characters, all
characters are counted, not only letters, numbers and symbols, but also spaces, tabs and newline
characters. A line is only counted if it ends with a newline character.

wc's syntax is
wc [options] [file_name(s)]

The items in square brackets are optional. If no file names are provided, wc reads from
its standard input, which by default is text entered at the keyboard.

This can be seen by typing

wc

at the command line (i.e., in the all-text mode), pressing the ENTER key to move to a new line
and then typing some text on one or more lines. The command is executed (i.e., run) by pressing
the ENTER key again and then pressing the CONTROL and d keys simultaneously. This causes
wc to write in a new line (under the lines of text) its count of the numbers of lines, words and
characters in the text.

Wc command ouputs the number of lines followed by number of words followed by number of
bytes followed by name of file.

14. Concept of aliases

The alias command makes it possible to launch any command or group of commands (inclusive of
any options, arguments and redirection) by entering a pre-set string (i.e., sequence
of characters).

That is, it allows a user to create simple names or abbreviations (even consisting of just a single
character) for commands regardless of how complex the original commands are and then use
them in the same way that ordinary commands are used.

A command is an instruction given by a user to tell a computer to do something. Commands are


generally issued by typing them in at the command line (i.e., an all-text user interface) and then
pressing the ENTER key, which passes them to the shell. A shell is a program that provides the
traditional, text-only user interface for a Unix-like operating systems. Its primary function is to
read commands and then execute (i.e., run) them.

The alias command is built into a number of shells including ash, bash (the default shell on
most Linux systems), csh and ksh. It is one of several ways to customize the shell (another is
setting environmental variables). Aliases are recognized only by the shell in which they are
created, and they apply only for the user that creates them, unless that user is the root (i.e.,
administrative) user, which can create aliases for any user.

Listing and Creating Aliases


The general syntax for the alias command varies somewhat according to the shell. In the case of
the bash shell it is

alias [-p] [name="value"]

When used with no arguments and with or without the -p option, alias provides a list of aliases
that are in effect for the current user, i.e.,

alias

Some of the aliases listed are likely to be system-wide aliases that apply to all users and are
created automatically for each new user for a particular shell. Aliases for any other shell can be
seen by first switching to that shell and then using the alias command as above.

name is the name of the new alias and value is the command(s) which it initiates. The alias name
and the replacement text can contain any valid shell input except for the equalssign ( = ).

The commands, including any options, arguments and redirection operators, are all enclosed
within a single pair of quotation marks, which can be single quotes or double quotes. No spaces
are permitted before or after the equals sign. Any number of aliases can be created
simultaneously by enclosing the name in each name-value pair in quotes.

As a trivial example of alias creation, the alias p could be created for the commonly
used pwd command, which shows the current location of the user in the directory structure (and
which is an abbreviation for present working directory), by typing the following command and
then pressing the ENTER key:

alias p="pwd"

Then, to show the current location, instead of typing pwd, the user would only have to type the
letter p and press the ENTER key, i.e.,

Removing Aliases

The command unalias, which is likewise built into bash and some other shells, is used to remove
entries from the current user's list of aliases. Its syntax is

unalias [-a] name(s)

For example, the following would remove the alias rm which was created in an earlier example:

unalias p
unalias removes not only aliases created during the current session but also permanent aliases
that are listed in system configuration files. The -a option tells unalias to remove all aliases for
the current user for the current shell.
A second way to remove an alias is by using the alias command to create a new alias with the
same name. This overwrites the existing alias with that name.

You might also like