Essential Unix/Linux Commands Guide
Essential Unix/Linux Commands Guide
This command displays the all the information about “ls” command including
its syntax, description and usage.
Command Who
Syntax $ who - [option]
Option Description
-a Same as -b -d –login -p -r -t -T -u
-b Time of last system boot
-d Print dead processes
-H Print line of column headings
-l Print system login processes
-m Only hostname and user associated with stdin
-p Print active processes spawned by init
-q All login names and number of users logged on
-r Print current runlevel
-t Print last system clock change
-T Add user’s message status as +, – or ?
-u List users logged in
Description “who” command displays who all are logged onto the network.
2) $ who - H
3) $ who - u
4) $ who – b
Command Cat
Syntax $ cat [argument] [specific file]
Description “cat" is short for concatenate. This command is used to create, view and
concatenate files.
Examples 1) $ cat /etc/passwd
2) $ cat /etc/profile
This command displays the "/etc/profile" file on your screen. Notice that some of the
contents of this file may scroll off of your screen.
This command combines the contents of the first three files into the fourth file.
Description "cd" stands for change directory. It is the primary command for moving around
the filesystem from current directory to the specified directory.
Examples 1) $ cd /usr
This command moves you to the "/usr" directory. "/usr" becomes your
current working directory.
2) $ cd /usr/fred
3) $ cd /u*/f*
Moves you to the "/usr/fred" directory - if this is the only directory matching
this wildcard pattern.
4) $ cd
Issuing the "cd" command without any arguments moves you to your home directory.
5) $ cd -
Using the Korn shell, this command moves you back to your previous working
directory. This is very useful when you're in the middle of a project, and keep
moving back-and-forth between two directories.
Command Cp
Syntax $ cp [options] file1 file2
2) $ cp /usr/fred/Chapter1 .
This command copies the file named "Chapter1" in the "/usr/fred" directory to the
current directory. This example assumes that you have write permission in the
current directory.
3) $ cp /usr/fred/Chapter1 /usr/mary
This command copies the "Chapter1" file in "/usr/fred" to the directory named
"/usr/mary". This example assumes that you have write permission in the
"/usr/mary" directory.
4) $ cp file1 file2
Command Ps
Syntax $ ps [options]
Description The "ps" command (process statistics) lets you check the status of processes
that are running on your Unix system. It gives a snapshot of the current
process’s
attribute.
Examples 1) $ ps
The ps command by itself shows minimal information about the processes you are
running. Without any arguments, this command will not show information about other
processes running on the system.
2) $ ps -f
The -f argument tells ps to supply full information about the processes it displays. In
this example, ps displays full information about the processes you are running.
3) $ ps -e
The -e argument tells the ps command to show every process running on the system.
4) $ ps -ef
The -e and -f arguments are normally combined like this to show full information
about every process running on the system. This is probably the most often-used form
of the ps command.
5) $ ps -ef | more
Because the output normally scrolls off the screen, the output of the ps -ef command
is often piped into the more command. The more command lets you view one
screenful of information at a time.
6) $ ps -fu fred
This command shows full information about the processes currently being run by
the user named fred (the -u option lets you specify a username).
Command Ls
Syntax $ ls [options] [names]
Description "ls" stands for list. It is used to list information about files and directories.
Examples 1) $ ls
This is the basic "ls" command, with no options. It provides a very basic listing of the
files in your current working directory. Filenames beginning with a decimal are
considered hidden files, and they are not shown.
2) $ ls -a
The -a option tells the ls command to report information about all files,
including hidden files.
3) $ ls -l
The -l option tells the "ls" command to provide a long listing of information about the
files and directories it reports. The long listing will provide important information
about file permissions, user and group ownership, file size, and creation date.
4) $ ls -al
This command provides a long listing of information about all files in the current
directory. It combines the functionality of the -a and -l options. This is probably the
most used version of the ls command.
5) $ ls -al /usr
This command lists long information about all files in the "/usr" directory.
This command lists long information about all files in the "/usr" directory, and all sub-
directories of /usr. The -r option tells the ls command to provide a recursive listing of
all files and sub-directories.
7) $ ls -ld /usr
Rather than list the files contained in the /usr directory, this command lists information
about the /usr directory itself (without generating a listing of the contents of /usr). This
is very useful when you want to check the permissions of the directory, and not the
files the directory contains.
Command Mv
Syntax $ mv [options] sources target
Options -b backup files that are about to be overwritten or removed.
-i interactive mode; if destination file exists, you'll be asked whether to overwrite the
file or not.
Description The "mv" command is used to move and rename files.
Examples 1) $ mv Chapter1 [Link]
This command renames the file "Chapter1" to the new name "[Link]".
2) $ mv Chapter1 garbage
This command renames the file "Chapter1" to the new name "garbage". (Notice that
if "garbage" is a directory, "Chapter1" would be moved into that directory).
3) $ mv Chapter1 /tmp
This command moves the file "Chapter1" into the directory named "/tmp".
4) $ mv tmp [Link]
Assuming in this case that tmp is a directory, this example renames the directory
tmp to the new name [Link].
Command Rm
Syntax $ rm [options] files
Options -d, --directory
unlink FILE, even if it is a non-empty directory (super-user only)
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
-v, --verbose
explain what is being done
Description The "rm" command is used to remove files and directories. (Warning - be very
careful when removing files and directories!)
Examples 1) $ rm [Link]
This command deletes the file named "[Link]" (assuming you have
permission to delete this file).
2) $ rm Chapter1 Chapter2 Chapter3
This command deletes the files named "Chapter1", "Chapter2", and "Chapter3".
This command prompts you before deleting any of the three files specified. The -i
option stands for inquire. You must answer y (for yes) for each file you really want
to delete. This can be a safer way to delete files.
4) $ rm *.html
This command deletes all files in the current directory whose filename ends with
the characters ".html".
5) $ rm index*
This command deletes all files in the current directory whose filename begins with
the characters "index".
6) $ rm -r new-novel
This command deletes the directory named "new-novel". This directory, and all of
its' contents, are erased from the disk, including any sub-directories and files.
Command Grep
Syntax $ grep [options] regular expression [files]
Options -i case-insensitive search
-n show the line# along with the matched line
-v invert match, e.g. find all lines that do NOT match
-w match entire words, rather than substrings
Description Think of the "grep" command as a "search" command (most people wish it was
named "search"). It is used to search for text strings within one or more files.
Examples 1) $ grep 'fred' /etc/passwd
This command searches for all occurrences of the text string 'fred' within the
"/etc/passwd" file. It will find and print (on the screen) all of the lines in this file
that contain the text string 'fred', including lines that contain usernames like "fred" -
and also "alfred".
This command searches for all occurrences of the text string 'fred' within the
"/etc/passwd" file, but also requires that the "f" in the name "fred" be in the first
column of each record (that's what the caret character tells grep). Using this more-
advanced search, a user named "alfred" would not be matched, because the letter
"a" will be in the first column.
3) $ grep 'joe' *
This command searches for all occurrences of the text string 'joe' within all files of
the current directory.
Command Mkdir
Syntax $ mkdir [options] directory name
Description The "mkdir" command is used to create new directories (sub-directories).
Examples 1) $ mkdir tmp
This command creates a new directory named "tmp" in your current directory. (This
example assumes that you have the proper permissions to create a new sub-directory in
your current working directory.)
This command creates three new sub-directories (memos, letters, and e-mail) in the
current directory.
3) $ mkdir /usr/fred/tmp
This command creates a new directory named "tmp" in the directory "/usr/fred". "tmp"
is now a sub-directory of "/usr/fred". (This example assumes that you have the proper
permissions to create a new directory in /usr/fred.)
4) $ mkdir -p /home/joe/customer/acme
Command Rmdir
Syntax $ rmdir [options] directories
Description The "rmdir" command is used to remove directories. (Warning - be very careful
when removing files and directories!)
Examples $rmdir customer
This command deletes the directory named "customer" (assuming you have
permission to delete this directory).
Command Echo
Syntax $ echo [options] IDs
Description The “echo” command is used to display a line of text, or messages. It takes zero, one r
more arguments.
Examples 1) $ echo hi
Two words can be displayed using echo by placing them in [Link] output of the
above command will be – Hello World
Command More
Syntax $ more [filename]
Description This command is used to display the contents of a file. It allows us to set the output
page size and pauses at the end of each page to allow us to read the file.
Examples $ more chapter1
Command Date
Syntax $ date [- options]
Description The “date” command prints the date and time. Using this command, the user ca
display the current date along with the time nearest to the second.
Examples $ date
Command Time
Syntax $ time [options]
Description This command is used to know the resource usage. It runs a program or command
with given arguments, generates a timing statistics about the program run and directs
this statistics report to the standard output.
Examples $ time
Real 0m0.000s
User 0m0.000s
Sys 0m0.000s
Command Kill
Syntax $ kill [options] IDs
Description The command “kill” ends one or more process IDs. In order to do this you must own
the process or be designated a privileged user. To find the process ID of a certain job
use “ps” command.
Examples $ kill 456
Command History
Syntax $ history
Description The command “history” is used to display the history of previous executed
commands. When used without options, it displays the command history list with line
numbers.
Examples $ history
1 pwd
2 date
3 cal
Command Chmod
Syntax $ chmod<permissions><filename>
Description The “chmod” command is used to change permissions of a file after its creation. Only
the Owner or Super User can change file permissions.
Examples $ chmod u+x sample
$ls - l sample
-r w x r --r –sample
Command Chown
Syntax $ chown <new_owner><filename>
Description The command “chown” is used to change the file’s owner and group. Only the
owner can change the major attributes of a file. Sometimes it is necessary to
change the ownership of a file.
Examples $ls -l sample
$ls -l sample
Mon 14:00
Command Pwd
Syntax $ pwd
Description "pwd" stands for print working directory. It displays your current position in the
UNIX filesystem. i.e., the location of the current directory in the directory
sttucture. There are no options (or arguments) with the "pwd" command
Examples $ pwd
Command Cal
Syntax $ cal <month><year>
Description This command is used to print the calendar of a specific month or a specific
year.
Examples $ cal 05 2021
Command Logout
Syntax $ logout
Description This command is used to logout from the shell.
Examples $ logout
Command Shutdown
Syntax $shutdown
Description This command is used to shutting down the system in a safe way.
Examples $ shutdown
EDIT Mode
Letter Action
: r <file> reads a file from disk into the vi editor
: w <file> writes current file to disk
: wq writes the file and quits vi
: q! quits without writing (useful if you've messed up!)
- To add text at the end of the file, position the cursor at the last character of the file.
- The editor accepts and inserts the typed character until Esc key is pressed.
Saving text:
Quitting vi:
Bourne again shell (Bash) is a free Unix shell that can be used in place of the Bourne shell. It
is a complete implementation of the IEEE Portable Operating System Interface for Unix (POSIX)
and Open Group shell specification.
Bash is basically a command processor that typically runs in a text window, allowing the user to
type commands that cause actions. It can read commands from a file, called a script. Like all
Unix shells it supports the following:
Bash was written for the GNU Project by Brian Fox. It is called Bourne again shell for many
reasons, the first being that it is the open-source version of the Bourne shell and the second as a
pun on the concept of being born again. Its acronym is also a description of what the project did,
which was to bash together sh, csh, and ksh features.
A Unix shell is a command-line interpreter that provides users with a basic user interface. It
allows users to communicate with the system through a series of commands that are typed in the
command-line window. There are no buttons or pop-up windows in a shell, simply lots and lots of
text.
Essentially, Bash allows users of Unix-like systems to control the innermost components of
the operating system using text-based commands.
Bash has a number of extensions and runs on Unix-like operating systems like Linux and
Mac OS X. It was ported to Windows through the Subsystem for UNIX-based Applications (SUA)
and by POSIX emulation using Cygwin or MSYS. It can even be used in MS-DOS.
The Bourne shell is also known by its executable program name, "sh" and the dollar
symbol, "$," which is used with command prompts. A Bourne shell interprets and executes user
defined commands and provides command based programming abilities. A Bourne shell enables
the writing and executing of shell scripts, which provide basic program control flow, control over
input/output (I/O) file descriptors and all key features required to create scripts or structured
programs for shell.
A Bourne shell also executes commands and functions that are predefined or integrated;
files that search or follow a command path, as well as text file commands.
The Bourne shell's architectural code was implemented in subsequent versions of UNIX
shell, including Bourne Again shell (Bash), Korn shell and Zsh shell.
Study of C shell
The C shell was created by Bill Joy while he was a graduate student at UC Berkeley in the
late 1970s. It was first released as part of the 2BSD Berkeley Software Distribution of Unix in
1978.
The C shell gets its name from its syntax, which is intended to resemble the C
programming language.
The C shell (csh) is a command shell for Unix-like systems that was originally created as
part of the Berkeley Software Distribution (BSD) in 1978. Csh can be used for entering commands
interactively or in shell scripts. The shell introduced a number of improvements over the earlier
Bourne shell designed for interactive use. These include history, editing operations, a directory
stack, job control and tilde completion. Many of these features were adopted in the Bourne Again
shell (bash), Korn shell (ksh) and in the Z shell (zsh). A modern variant, tcsh, is also very popular.
The C shell introduced features that were intended to make it easier to use interactively at
the command line, though like other shells it is capable of being scripted. One of the most notable
features was command history. Users can recall previous commands they have entered and either
repeat them or edit these commands. Aliases allow users to define short names to be expanded into
longer commands. A directory stack lets users push and pop directories on the stack to jump back
and forth quickly. The C shell also introduced the standard tilde notation where "~" represents a
user's home directory.
Most of these features have been incorporated into later shells, include the Bourne Again
shell, the Korn shell and the Z shell. A popular variant is tsch, which is the current default shell on
BSD systems, as well as on early versions of Mac OS X.
d) Study of Unix/Linux file system (tree structure).
Files in Unix System are organized into multi-level hierarchy structure known as a directory tree.
At the very top of the file system is a directory called “root” which is represented by a “/”. All
other files are “descendants” of root.
/ :The slash / character alone denotes the root of the filesystem tree.
/bin :Stands for “binaries” and contains certain fundamental utilities, such as ls or cp,
which are generally needed by all users.
/boot :Contains all the files that are required for successful booting process.
/dev :Stands for “devices”. Contains file representations of peripheral devices and pseudo-
devices.
/etc :Contains system-wide configuration files and system databases. Originally also
contained “dangerous maintenance utilities” such as init,but these have typically been
moved to /sbin or elsewhere.
/home :Contains the home directories for the users.
/lib :Contains system libraries, and some critical files such as kernel modules or device
drivers.
/media :Default mount point for removable devices, such as USB sticks, media players, etc.
/mnt :Stands for “mount”. Contains filesystem mount points. These are used, for example,
if the system uses multiple hard disks or hard disk partitions. It is also often used for
remote (network) filesystems, CD-ROM/DVD drives, and so on.
/proc :procfs virtual filesystem showing information about processes as files.
/root :The home directory for the superuser “root” – that is, the system administrator. This
account’s home directory is usually on the initial filesystem, and hence not in /home
(which may be a mount point for another filesystem) in case specific maintenance needs to
be performed, during which other filesystems are not available. Such a case could occur,
for example, if a hard disk drive suffers physical failures and cannot be properly mounted.
/tmp :A place for temporary files. Many systems clear this directory upon startup; it might
have tmpfs mounted atop it, in which case its contents do not survive a reboot, or it might
be explicitly cleared by a startup script at boot time.
/usr :Originally the directory holding user home directories,its use has changed. It now
holds executables, libraries, and shared resources that are not system critical, like the X
Window System, KDE, Perl, etc. However, on some Unix systems, some user accounts
may still have a home directory that is a direct subdirectory of /usr, such as the default as
inMinix. (on modern systems, these user accounts are often related to server or system use,
and not directly used by a person).
/usr/bin : This directory stores all binary programs distributed with the operating system
not residing in /bin, /sbin or (rarely) /etc.
/usr/include :Stores the development headers used throughout the system. Header files are
mostly used by the #include directive in C/C++ programming language.
/usr/lib :Stores the required libraries and data files for programs stored within /usr or
elsewhere.
/var :A short for “variable.” A place for files that may change often – especially in size, for
example e-mail sent to users on the system, or process-ID lock files.
/var/log :Contains system log files.
/var/mail :The place where all the incoming mails are stored. Users (other than root) can
access their own mail only. Often, this directory is a symbolic link to /var/spool/mail.
/var/spool :Spool directory. Contains print jobs, mail spools and other queued tasks.
/var/tmp :A place for temporary files which should be preserved between system reboots.
e) Study of .bashrc, /etc/bashrc and Environment variables.
A variable is a character string to which we assign a value. The value assigned could be a number,
text, filename, device, or any other type of data.
For example, first we set a variable TEST and then we access its value using the echo command −
$TEST="Unix Programming"
$echo $TEST
Note that the environment variables are set without using the $ sign but while accessing them
we use the $ sign as prefix. These variables retain their values until we come out of the shell.
When you log in to the system, the shell undergoes a phase called initialization to set up the
environment. This is usually a two-step process that involves the shell reading the following files
−
/etc/profile
profile
As soon as both of these files have been read, the shell displays a prompt −
This is the prompt where you can enter commands in order to have them executed.
Note − The shell initialization process detailed here applies to all Bourne type shells, but some
additional files are used by bash and ksh.
The .profile File
The file /etc/profile is maintained by the system administrator of your Unix machine and
contains shell initialization information required by all users on a system.
The file .profile is under your control. You can add as much shell customization information
as you want to this file. The minimum set of information that you need to configure includes −
You can check your .profile available in your home directory. Open it using the vi editor
and check all the variables set for your environment.
Environment Variables
Following is the partial list of important environment variables. These variables are set
and accessed as mentioned below −
4 LANG expands to the default system locale; LC_ALL can be used to override this. For
example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the
locale to Brazil.
LD_LIBRARY_PATH
5 A Unix system with a dynamic linker, contains a colonseparated list of directories that the
dynamic linker should search for shared objects when building a process image after exec,
before searching in any other directories.
PATH
6
Indicates the search path for commands. It is a colon-separated list of directories in
which the shell looks for commands.
PWD
7
Indicates the current working directory as set by the cd command.
RANDOM
8
Generates a random integer between 0 and 32,767 each time it is referenced.
SHLVL
9
Increments by one each time an instance of bash is started. This variable is useful
for determining whether the built-in exit command ends the current session.
TERM
10
Refers to the display type.
TZ
11
Refers to Time zone. It can take values like GMT, AST, etc.
UID
12
Expands to the numeric user ID of the current user, initialized at the shell startup.
MAIL
13
This variable holds the absolute pathname of the file where user’s mail is
kept. Usually the name of this file is the user’s login name
SHELL
14
This variable contains the name of the users shell program in the form of
absolute pathname. System administrator sets the default shell If
required, user can change it
TERM
15
This variable holds the information regarding the type of the terminal being
used. If TERM is not set properly, utilities like vi editor will not work
$ echo $HOME
/root
$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$
.bashrc
.bashrc file is automatically executed when new terminal(shell) is opened.
Purpose of bashrc file:
You can export environment variables(So there is no need to export environment variable
every time)
You can define aliases
You can provide the path for cross compiler
You can add your own script which can start automatically whenever new shell is opened.
You can change the history length
/etc/bashrc
Like .bash_profile you will also commonly see a .bashrc file in your home directory. This file
is meant for settingcommand aliases and functions used by bash shell users.
Just like the /etc/profile is the system wide version of .bash_profile. The /etc/bashrc for Red
Hat and /etc/[Link] in Ubuntu is the system wide version of .bashrc.
Interestingly enough in the Red Hat implementation the /etc/bashrc also executes the shell scripts
within /etc/profile.d but only if the users shell is a Interactive Shell (aka Login Shell)