Linux Basic Commands
Linux Basic Commands
Basic Commands
1. Echo command
‘\n’ newline
‘\\’ backslash
x=5
echo $HOME
echo $PATH
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
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.
#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:
%b
%C The current century; like %Y, except omit last two digits (e.g., 20).
%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).
%m month (01..12).
%M minute (00..59).
%n a newline.
%N nanoseconds (000000000..999999999).
%S second (00..60).
%t a tab.
%Y year
%:::z numeric time zone with ":" to necessary precision (e.g., -04,
+05:30).
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
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
hostname [name]
4. Arch
6. Uptime
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
Syntax
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
w [options] [username1,
username2, . . .]
a login
script. Syntax
Options
-3 Display three months: last month, this month, and next month.
-s Display the calendar using Sunday as the first day of the week.
Examples
cal
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.
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.
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.
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.
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
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.