UNIX – Commands
UNIX – Commands
Login Commands
passwd
logout
who
who am I
clear
Login Commands
Passwd
The passwd command changes passwords
for user accounts. A normal user may only
change the password for his/her own account,
while the superuser may change the
password for any account.
OPTIONS
-d, --delete
Delete a user's password (make it empty).
-e, --expire
Immediately expire an account's password.
-l, --lock
Lock the password of the named account.
Login Commands
-u, --unlock
Unlock the password of the named account.
Example :
Change your own password:
$ passwd
output:
$ passwd
Changing password for ubuntu.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Login Commands
logout
logout command allows you to
programmatically logout from your session.
causes the session manager to take the
requested action immediately.
EXAMPLES
Example:
To logout from current user session:
$ logout
output:
no output on screen, current user session will
be logged out.
Login Commands
who
who - show who is logged on
OPTIONS
-l, --login
print system login processes
-q, --count
all login names and number of users logged
on
-u, --users
list users logged in
Login Commands
whoami
whoami prints the effective user ID.
This command prints the username
associated with the current effective user ID.
syntax
whoami [OPTION]
OPTIONS
--help
Display a help message, and exit.
--version
Display version information, and exit.
Login Commands
clear
clear the terminal screen.
EXAMPLE
Options
-l
use a long listing format
--author
with –l, print the author of each file.
-C
list entries by columns
-g
like -l, but do not list owner
-G, --no-group
in a long listing, don't print group names
File & Directory
Commands
-m
fill width with a comma separated list of
entries
-S
sort by file size
-r, --reverse
reverse order while sorting
-t
sort by modification time
-x
list entries by lines instead of by columns
File & Directory
Commands
Example:
#sample1.txt
Commands
To display content of a file.
$ cat sample.txt
This is a sample text file
EXAMPLES
Move to the sample folder
$ cd /usr/local/sample
Syntax
pwd [options]
Example
To print current working directory, enter:
$ pwd
output:
/home/user
Commands
mv
The mv command allows you to move and
rename files.
Syntax
The syntax for the mv command is:
mv [options] sources target
Options
Option Description
-f Forces the move.
-i Prompt for a confirmation before
overwriting any files.
Example
mv -f tech /usr
Commands
cp
To copy one or more files to another location.
SYNTAX
cp [options]... Source Dest
EXAMPLES
Copy sample.txt to sample.bak.
$ cat sample.txt
This is a sample file
$ cp sample.txt sample.bak
$ cat sample.bak
This is a sample file
Commands
ln
creates links between files. ln creates a link
to file TARGET with the name LINKNAME.
Syntax
$ ln file1 file2
Example
$ cat target.txt
target file
$ ln target.txt link.txt
$ cat link.txt
target file
Commands
rm
rm removes each specified file. By default, it
does not remove directories.
SYNTAX
rm [OPTION]... FILE...
Example-1:
Remove the file myfile.txt. If the file is write-
protected, you will be prompted to confirm
that you really want to delete it:
$ rm myfile.txt
Example-2:
Remove the file myfile.txt. You will not be
prompted, if the file is write-protected .
$ rm -f myfile.txt
Commands
rmdir
Remove the DIRECTORY(ies), if they are
empty.
Syntax :
rmdir [OPTION]... DIRECTORY...
Example :
rmdir command will delete the empty
directories. i.e directory without any sub-
directories or files:
$ rmdir test
Commands
mkdir
Create the DIRECTORY(ies), if they do not
already exist.
Syntax :
mkdir [OPTION]... DIRECTORY...
Example :
Creates a new directory called mydir whose
parent is the current directory.
$ mkdir mydir
output:
$ls
mydir
Commands
umask
a new file's permissions may be restricted in
a specific way by applying a permissions
"mask" called the umask. The umask
command is used to set this mask, or to show
you its current value.
Syntax
$ umask [-S] [mask]
Options
-S Accept a symbolic representation
of a mask, or return one.
0002
0002 is the same as 002.
$ umask -S
Numeric mode
A numeric mode is from one to four octal
digits (0-7), derived by adding up the bits
with values 4, 2, and 1. Permissions for the
user who owns the file: read (4), write (2),
and execute (1).
Commands
EXAMPLES
Read by owner only
$ chmod 400 sample.txt
Read by anyone
$ chmod 004 sample.txt
EXAMPLES
Change the owner of file.
$ chown user sample.txt
EXAMPLES
To Make oracleadmin the owner of the
database directory
$ chgrp oracleadmin /usr/database
Commands
find
The find command in UNIX is a command line
utility for walking a file hierarchy. It can be
used to find files and directories and perform
subsequent operations on them. It supports
searching by file, folder, name, creation date,
modification date, owner and permissions.
To find a single file by name pass the -name
option to find along with the name of the file
you are looking for.
pg [options] [file...]
Options
-number : The number of lines per page.
Usually, this is the number of CRT lines minus
one.
-c : Clear the screen before a page is
displayed, if the terminfo entry for the
Commands
Example
$ pg myfile.txt
Syntax
more [options] [files]
Options
-c : Page through the file by clearing
the
window. (not scrolling).
-d : Displays "Press space to continue,
'q' to quit“
-w : Waits for a user to press a key
Commands
examples
more +3 myfile.txt
Display the contents of file myfile.txt,
beginning at line 3.
ls | more
List the contents of the current directory with
ls, using more to display the list one screen
at a time.
Commands
less
This command is used to see the information
of the document page wise. This command
work just like more command and it is older
version.
Syntax
Example
$less abc.txt
Commands
Navigation of less Command
p Beginning of file
G End of file
Commands
head
head command help in viewing lines at the
beginning of the file respectively.
Syntax:
$head –Line Number File Name
Syntax
Options
-l Counts number of lines
-w Counts number of words
-c Counts number of
characters
Commands
Example
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
$ wc abc.txt
Output
2 20 42 abc.txt
Syntax
Timestamp Description
Access time The last time the file was
read. - atime
Modification The last time the contents of
the file were modified. -
mtime
Change time The last time the file's
Commands
With no options, touch will change the atime,
mtime, and ctime of file to the current system
time.
Examples
$ touch file.txt
$ touch -c file.txt
Redirection & Piping
Redirection is a feature in Linux such that
when executing a command, you can change
the standard input/output devices. The basic
workflow of any Linux command is that it
takes an input and give an output.
Output Redirection
The '>' symbol is used for output (STDOUT)
redirection.
Example:
Redirection & Piping
>> : Does the same as >, except that if the
target file exists, the new data are appended.
Input redirection
The '<' symbol is used for input(STDIN)
redirection.
command1 | command2
SYNTAX
grep [OPTIONS] PATTERN [FILE...]
EXAMPLE
$ grep "Linux" input.txt
Welcome to Linux.
For example :
$ grep "Linux" input.txt output.txt
input.txt:Welcome to Linux.
output.txt:I hope you enjoyed working on
Linux.
Syntex:
example
$ fgrep "support" myfile.txt
Finding Pattern in Files
egrep
Search for a pattern using extended regular
expressions. A regular expression is a pattern
that describes a set of strings.
egrep is essentially the same as running
grep with the -E option.
syntax
egrep [options] PATTERN [FILE...]
Example 1
$ egrep "support|help|windows" myfile.txt
Example 3
$ egrep -c '^begin|end$' myfile.txt
Options
-f Extract a set of specified fields.
-d Used with the -f option. Use a specified
delimiter rather than default tab.