Monday, January 22, 2018

Unix/Linux Commands 1

1. Common Shells on UNIX System

Following are the common shells used on UNIX System:

• Bourn Shell (/bin/sh)
• Bash Shell (/bin/bash)
• C Shell (/bin/csh)
• Korn Shell (/bin/ksh)

2. UNIX Commands

Following are the frequently used UNIX shell commands:

The ls command

Description:

This command lists information about the files (the current directory by default).

Options:

Generally used options with ls command are:

• -l : use a long listing format
• -t : sort by modification time
• -r : reverse order while sorting
• -s : with –l, print size of each file, in blocks
• -a : do not ignore entries starting with.

Synopsis:

ls [OPTION]… [FILE]…

Example:

• ls -ltr #This command displays all the files in the current directory in long listing format and reverse sorted on modification time.

Scenario:

• List the files along with size access permissions for the files.

The pwd command

Description:

The pwd utility writes the absolute pathname of the current working directory to the standard output.

Options:

-L Display the logical current working directory.
-P Display the physical current working directory (all symbolic links resolved).

Synopsis:

pwd [Option]

Example:

• Pwd

/home/andy

Scenario:

• Display the current directory.

The mkdir

Description:

Create the DIRECTORY(ies), if they do not already exist.

Options:

• -p : used to create subdirectories.

Synopsis:

Mkdir [OPTION] DIRECTORY…

Example:

• Mkdir andy

Scenario:

• Create the directory.

The cd command

Description:

Change the current directory.

Options:

• None

Synopsis:

Cd DIRECTORY PATH…

Example:

•cd andy

Scenario:

• Change the current directory to the required path.

The rm command

Description:

The rm command removes each specified file. By default, it does not remove directories.

Options:

• -f, --force ignore nonexistent files, never prompt.
• -I, --interactive prompt before any removal.
• -r, -R –recursive remove the contents of directories recursively.

Synopsis:

rm [OPTION]... FILE…

Example:

• rm andy

Scenario:

• Remove the directory along with its contents.

The cat command

Description:

Concatenate FILE(s), or standard input, to standard output.

Options:

• -n, number all output lines.

Synopsis:

Cat [OPTION] [FILE]…

Example:

• cat test

Scenario:

• Display the contents of the file test.

The cp command

Description:

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

Options:

• -f, --force : if an existing destination file cannot be opened, remove it

And try again

• -i, --interactive : prompt before overwrite

Synopsis:

cp [OPTION]… SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY

Example:

• cp test test2

Scenario:

• Copy the content of one file to another file

The mv command

Description:

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Option:

• -f, do not prompt before overwriting (equivalent to –reply=yes)
• -I, prompt before overwrite (equivalent to –reply=query)

Synopsis:

mv [OPTION]… SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
Example:

• mv cptest1 mvtest

Scenario:

• Rename or moving of the particular file to another file or folder.

The echo command

Description:

Echo the STRING(s) to standard output.

Options:

• -n do not output the trailing newline
• -e enable interpretation of the backslash-escaped characters listed below
• -E disable interpretation of those sequence in STRINGs
• \\ backslash
• \a alert(BEL)
• \b backspace
• \c suppress trailing newline
• \f form feed
• \n new line
• \r carriage return
• \t horizontal tab
• \v vertical tab

Synopsis:

echo [OPTION]… [STRING]…

Example:

• echo “Welcome to Ramu Technologies \n \t Hyderabad”

Scenario:

• Display the text to the standard output or display the content of the shell variables.

The head command

Description:

Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.

Options:

• -n, print the first N lines instead of the first 10

Synopsis:

head [OPTION]… [FILE]…

Example:

• head –n5 ramu.txt

Scenario:

• Print the first N lines or bytes of the file.

The tail command

Description:

Print the last 10 lines of each FILE to standard output.With more than one FILE, precede each with a header giving the file name.With no FILE, or when FILE is -, read standard input.

Options:

• -n, print the first N lines instead of the first 10

Synopsis:

Tail [OPTION]… [FILE]…

Example:

• Tail –n5 ramu.txt

Scenario:

• Print the last N lines or bytes of the file.

The wc command

Description:

Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input.

Options:

• -c, --bytes Print the byte counts
• -m, --chars Print the character counts
• -l, --lines Print the newline counts
• -w, --words Print the word counts
• -L, --max-line-length Print the length of the longest line

Synopsis:

wc [OPTION]… [FILE]…

Example:

• wc –l ramu.txt

Scenario:

• Count the number of characters, words and lines in the file.

The date command

Description:

Display the current time in the given FORMAT, or set the system date.

Format Strings:

• %C century(year divided by 100 and truncated to an integer) [00-99]
• %d day of month (01 .. 31)
• %D date (mm/dd/yy)
• %e day of month, blank padded (1 .. 31)
• %F same as %Y-%m-%d
• %H hour (00 .. 23)
• %I hour (01 .. 12)
• %J day of year (001 .. 366)
• %k hour (0 .. 23)
• %l hour (1 .. 12)
• %m month (01 .. 12)
• %M minute(00 .. 59)
• %n a newline
• %N nanoseconds (000000000 .. 999999999)
• %r time, 12-hour (hh:mm:ss [AP]M)
• %R time, 24-hour(hh:mm)
• %S second (00 ..66); the 60 is necessary to accommodate a leap second
• %T time, 24-hour (hh:mm:ss)
• %u day of week (1 ..7); 1 represents Monday
• %w day of week (0 .. 6); 0 represents Sunday
• %y last two digits of year (00 .. 99)
• %Y year (1970…)
• %Z time zone (e.g., EDT), or nothing if no time zone is determinable

Synopsis:

Date [OPTION]… [+FORMAT]

Example:

• Echo ‘date +%D’

Scenario:

• Print the date or get the date value into the variable.

The time command

Description:

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard output giving timing statistics about this program run.

Synopsis:

time [options] command [arguments…]

Example:

• Time ls –l

Scenario:

• Calculate the elapsed time for the command

The grep command

Description:

grep searches the named input FILEs (or standard input if no files are named, or the file name – is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

Options:

• -i, Ignore case distinctions in both the PATTERN and the input files.

Synopsis:

• grep [options] PATTERN [FILE…]

Scenario:

• Find out the string in the contents of list of files.


The who command

Description:

This command shows information about who is logged on.

Options:

• -a, --all
• -b, --boot time of last system boot
• -d, --dead print dead processes
• -H, --heading print line of column headings
• -i, --idle add idle time as HOURS:MINUTES, . or old (deprecated, use -u)

Synopsis:

Who [ OPTION]… [FILE | ARG1 ARG2]

Example:

• Who –a

Scenario:

• Find out the list of users connected to the server.

The tee command

Description:

The output that is being redirected to a file can also be viewed on standard output.

Options:

• -a, append to the given FILEs, do not overwrite

Synopsis:

• Tee [OPTION]… [FILE]…

Example:

• echo “hello world” tee ramu.txt

Scenario:

• Keep track of the details in log file.

The expr command

Description:

Print the value of EXPRESSION to standard output. A blank line below separates increasing precedence groups.

Synopsis:
expr EXPRESSION
expr OPTION

Usage:

• ARG1 | ARG2 : ARG1 if it is neither null nor 0, otherwise ARG2
• ARG1 & ARG2 : ARG1 if neither arguments is null or 0, otherwise 0
• ARG1 < ARG2 : ARG1 is less than ARG2 • ARG1 <= ARG2 : ARG1 is less than or equal to ARG2 • ARG1 = ARG2 : ARG1 is equal to ARG2 • ARG1 != ARG2 : ARG1 is unequal to ARG2 • ARG1 >= ARG2 : ARG1 is greater than or equal to ARG2
• ARG1 > ARG2 : ARG1 is greater than ARG2
• ARG1 + ARG2 : arithmetic sum of ARG1 and ARG2
• ARG1 – ARG2 : arithmetic difference of ARG1 and ARG2
• ARG1 * ARG2 : arithmetic product of ARG1 and ARG2
• ARG1 / ARG2 : arithmetic quotient of ARG1 divided by ARG2
• ARG1 % ARG2 : arithmetic remainder of ARG1 divided by ARG2

Example:

• expr 10 + 5
• expr ‘expr 20 – 2’

The ps command

Description:
ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.

Options:

• -a : all
• -u : information about the owner of the process
• -x : excluding terminal all other processes

Synopsis:

ps [options]

Example:

• ps –e
• ps –ef

Scenario:

• List the processes which are running on the UNIX server.

The exit command

Description:

This command is used to stop the execution of a shell script. And this command has return value for the parent shell.

Synopsis:

exit [n]

Example:

• exit 0
• exit 4

The cut command

Description:

Print selected parts of lines from each FILE to standard output.

Options:

• -b, --bytes=LIST output only these bytes
• -c, --characters=LIST output only these characters
• -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter
• -f, --fields=LIST output only these fields

Synopsis:

cut [OPTION]… [FILE]…

Example:

• cut –c3-8 ramu.txt
• cut –d: -f2 ramu.txt

Scenario:

• Display the part of the string in the data file.

The diff command

Description:

In the simplest case, diff compares the contents of the two files from-file and to-file. A file name of stands for text read from the standard input.

Options:

• -a, Treat all files as text and compare them line-by-line, even if they do not seem to be text.
• -b, Ignore changes in amount of white space.
• -c, Use the context output format.

Synopsis:

Diff [options] from-file to-file

Example:

• Diff –a file1 file2

Scenario:

• Find the differences to two files.

The man command

Description:

The “man” formats and displays the on-line manual pages. If you specify section, man only looks in that section of the manual. The “name” is normally the name of the manual page, which is typically the name of a command, function, or file.

Synopsis:

Man [-acdfFhkKtwW] [--path] [-m system] [-p string] [-C config_file] [-M pathlist] [-P pager] [-S sec-tion_list] [section] name …

Example:

• man ls

Scenario:

• To get the help on any of the UNIX command.

The df command

Description:

The df command displays the amount of disk space available on the filesystem containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown.

Options:

• -a, --all include filesystems having 0 blocks
• -B, --block-size=SIZE use SIZE-byte blocks
• -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
• -H, --si likewise, but use powers of 1000 not 1024
• -i, --inodes list inode information instead of block usage
• -k, like --block-size=1K
•-l, --local limit listing to local file systems

Synopsis:

df [OPTION]… [FILE]…

Example:

• df –a ramu

Scenario:

• Check the free space on the disk.

The sed command

Description:

The sed is a stream editor. A stream editor is use to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.

Options:

• s/regexp/replacement/
Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.

Synopsis:

sed [OPTION]… {script-only-if-no-other-script} [input-file]

Example:

• sed ‘s/old_dt/new_dt/g’ filename

The mail command

Description:

The mail utility provides a message sending and receiving facility. It has two major modes, selected by the options used: Send Mode and Receive Mode.

Synopsis:

Send Mode:

Mail [-s subject] address…

Receive Mode:

Mail

Example:

• mail –s “hello” [username] The tr command

Description:

Translate characters from standard input, writing to standard output.

Options:

• char1-char2

All characters from char1 to char2 in ascending order

Synopsis:

• tr [OPTION]… SET1 [SET2]

SETs are specified as strings of characters

Example:

• tr ‘a-z’ ‘A-Z’ < [filename] Scenario: • Change the lower cases letters to capital letters in a file. The find command

Description:

Searches for the specified files inside the given directory hierarchy

Options:

• Name (matches the exact filename)
• -iname (ignores the cases)
• -user (searches the files which belongs to user)
• -uid (searches the files which belongs to that uid match)

Synopsis:

• find [path] [expression]

Example:

• find /etc –name passwd
• find /etc –name passWd
• find /home –user andy
• find /home –uid 501

scenario:

• search for the file by name passwd in /etc directory

The ln command

Description:
Used to link files.
Create link to specified Target with optional Link_Name. If the Link_Name is omitted, a link with the same basename as the Target is created in the current working directory. When using with following second form with more than one target, the last argument must be a directory. Two kinds of links, hardlinking and softlinking.

Options:

• -s (Used for softlinking)

Synopsis:

• ln [options] … Target [Link_Name]
• ln [options] … Target … Directory

Example:

• ln file1 file2
• ln –s /etc lfile

Scenario:

• link the bin directory to filename called lbin



No comments: