Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Wednesday, March 19, 2014

Useful Scripts for Unix

CAT

The cat command reads one or more files and prints them to standard output. The operator > can be used to combine multiple files into one. The operator>> can be used to append to an existing file.

SYNTAX
The syntax for the cat command is:
cat [options] [files]

OPTIONS
Option
Description
-e
$ is printed at the end of each line. This option must be used with -v.
-s
Suppress messages pertaining to files that do not exist.
-t
Each tab will display as ^I and each form feed will display as ^L. This option must be used with -v.
-u
Output is printed as unbuffered.
-v
Display control characters and nonprinting characters

EXAMPLE
cat file1
cat file1 file2 > all
cat file1 >> file2

MAIL

The mail command allows you to read or send mail. If users is left blank, it allows you to read mail. If usershas a value, then it allows you send mail to those users.

SYNTAX
The syntax for the mail command is:
mail [options] [users]

OPTIONS FOR READING MAIL


Option
Description
-e
Check if mail exists. Exit status is 0 if mail exists and 1 if mail does not exist.
-f file
Read mail from mailbox called file.
-F names
Forward mail to names.
-h
Displays messages in a window.
-i
Ignore interrupts
-p
Displays all messages.
-P
Displays all messages with header lines.
-q
Terminate when an interrupt occurs.
-r
Displays oldest messages first.
-U
Convert uucp-type addresses to Internet format.
-v
Verbose.
OPTIONS FOR SENDING MAIL
Option
Description
-m type
Display a "Message-type:" line at the heading of the message, followed by type.
-t
Display a "To:" line at the heading of the message with the names of the recipients.
-w
Send mail to users without waiting for a remote transfer program to finish.
-F
Save message in a file called the name of the first recipient.
-h n
If message has not been sent after n network connections, do not send message.
-i
Ignore interrupts
-r address
address is the return address for mail messages.
-s subject
Displays subject in the subject header.
-U
Convert uucp-type addresses to Internet format.
-v
Verbose.


MAN

The man command displays the online manual pages.

SYNTAX
The syntax for the man command is:
man [options] [ [section] subjects]

OPTIONS
Option
Description
-
Output is piped through the more command.
-d
Debug.
-F
Search the MANPATH directories.
-f files
Display a summary (one line) for each file.
-k keywords
Displays the header lines that contain any of the keywords.
-M path
Searchs in the path instead of the MANPATH directories.
-t
Format the pages with troff.
-T mac
Display using mac.

EXAMPLE
man -f tech


TAIL
The tail command displays the last ten lines of the file.

SYNTAX
The syntax for the tail command is:
tail [options] [file]

OPTIONS

Option
Description
-f
Follow the file as it grows.
-r
Displays the lines in the reverse order.
-n[k]
Displays the file at the nth item from the end of the file.
+n[k]
Displays the file at the nth item from the beginning of the file.

EXAMPLE
tail -r tech

FTP

The ftp command allows you to transfer files to and from a remote server.

SYNTAX
The syntax for the ftp command is:
ftp [options] [hostname]

OPTIONS

Option
Description
-d
Debugging is enabled.
-g
Filename globbing is disabled.
-i
Interactive prompting is disabled.
-n
When you are initially connecting, auto-login is disabled.
-v
Display all responses from the server.

EXAMPLE
ftp tech
ftp -v tech

MORE

The more command displays the file called namein the screen. The RETURN key displays the next line of the file. The spacebar displays the next screen of the file.

SYNTAX
The syntax for the more command is:
more [options] [files]

OPTIONS

Option
Description
-c
Page through the file by clearing the window. (not scrolling).
-d
Displays "Press space to continue, 'q' to quit"
-f
Count logical lines rather than screen lines (wrapping text)
-l
Ignores form feed (^L) characters.
-r
Display all control characters.
-s
Displays multiple blank lines as one blank line.
-u
Does not display underline characters and backspace (^H).
-w
Waits for a user to press a key before exiting.
-n
Displays n lines per window.
+num
Displays the file starting at line number num.
+/pattern
Displays the file starting at two lines before the pattern.

EXAMPLE
more -d tech


GREP

The grep command allows you to search one file or multiple files for lines that contain a pattern. Exit status is 0 if matches were found, 1 if no matches were found, and 2 if errors occurred.

SYNTAX
The syntax for the grep command is:
grep [options] pattern [files]

OPTIONS

Option
Description
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.

EXAMPLE
grep -c tech file1
PS
The ps command displays active processes.

SYNTAX
The syntax for the ps command is:
ps [options]

OPTIONS

Option
Description
-a
Displays all processes on a terminal, with the exception of group leaders.
-c
Displays scheduler data.
-d
Displays all processes with the exception of session leaders.
-e
Displays all processes.
-f
Displays a full listing.
-glist
Displays data for the list of group leader IDs.
-j
Displays the process group ID and session ID.
-l
Displays a long listing
-plist
Displays data for the list of process IDs.
-slist
Displays data for the list of session leader IDs.
-tlist
Displays data for the list of terminals.
-ulist
Displays data for the list of usernames.


EXAMPLE
ps -ef
ps -aux



Hopefully this has helped some of you out and I’d appreciate any comments you have.

Wednesday, July 17, 2013

Important Unix Scripts

1.  Script To Find the Line Numbers In a Files For a Particular String

Syntax :- sed -n '/String_to_search/=' file_name.txt

Ex:  sed -n '/LAST_UPDATE_DATE/=' XX_CONC_PROG.txt

2. Script To Change/replace the Line in a File
The Script is used to replace a line in a File for a Particular string in the Line

Syntax : sed -i '/String_in_a_line/ c\New_String_to_be_added ' XX_FILE_NAME.txt

 Ex: sed -i '/LAST_UPDATE_DATE =/ c\LAST_UPDATE_DATE = "2013/07/17"' XX_FILE_NAME.txt

3. Script To replace a String in a File
The script is used to replace a String in the File.

Syntax: sed -i 's/String to Stearch/string_to_replaced/g' file_name.txt

Ex : sed -i 's/LAST_UPDATE_DATE =/LAST_UPDATE_DATE=SYS/g' XX_FILE_NAME.txt