You are on page 1of 28

LINUX Basic Commands

Part II
Shell
• The prompt is displayed by a special program called the shell.
• A system program that allows a user to execute:
• shell functions (internal commands)
• other programs (external commands)
• shell scripts
• Linux is case-sensitive. This means that cat and Cat are different
commands.
• Shell accepts commands, and run those commands.
• They can also be programmed in their own language - called “shell
scripts”.
Commands
• Command is a program that can run
• To run a command type its name and press Enter key
• Format:
Command Name and 0 or more arguments:
Command_Name [arg1] ... [argN]
• Arguments can be
• Options (switches to the command to indicate a mode of operation) ; usually prefixed
with a hyphen (-)
• Non-options, or operands, basically the data to work with (actual data, or a file name)
• There must be space between the command name and arguments
• Use small case
Internal Commands vs External Commands
• Internal commands are something which is built into the shell.
- Execution speed is high
- No process needs to be spawned for executing it

   For example, when using the "cd" command, no process is created. The current directory simply gets
changed on executing it.
• External commands are not built into the shell.
- These are Executable present in a separate file
- For execution, a new process has to be spawned and the command gets executed.

For example, when you execute the "cat" command, which usually is at /usr/bin, the executable
/usr/bin/cat gets executed.
Internal Commands vs External Commands
• How to find out whether a command is internal or external?
• Type command
$ type cd

• PATH
PATH is an Environment Variable, specifying a set of directories where executable
files are located
In Linux, PATH variable is specified as a list of one or more directory names separated by
(:) colon sign.
$ env
which command
● Used to locate the executable file associated with the given command by searching it
in the path environment variable.
● It has 3 return status as follows:
– 0 : If all specified commands are found and executable.
– 1 : If one or more specified commands is nonexistent or not executable.
– 2 : If an invalid option is specified.
● Syntax: which [filename1] [filename2] ...
Obtaining help

• The man command displays reference pages for the command you specify.
• The Linux man pages (man is short for manual ) cover every command available.
• To search for a man page, enter man followed by the name of the command to
find .
• For example: man ls
• To exit press “q”

7
Users in Linux
• Accounts that are used to login into a system.
• unique identification number or UID.
• All the information are stored in /etc/passwd file.
• The hashed passwords are stored in /etc/shadow file.
Logging into Linux and Logging out of Linux
• login: <type your user id>
• password: <type your password>
• Linux is case sensitive
• logout
• leaves the system
• quits the shell
• exit
• In the /etc directory, the passwd and the group files hold all of the
users and group information.
• These files are essential when logging on to the system.
User types & groups
• Superuser/root/administrator
• Access to all the files on the system.
• Can add, delete and modify a user account.
• Normal users 
• Limited access.
• The service account
• Created by installation packages when they are installed.
• Used by services to run processes and execute functions.
• User groups
• A user group is a group of one or more users.
• A user can be a member of more than one group.
• When a new user is created, system
• Assigns UID to the user.
• Creates a home directory /home/.
• Sets the default shell of the user to be /bin/sh.
• Creates a private user group, named after the username itself.
• Contents of /etc/skel are copied to the home directory of the new user.
• .bashrc, .bash_profile and .bash_logout are copied to the home directory of
new user.
Real and Effective user id
• Real User ID
• The UserID of the user that has initiated the operation.
• Effective UserID
• It is normally the same as Real UserID, but sometimes it is changed to enable
a non-privileged user to access files that can only be accessed by a privileged
user like root.
User account management

• Log in as root

• User account • Group


• useradd • groupadd
• To add user accounts to your system • To create a new group
• Syntax: useradd [options] • groupadd [option] group_name
name_of_the_user
• To add the user to the group
• user is added to the passwd file
• useradd –g group_name user_name
• userdel
• groupdel
• To delete user account
• syntax: userdel [option] user_name
Commands su, sudo, id
• su
• To switch from one user account  to other user account
• Syntax: su username
• Sudo
• runs a single command with root privileges
• Syntax: sudo [command]
• id
• To print user id, group id
Basic Commands
• Who is logged in?
$ who
displays a list of users who are currently logged into the computer.
$ users
$w
• Who are you?
$ whoami
$who am i
Echo command
• Used to display line of text/string that are passed as an argument .
• This is a built in command 
• Syntax:
echo [option] [string]
Example
echo “message”
\n option creates new line from where it is used.
echo -e “Welcome \n To \n DCST” Displays each word on new line
Assigning value to a variable
VAR1=“Name"
VAR2=100
To display the value command is
echo $VAR1
Listing the files in directory
• ls
• lists directory contents of files and directories.
date command
• To display system’s date
• date [OPTION]... [+FORMAT]
Example
$ date Displays IST time
$ date –u Displays GMT time
$ date +%A Displays the day of the week
History command
• Used to view the previously executed command.
• Syntax
history [option]
$ history {all commands in the history}
$ history 10 { last 10 }
The most recent command can be viewed using !!
$ !! { repeats last command}
$!-2 {repeats second last command}
$!ca {repeats last command that begins with ‘ca’}
To print command before executing so that a wrong command does not get executed
use :p after event number of command.
Ex. !5:p to print 5th last command
Cal command
• To display calendar
• Syntax
cal [ [ month ] year]
Example
$ cal {Shows current month calendar on the terminal.}
$ cal 2 2011 { for February 2011}
$cal -3 {Shows calendar of previous, current and next month}
Using calculator
Syntax • The bc command supports the
bc [ -hlwsqv ] [long-options] [ file ... ] following operators:
Options: • Arithmetic operators
-h, {- -help } : Print the usage and exit • Increment or Decrement operators
-i, {- -interactive } : Force interactive mode
-l, {- -mathlib } : Define the standard math library • Assignment operators
-w, {- -warn } : Give warnings for extensions to • Comparison or Relational
POSIX bc operators
-s, {- -standard } : Process exactly the POSIX bc
language • Logical or Boolean operators
-q, {- -quiet } : Do not print the normal GNU bc • Math functions
welcome
-v, {- -version } : Print the version number and • Conditional statements
copyright and quit • Iterative statements
Example
Use –l option to include math library
bc –l
s(2) 🡪 displays the sin(x) for x=2, here x is in radians
var++ 🡪increments the value of variable
var =1 while(var <10) var++ 🡪displays number from 1 to 9
v1=1 v2=0 v1&&v2 🡪 displays logical and of v1 and v2
Basic commands

• Knowing machine characteristics


• uname
• Terminal characteristics
• tty
• To display currently running process
• ps
• To immediately Shutdown the system
$ halt
• To clear the screen
$ clear
• To do nothing for sometime
$ sleep 4 { 4 seconds }
- usually used for delays in shell scripts
• Command grouping
- using semicolon “;” group multiple commands as if a single command is working
$ date; cal; date
tput
• Control the color and cursor of your terminal
• Screen attributes
• Colors background and text color
• Hide the cursor:
• tput civis
• Display the cursor:
• tput cnorm
Redirection operator
• Output redirection
• The output from a command normally intended for standard output can be
easily diverted to a file instead.
•  use > operator to write the output of a command to a file.
• use >> operator to append the output in an existing
• Input Redirection
• The commands that normally take their input from the standard input can
have their input redirected from a file using input redirection.
• Use < operator to redirect input from a file
• Assignment 1.1

You might also like