Vous êtes sur la page 1sur 39

UNIX COMMANDS

In Unix, how do I combine several text files into a single file?


To combine several text files into a single file in Unix, use the cat command: cat file1 file2 file3 > newfile Replace file1, file2, and file3 with the names of the files you wish to combine, in the order you want them to appear in the combined document. Replace newfile with a name for your newly combined single file. If you want to add one or more files to an existing document, use the format: cat file1 file2 file3 >> destfile This command will add file1, file2, and file3 (in that order) to the end of destfile. Note: If you use > instead of >>, you will overwrite destfile rather than add to it Examples of the unix join command Given the files: :::::::::::::: 1.txt :::::::::::::: 1 abc 2 lmn 3 pqr :::::::::::::: 2.txt :::::::::::::: 1 abc

3 lmn 9 opq if we type: join 1.txt 2.txt We get 1 abc abc 3 pqr lmn since the two files are trying to match on the 1st column, and both have a 1 and a 3 in that column. To tell join to use the second column to join with, we type: join -1 2 -2 2 1.txt 2.txt (where "-1 2" stands for the 2nd filed of the 1st file, and "-2 2" stands for the 2nd field of the 2nd file) we get: abc 1 1 lmn 2 3 which are just those fields that match in both files. This is refered to as an inner join. Inner joins look for rows that match rows in the other table. The problem with inner joins is that only rows that match between tables are returned. If we type: join -a1 -1 2 -2 2 1.txt 2.txt (where "-a1" says include all the records from the first file) we get: abc 1 1 lmn 2 3 pqr 3 this is missing a number for pqr for the second file (since there is no pqr in that file) and is missing "9 opq" from the second file. This is an example of a left outer join, called such because it includes all of the rows from the left (or first) file specified. If we type:

join -a1 -a2 -1 2 -2 2 1.txt 2.txt (adding "-a2" to tell join to also include all records from the second file) we get: abc 1 1 lmn 2 3 opq 9 pqr 3 Which has all of the records. This is an example of a full outer join since it has all of the rows from both files. (Missing from these examples is the case where we had the -a2 without the -a1. That would have produced a right outer join which contained all of the records from the second (right) file and only those rows from the first (left) file that matched). While this example has all of the rows from both files, we still have a problem since we can not tell which file the count is for on the last two records. We now need to format the output using the "-o" command. The options for "-o" are as follows: A "0" (that.s a zero) means display the join field A number in the format of X.Y means to display the Y field from the X file (ex 2.1 means display the first field from the second file). -e "0" says to replace any missing data fields with whatever is in-between the quotes (in this case a zero). So if we type: join -a1 -a2 -1 2 -2 2 -o 0 1.1 2.1 -e "0" 1.txt 2.txt we get: abc 1 1 lmn 2 3 opq 0 9 pqr 3 0 which has all of the records, and fills in the zeros for us.

About join The join command forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. Syntax join [-a filenumber | -v filenumber ] [ -1 fieldnumber ] [ -2 fieldnumber ] [ -o list ] [ -e string ] [ -t char ] file1 file2 join [ -a filenumber ] [ -j fieldnumber ] [-j1 fieldnumber ] [ -j2 fieldnumber ] [ -o list ] [e string ] [ -t char ] file1 file2 -a filenumber In addition to the normal output, produce a line for each unpairable line in file filenumber, where filenumber is 1 or 2. If both -a 1 and -a 2 are specified, all unpairable lines will be output. Instead of the default output, produce a line only for each unpairable line in filenumber, where filenumber is 1 or 2. If both -v 1 and -v 2 are specified, all unpairable lines will be output. Join on the fieldnumberth field of file 1 . Fields are decimal integers starting with 1. Join on the fieldnumberth field of file 2. Fields are decimal integers starting with 1. Equivalent to -1fieldnumber -2fieldnumber. Equivalent to -1fieldnumber. Equivalent to -2fieldnumber Fields are numbered starting with 1. Each output line includes the fields specified in list. Fields selected by list that do not appear in the input will be treated as empty output fields. (See the -e option.) Each element of which has the either the form filenumber.fieldnumber, or 0, which represents the join field. The common field is not printed unless specifically requested. Replace empty output fields with string. Use character char as a separator. Every appearance of char in a line is significant. The character char is used as the field separator for both input and output. With this option specified, the collating term should be the same as sort without the -b option. File name and/or Directory and file name of the files being joined

-v filenumber

-l fieldnumber -2 fieldnumber -j fieldnumber -j1 fieldnumber -j2 fieldnumber -o list

-e string -t char

file1 file2

Use grep to search file


Search /etc/passwd for boo user:
$ grep boo /etc/passwd

You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
$ grep -i "boo" /etc/passwd

Use grep recursively


You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/

Use grep to search words only


When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" /path/to/file

Use grep to search 2 different words


use egrep as follows:
$ egrep -w 'word1|word2' /path/to/file

Count line when words has been matched


grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file

Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'word' /path/to/file

SEARCHING FILE FROM DIR Find all perl (*.pl) files in current directory:
$ find . -name '*.pl'

The . represent the current directory and the -name option specifies all pl (perl) files. The quotes avoid the shell expansion and it is necessary when you want to use wild card based search (without quotes the shell would replace *.pl with the list of files in the current directory).

To list only files and avoid all directories


$ find . -type f -name '*.pl'

Above command will only list files and will exclude directories, special files, pipes, symbolic links etc.

Search all directories


Search file called httpd.conf in all directories:
$ find / -type f -name httpd.conf

Generally this is a bad idea to look for files. This can take a considerable amount of time. It is recommended that you specify the directory name. For example look httpd.conf in /usr/local directory:
$ find /usr/local -type f -name httpd.conf

Execute command on all files


Run ls -l command on all *.c files to get extended information :
$ find . -name "*.c" -type f -exec ls -l {} \;

You can run almost all UNIX command on file. For example, modify all permissions of all files to 0700 only in ~/code directory:
$ find ~/code -exec chmod 0700 {} \;

Search for all files owned by a user called payal:


$ find . -user $ find . -user payal

In Unix, how do I list the files in a directory?


You can use the ls command to list the files in any directory to which you have access. For a simple directory listing, at the Unix prompt, enter: ls This command will list the names of all the files and directories in the current working directory.

You can limit the files that are described by using fragments of filenames and wildcards. Examples of this are:
ls hello ls hel* ls hell?

Lists files whose complete name is hello; if hello is a directory, displays the contents of the hello directory. Lists all files in the directory that begin with the characters hel (e.g., files named hel, hello, and hello.officer). Lists files that begin with hell followed by one character, such as helli, hello, and hell1.

The * represents any number of unknown characters, while ? represents only one unknown character. You can use * and ? anywhere in the filename fragment. If you would like to list files in another directory, use the ls command along with the path to the directory. For example, if you are in your home directory and want to list the contents of the /etc directory, enter: ls /etc This will list the contents of the /etc directory in columns.

Several options control the way in which the information you get is displayed. Options are used in this format: ls -option filename Neither the options nor the filename are required (you may use ls by itself to see all the files in a directory). You may have multiple options and multiple filenames on a line. The options available with ls are far too numerous to list here, but you can see them all in the online manual (man) pages. Some of the more helpful options for ls are:
a d F l R

Shows all files, including those beginning with . (a period). The dot is special in the Unix file system. Shows directory names, but not contents Marks special files with symbols to indicate what they are: / for directories, @ for symbolic links, * for executable programs Shows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (The l stands for "long".) Recursively lists subdirectories

The options can be combined. To list all the files in a directory in the long format, with marks for the types of files, you would enter: ls -Flg

As with many other Unix commands, you can redirect the output from ls to a file, or pipe it to another command. If you want to save a list of the files in your directory to a file named foo, you would use the following command combination: ls > foo

If you want to mail a list of the files in your directory to a user named tom, you would use the following combination: ls | Mail tom

For a more complete discussion of the ls command, see the online manual pages. At the Unix prompt, enter: man ls -aShows you all files, even files that are hidden (these files begin with a dot.) -AList all files including the hidden files. However, does not display the working directory (.) or the parent directory (..). -bForce printing of non-printable characters to be in octal \ddd notation. -cUse time of last modification of the i-node (file created, mode changed, and so forth) for sorting (-t) or printing (-l or -n). -CMulti-column output with entries sorted down the columns. Generally this is the default option. -dIf an argument is a directory it only lists its name not its contents. -fForce each argument to be interpreted as a directory and list the name found in each slot. This option turns off -l, -t, -s, and -r, and turns on -a; the order is the order in which entries appear in the directory. -FMark directories with a trailing slash (/), doors with a trailing greater-than sign (>), executable files with a trailing asterisk (*), FIFOs with a trailing vertical bar (|), symbolic links with a trailing at-sign (@), and AF_Unix address family sockets with a trailing equals sign (=). -gSame as -l except the owner is not printed. -iFor each file, print the i-node number in the first column of the report. -lShows you huge amounts of information (permissions, owners, size, and when last modified.) -LIf an argument is a symbolic link, list the file or directory the link references rather than the link itself. -mStream output format; files are listed across the page, separated by commas. -nThe same as -l, except that the owner's UID and group's GID numbers are printed, rather than the associated character strings. -oThe same as -l, except that the group is not printed. -pDisplays a slash ( / ) in front of all directories. -qForce printing of non-printable characters in file names as the character question mark (?). -rReverses the order of how the files are displayed. -RIncludes the contents of subdirectories. -sGive size in blocks, including indirect blocks, for each entry. -tShows you the files in modification time. -uUse time of last access instead of last modification for sorting (with the -t option) or printing (with the -l option). -xDisplays files in columns. -1Print one entry per line of output. pathnamesFile or directory to list.

isplay actual directory location


Use -P option to display the physical current working directory (all symbolic links resolved). For example, /home/lighttpd is /var/www/root/lighttpd:
cd /home/lighttpd pwd

Output:
/home/lighttpd

Now run with -P option


pwd -P

Pipes and Filters


The purpose of this lesson is to introduce you to the way that you can construct powerful Unix command lines by combining Unix commands.

Concepts
Unix commands alone are powerful, but when you combine them together, you can accomplish complex tasks with ease. The way you combine Unix commands is through using pipes and filters.

Using a Pipe
The symbol | is the Unix pipe symbol that is used on the command line. What it means is that the standard output of the command to the left of the pipe gets sent as standard input of the command to the right of the pipe. Note that this functions a lot like the > symbol used to redirect the standard output of a command to a file. However, the pipe is different because it is used to pass the output of a command to another command, not a file. Here is an example:
$ cat apple.txt core worm seed jewel $ cat apple.txt | wc 3 4 21 $

In this example, at the first shell prompt, I show the contents of the file apple.txt to you. In the next shell prompt, I use the cat command to display the contents of the apple.txt file, but I sent the display not to the screen, but through a pipe to the wc (word count) command. The wc command then does its job and counts the lines, words, and characters of what it got as input. You can combine many commands with pipes on a single command line. Here's an example where I count the characters, words, and lines of the apple.txt file, then mail the results to nobody@december.com with the subject line "The count."
$ cat apple.txt | wc | mail -s "The count" nobody@december.com

Using a Filter
A filter is a Unix command that does some manipulation of the text of a file. Two of the most powerful and popular Unix filters are the sed and awk commands. Both of these commands are extremely powerful and complex.

sed
Here is a simple way to use the sed command to manipulate the contents of the apple.txt file:
$ cat apple.txt core worm seed jewel $ cat apple.txt | sed -e "s/e/WWW/" corWWW worm sWWWed jWWWwel $ cat apple.txt | sed -e "s/e/J/g" corJ worm sJJd jJwJl $

Unix commands
Misc commands Shell and other programming File management comparison and searching Text processing System status communications Storage commands

Misc commands
man,banner,cal, calendar,clear,nohup, tty .

Man ual command.


man man This is help command, and will explains you about online manual pages you can also use man in conjunction with any command to learn more about that command for example.

man ls will explain about the ls command and how you can use it. man -k pattern command will search for the pattern in given command.

Banner command.
banner prints characters in a sort of ascii art poster, for example to print wait in big letters. I will type banner wait at unix command line or in my script. This is how it will look.
# # # # ## # # # # ## # # ###### # # # # # # ##### # # #

## #

## #

# #

# #

# #

# #

Cal command
cal command will print the calander on current month by default. If you want to print calander of august of 1965. That's eightht month of 1965. cal 8 1965 will print following results.
August 1965 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Clear command
clear command clears the screen and puts cursor at beginning of first line.

Calendar command
calendar command reads your calendar file and displays only lines with current day. For example in your calendar file if you have this
12/20 1/15 1/20 Test new software. Test newly developed 3270 product. Install memory on HP 9000 machine.

On dec 20th the first line will be displayed. you can use this command with your crontab file or in your login files.

Nohup command.
nohup command if added in front of any command will continue running the command or process even if you shut down your terminal or close your session to machine. For exmaple, if I want to run a job that takes lot of time and must be run from terminal and is called update_entries_tonight . nohup update_entries_tonight will run the job even if terminal is shut down in middle of this job.

Tty command
Tty command will display your terminal. Syntax is tty options

Options -l will print the synchronous line number. -s will return only the codes: 0 (a terminal), 1 (not a terminal), 2 (invalid options) (good for scripts)

back to top of misc commands back to top of page

File Management commands.


cat,cd, cp, file,head,tail, ln,ls,mkdir ,more,mv, pwd, rcp,rm, rmdir, wc.

Pwd command.
pwd command will print your home directory on screen, pwd means print working directory.
/u0/ssb/sandeep

is output for the command when I use pwd in /u0/ssb/sandeep directory.

Ls command
ls command is most widely used command and it displays the contents of directory.

options

ls will list all the files in your home directory, this command has many options. ls -l will list all the file names, permissions, group, etc in long format. ls -a will list all the files including hidden files that start with . . ls -lt will list all files names based on the time of creation, newer files bring first. ls -Fxwill list files and directory names will be followed by slash. ls -Rwill lists all the files and files in the all the directories, recursively. ls -R | more will list all the files and files in all the directories, one page at a time.

Mkdir command.
mkdir sandeep will create new directory, i.e. here sandeep directory is created.

Cd command.
cd sandeep will change directory from current directory to sandeep directory. Use pwd to check your current directory and ls to see if sandeep directory is there or not. You can then use cd sandeep to change the directory to this new directory.

Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).

Head command.
head filename by default will display the first 10 lines of a file. If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and so forth.

Tail command.
tail filename by default will display the last 10 lines of a file. If you want last 50 lines then you can use tail -50 filename.

More command. more command will display a page at a time and then wait for input
which is spacebar. For example if you have a file which is 500 lines and you want to read it all. So you can use more filename

Wc command
wc command counts the characters, words or lines in a file depending upon the option.

Options

wc -l filename will print total number of lines in a file. wc -w filename will print total number of words in a file. wc -c filename will print total number of characters in a file.

File command.
File command displays about the contents of a given file, whether it is a text (Ascii) or binary file. To use it type file filename. For example I have cal.txt which has ascii characters about calander of current month and I have resume1.doc file which is a binariy file in microsoft word. I will get file resume.doc
resume1.doc: data ascii text

file cal.txt
cal.txt:

Cp command.
cp command copies a file. If I want to copy a file named oldfile in a current directory to a file named newfile in a current directory. cp oldfile newfile If I want to copy oldfile to other directory for example /tmp then cp oldfile /tmp/newfile. Useful options available with cp are -p and -r . -p options preserves the modification time and permissions, -r recursively copy a directory and its files, duplicating the tree structure.

Rcp command.
rcp command will copy files between two unix systems and works just like cp command (-p and -i options too). For example you are on a unix system that is called Cheetah and want to copy a file which is in current directory to a system that is called lion in /usr/john/ directory then you can use rcp command rcp filename lion:/usr/john You will also need permissions between the two machines. For more infor type man rcp at command line.

Mv command.
mv command is used to move a file from one directory to another directory or to rename a file.

Some examples:

mv oldfile newfile will rename oldfile to newfile. mv -i oldfile newfile for confirmation prompt. mv -f oldfile newfile will force the rename even if target file exists. mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory.

Ln command.
Instead of copying you can also make links to existing files using ln command. If you want to create a link to a file called coolfile in /usr/local/bin directory then you can enter this command. ln mycoolfile /usr/local/bin/coolfile

Some examples:

ln -s fileone filetwo will create a symbolic link and can exist across machines. ln -n option will not overwrite existing files. ln -f will force the link to occur.

Rm command.
To delete files use rm command.

Options:

rm oldfile will delete file named oldfile. rm -f option will remove write-protected files without prompting. rm -r option will delete the entire directory as well as all the subdirectories, very dangerous command.

Rmdir command.
rmdir command will remove directory or directories if a directory is empty.

Options:

rm -r directory_name will remove all files even if directory is not empty. rmdir sandeep is how you use it to remove sandeep directory. rmdir -p will remove directories and any parent directories that are empty. rmdir -s will suppress standard error messages caused by -p.

back to top of File management commands back to top of page

Comparison and Searching


diff,dircmp, cmp, grep, find.

Diff command.
diff command will compare the two files and print out the differences between. Here I have two ascii text files. fileone and file two. Contents of fileone are
This this this this this is is is is is first file second line third line different as;lkdjf not different

filetwo contains
This this this this is is is is

first file second line third line different

xxxxxxxas;lkdjf

this is not different

diff fileone filetwo will give following output


4c4 < this is different --> this is different as;lkdjf xxxxxxxas;lkdjf

Cmp command.
cmp command compares the two files. For exmaple I have two different files fileone and filetwo. cmp fileone filetwo will give me
fileone filetwo differ: char 80, line 4

if I run cmp command on similar files nothing is returned. -s command can be used to return exit codes. i.e. return 0 if files are identical, 1 if files are different, 2 if files are inaccessible. This following command prints a message 'no changes' if files are same cmp -s fileone file1 && echo 'no changes' .
no changes

Dircmp Command.
dircmp command compares two directories. If i have two directories in my home directory named dirone and dirtwo and each has 5-10 files in it. Then dircmp dirone dirtwo will return this
Dec 9 16:06 1997 dirone only and dirtwo only Page 1 ./fourth.txt ./rmt.txt ./te.txt ./third.txt

./cal.txt ./dohazaar.txt ./four.txt ./junk.txt ./test.txt

Grep Command
grep command is the most useful search command. You can use it to find processes running on system, to find a pattern in a file, etc. It can be used to search one or more files to match an expression. It can also be used in conjunction with other commands as in this following example, output of ps command is passed to grep command, here it means search all processes in system and find the pattern sleep. ps -ef | grep sleep will display all the sleep processes running in the system as follows.
ops 12964 25853 dxi 12974 15640 0 16:12:24 ttyAE/AAES 0 16:12:25 ttyAH/AAHP 0:00 sleep 60 0:00 sleep 60

ops ops ops dxi ops dxi ops ops ssb pjk

12941 12847 12894 13067 13046 12956 12965 12989 13069 27049

25688 25812 25834 27253 25761 13078 25737 25778 26758 3353

0 0 0 2 0 0 0 0 2 0

16:12:21 16:11:59 16:12:12 16:12:48 16:12:44 16:12:23 16:12:24 16:12:28 16:12:49 15:20:23

ttyAE/AAEt ttyAH/AAH6 ttyAE/AAEX ttyAE/ABEY ttyAE/AAE0 ttyAG/AAG+ ttyAE/AAEp ttyAH/AAHv ttyAH/AAHs ?

0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00

sleep 60 sleep 60 sleep 60 sleep 1 sleep 60 sleep 60 sleep 60 sleep 60 grep sleep sleep 3600

Options:
-b option will precede each line with its block number. -c option will only print the count of matched lines. -i ignores uppercase and lowercase distinctions. -l lists filenames but not matched lines.

other associated commands with grep are egrep and fgrep. egrep typically runs faster. for more information type man egrep or man fgrep in your system.

Find command.
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions. Here are some examples.
Some Examples:

find $HOME -print will lists all files in your home directory. find /work -name chapter1 -print will list all files named chapter1 in /work directory. find / -type d -name 'man*' -print will list all manpage directories. find / -size 0 -ok rm {} \; will remove all empty files on system.

conditions of find

-atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days. -ctime +n or -n will find that were changed +n -n or n days ago. -depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command. -exec commad {} \; run the Unix command on each file matched by find. Very useful condition. -print print or list to standard output (screen). -name pattern find the pattern. -perm nnnfind files whole permission flags match octal number nnn. -size n find files that contain n blocks. -type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file.

back to top of misc commands back to top of page

Text processing
cut,paste, sort, uniq,awk,sed,vi.

Cut command.
cut command selects a list of columns or fields from one or more files. Option -c is for columns and -f for fields. It is entered as cut options [files] for example if a file named testfile contains
this is firstline this is secondline this is thirdline

Examples: cut -c1,4 testfile will print this to standard output (screen)
ts ts ts

It is printing columns 1 and 4 of this file which contains t and s (part of this).

Options:

-c list cut the column positions identified in list. -f list will cut the fields identified in list. -s could be used with -f to suppress lines without delimiters.

Paste Command.
paste command merge the lines of one or more files into vertical columns separated by a tab. for example if a file named testfile contains
this is firstline

and a file named testfile2 contains


this is testfile2

then running this command paste testfile testfile2 > outputfile will put this into outputfile
this is firstline this is testfile2

it contains contents of both files in columns. who | paste - - will list users in two columns.

Options:

-d'char' separate columns with char instead of a tab.

-s merge subsequent lines from one file.

Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz aaa 1234 yuer wer qww wwe

Then running sort testfile will give us output of


1234 aaa qww wer wwe yuer zzz

Options:
-b ignores leading spaces and tabs. -c checks whether files are already sorted. -d ignores punctuation. -i ignores non-printing characters. -n sorts in arithmetic order. -ofile put output in a file. +m[-m] skips n fields before sorting, and sort upto field position m. -r reverse the order of sort. -u identical lines in input file apear only one time in output.

Uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of each second file.

Examples
sort names | uniq -d will show which lines appear more than once in names file.

Options:

-c print each line once, counting instances of each. -d print duplicate lines once, but no unique lines. -u print only unique lines.

Awk and Nawk command.


awk is more like a scripting language builtin on all unix systems. Although mostly used for text processing, etc. Here are some examples which are connected with other commands.

Examples:
df -t | awk 'BEGIN {tot=0} $2 == "total" {tot=tot+$1} END {print (tot*512)/1000000}' Will give total space in your system in megabytes. Here the output of command df -t is being passed into awk which is counting the field 1 after pattern "total" appears. Same way if you change $1 to $4 it will accumulate and display the addition of field 4 which is used space. for more information about awk and nawk command in your system enter man awk or man nawk.

Sed command.
sed command launches a stream line editor which you can use at command line. you can enter your sed commands in a file and then using -f option edit your text file. It works as sed [options] files
options:

-e 'instruction' Apply the editing instruction to the files. -f script Apply the set of instructions from the editing script. -n suppress default output.

for more information about sed, enter man sed at command line in your system.

Vi editor.
vi command launches a vi sual editor. To edit a file type vi filename vi editor is a default editor of all Unix systems. It has several modes. In order to write characters you will need to hit i to be in insert mode and then start typing. Make sure that your terminal has correct settings, vt100 emulation works good if you are logged in using pc. Once you are done typing then to be in command mode where you can write/search/ you need to hit :w filename to write and in case you are done writing and want to exit :w! will write and exit.

options:

i for insert mode. o I inserts text at the curson o A appends text at the end of the line. o a appends text after cursor. o O open a new line of text above the curson. o o open a new line of text below the curson. : for command mode. o <escape> to invoke command mode from insert mode. o :!sh to run unix commands. o x to delete a single character. o dd to delete an entire line o ndd to delete n number of lines. o d$ to delete from cursor to end of line. o yy to copy a line to buffer. o P to paste text from buffer. o nyy copy n number of lines to buffer. o :%s/stringA/stringb /g to replace stringA with stringB in whole file. o G to go to last line in file. o 1G to go to the first line in file. o w to move forward to next word. o b to move backwards to next word. o $ to move to the end of line. o J join a line with the one below it. /string to search string in file. n to search for next occurence of string.

back to top of Text processing commands back to top of page

Shell and programming


Shell programming,bourne shell, ksh, csh, echo,line,sleep, test,cc compiler.

Shell programming concepts and commands.


Shell programming is integral part of Unix operating systems. Shell is command line userinterface to Unix operating system, User have an option of picking an interface on Unix such as ksh, csh, or default sh., these are called shells(interface). Shell programming is used to automate many tasks. Shell programming is not a programming language in the truest sense of word since it is not compiled but rather an interpreted language. Unix was written in C language and thus c language is integral part of unix and available on all versions. Shells, like ksh and csh are popular shells on unix although there are 5 or 6 different shells available but I will only be discussing ksh and csh as well as sh. Common features among all shells are job control, for example if I am running a processes which is

searching the whole system for .Z files and output is directed to a file named compressedfiles.
example:

find / -name *.Z -print > compressedfiles then after entering this command hitting <control z> key will suspend this job, then entering bg at command line will put this job in background, entering fg will put this job in foreground. Entering jobs at command line will show me all my concurrent jobs that are running. Other common features
o o o o o o o o o o o o o o o o o o o o o

> will redirect output from standard out (screen) to file or printer or whatever you like. >> filename will append at the end of a file called filename. < will redirect input to a process or commnand. | pipe output, or redirect output, good for joining commands, i.e. find command with cpio, etc. & at the end of command will run command in background. ; will separate commands on same line. * will match any characters in a file or directories. junk* will match all files with first 4 letters ? will match single characters in a file. [] will match any characters enclosed. () execute in subshell. ` ` to run a command inside another command and use its output. " " partial quote for variables. ' ' full quote for variables. # begin comment (if #/bin/ksh or csh or sh is entered at first line of script it runs script in that shell) bg background execution. break break from loop statements. continue Resume a program loop. Kill pid number will terminate running jobs stop will stop background job. suspend will suspend foreground job. wait will wait for a background job to finish.

Bourne Shell (sh shell).


sh or Bourne shell is default shell of Unix operating systems and is the most simplest shell in Unix systems.

Examples: cd; ls execute one after another. (date;who;pwd)> logifile will redirect all the output from three commands to a filenamed logfile. sort file | lp will first sort a file and then print it. alias [options] [name[='command']] will let you create your own commands. i.e. o alias ll="ls -la" will execute `ls -la` command whenever ll is entered. let expressions is syntax of let statement. o let i=i+1 will work as a counter with i incrementing each time this statement is encountered. for x[in list] do commands done is syntax for for do loop. function name {commands;} is the syntax of a function which can be called from anywhere in program. if condition1 then commands1 elif condition2 then commands2 ... ... ... else commands3 fi

Ksh shell (Korn).


Ksh or Korn shell is widely used shell.

Csh or C shell
csh is second most used shell.

Echo command
echo command in shell programming.

Line command.
line command in shell programming.

Sleep command.
sleep command in shell programming.

Test Command.
test command in shell programming.

CC compiler (c programming language compiler).


Since Unix is itself written in C programming language, most Unix operating systems come with c compiler called cc. back to top of Shell programming. back to top of page

Communications
cu,ftp,login, rlogin,talk,telnet, vacation and write .

Cu command.
cu command is used for communications over a modem or direct line with another Unix system. Syntax is cu options destination

Options -bn process lines using n-bit characters (7 or 8). -cname Search UUCP's device file and select local area network that matches name. -d Prints diagnostics. -e sends even parity data to remote system -lline communicate on this device (line=/dev/tty001, etc) -n prompts for a telephone number. -sn set transmission rate to n(e.g 1200,2400,9600, BPS) Destination telno is the telephone number of the modem to connect to. system is call the system known to uucp. aadr is an address specific to LAN.

Ftp command (protocol).


ftp command is used to execute ftp protocol using which files are transferred over two systems. Syntax is ftp options hostname

options -d enable debugging. -g disable filename globbing. -i turn off interactive prompts. -v verbose on. show all responses from remote server.

ftp hostname by default will connect you to the system, you must have a login id to be able to transfer the files. Two types of files can be transferred, ASCII or Binary. bin at ftp> prompt will set the transfer to binary. Practice FTP by ftping to nic.funet.fi loggin in as anomymous with password being your e-mail address.

Login command.
login command invokes a login session to a Unix system, which then authenticates the login to a system. System prompts you to enter userid and password.

Rlogin command.
rlogin command is used to log on to remote Unix systems, user must have permissions on both systems as well as same userid, or an id defined in .rhosts file. Syntax is rlogin options host

options -8 will allow 8 bit data to pass, instead of 7-bit data. -e c will let you use escape character c. -l user will let you to login as user to remote host, instead of same as local host.

Talk command.
talk command is used to invoke talk program available on all unix system which lets two users exchange information back and forth in real time. Syntax is talk userid@hostname

Telnet command.
Telnet command invokes a telnet protocol which lets you log on to different unix, vms or any machine connected over TCP/IP protocol, IPx protocol or otherwise. Syntax is telnet hostname

Vacation command.
vacation command is used when you are out of office. It returns a mail message to sender announcing that you are on vacation. to disable this feature, type mail -F " " .

syntax is vacation options


Options -d will append the date to the logfile. -F user will forward mail to user when unable to send mail to mailfile. -l logfile will record in the logfile the names of senders who received automatic reply. -m mailfile will save received messages in mailfile.

Write command will initiate an interactive conversation with user. Syntax is


write user tty

Storage commands
compress uncompress, cpio,dump,pack, tar, mt.

Compress command.
Compress command compresses a file and returns the original file with .z extension, to uncompress this filename.Z file use uncompress filename command. syntax for compress command is compress options files

Options -bn limit the number of bits in coding to n. -c write to standard output (do not change files). -f compress conditionally, do not prompt before overwriting files. -v Print the resulting percentage of reduction for files.

Uncompress command.
Uncompress file uncompresses a file and return it to its original form. syntax is uncompress filename.Z this uncompresses the compressed file to its original name.

Options -c write to standard output without changing files

Cpio command.
cpio command is useful to backup the file systems. It copy file archives in from or out to

tape or disk, or to another location on the local machine. Its syntax is cpio flags [options]

It has three flags, -i, -o, -p cpio -i [options] [patterns] o cpio -i copy in files who names match selected patterns. o If no pattern is used all files are copied in. o It is used to write to a tape. cpio -o
o

Copy out a list of files whose name are given on standard output. cpio -p

copy files to another directory on the same system. Options

o o o o o o o o o o o o o o

-a reset access times of input files. -A append files to an archive (must use with -o). -b swap bytes and half-words. Words are 4 bytes. -B block input or output using 5120 bytes per record. -c Read or write header information as Ascii character. -d create directories as needed. -l link files instead of copying. -o file direct output to a file. -r rename files interactively. -R ID reassign file ownership and group information to the user's login ID. -V print a dot for each file read or written. -s swap bytes. -S swap half bytes. -v print a list of filenames. Examples

o o o

find . -name "*.old" -print | cpio -ocvB > /dev/rst8 will backup all *.old files to a tape in /dev/rst8 cpio -icdv "save"" < /dev/rst8 will restore all files whose name contain "save" find . -depth -print | cpio -padm /mydir will move a directory tree.

Dump command is useful to backup the file systems.


dump command copies all the files in filesystem that have been changed after a certain

date. It is good for incremental backups. This information about date is derived from /var/adm/dumpdates and /etc/fstab . syntax for HP-UX dump is /usr/sbin/dump [option [argument ...] filesystem]

Options 0-9 This number is dump level. 0 option causes entire filesystem to be dumped. b blocking factor taken into argument. d density of tape default value is 1600. f place the dump on next argument file instead of tape. This example causes the entire file system (/mnt) to be dumped on /dev/rmt/c0t0d0BEST and specifies that the density of the tape is 6250 BPI. o /usr/sbin/dump 0df 6250 /dev/rmt/c0t0d0BEST /mnt for more info type man dump at command line.

Pack command.
pack command compacts each file and combine them together into a filename.z file. The original file is replaced. Pcat and unpack will restore packed files to their original form. Syntax is Pack options files

Options - Print number of times each byte is used, relative frequency and byte code. -f Force the pack even when disk space isn't saved. To display Packed files in a file use pcat command pcat filename.z To unpack a packed file use unpack command as unpack filename.z .

Tar command.
tar command creates an archive of files into a single file. Tar copies and restore files to a tape or any storage media. Synopsis of tar is tar [options] [file]

Examples:
tar cvf /dev/rmt/0 /bin /usr/bin creates an archive of /bin and /usr/bin, and store on the tape in /dev/rmt0. tar tvf /dev/rmt0 will list the tape's content in a /dev/rmt0 drive. tar cvf - 'find . -print' > backup.tar will creates an archive of current directory and store it in file backup.tar.

Functions:

c creates a new tape.

r append files to a tape. t print the names of files if they are stored on the tape. x extract files from tape.

Options:

b n use blocking factor of n. l print error messages about links not found. L follow symbolic links. v print function letter (x for extraction or a for archive) and name of files.

Mt command
Mt command is used for tape and other device functions like rewinding, ejecting, etc. It give commands to tape device rather than tape itself. Mt command is BSD command and is seldom found in system V unix versions. syntax is mt [-t tapename] command [count]

mt for HP-UX accept following commands eof write count EOF marks. fsf Forward space count files. fsr Forward space count records. bsf Backward space count files. bsr Backward space count records. rew Rewind tape. offl Rewind tape and go offline. eod Seek to end of data (DDS and QIC drives only). smk Write count setmarks (DDS drives only). fss Forward space count setmarks (DDS drives only). bss Backward space count setmarks (DDS drives only). Examples o mt -t /dev/rmt/0mnb rew will rewind the tape in this device. o mt -t /dev/rmt/0mnb offl will eject the tape in this device.

back to top of storage commands back to top of page

System Status
at, chmod,chgrp, chown,crontab,date, df,du, env, finger, ps,ruptime, shutdwon,stty, who.

At command.
at command along with crontab command is used to schedule jobs. at options time [ddate] [+increment] is syntax of at command. for example if I have a script named usersloggedin which contains.
#!/bin/ksh who | wc -l echo "are total number of people logged in at this time."

and I want to run this script at 8:00 AM. So I will first type at 8:00 %lt;enter> usersloggedin %lt;enter> I will get following output at 8:00 AM
30 are total number of people logged in at this time.

Options: -f file will execute commands in a file. -m will send mail to user after job is completed. -l will report all jobs that are scheduled and their jobnumbers. -r jobnumber will remove specified jobs that were previously scheduled.

Chmod command.
chmod command is used to change permissions on a file. for example if I have a text file with calender in it called cal.txt. initially when this file will be created the permissions for this file depends upon umask set in your profile files. As you can see this file has 666 or -rw-rw-rw attributes. ls -la cal.txt
-rw-rw-rw1 ssb dxidev 135 Dec 3 16:14 cal.txt

In this line above I have -rw-rw-rw- meaning respectively that owner can read and write file, member of the owner's group can read and write this file and anyone else connected to this system can read and write this file., next ssb is owner of this file dxidev is the group of this file, there are 135 bytes in this file, this file was created on December 3 at time16:14 and at the end there is name of this file. Learn to read these permissions in binary, like this for example Decimal 644 which is 110 100 100 in binary meand rw-r--r-- or user can read,write this file, group can read only, everyone else can read only. Similarly, if permissions are 755 or 111 101 101 that means rwxr-xr-x or user can read, write and execute, group can read and execute, everyone else can read and execute. All directories have d in front of permissions. So if you don't want anyone to see your files or to do anything with it use chmod command and make permissions so that only you can read and write to that file, i.e. chmod 600 filename.

Chgrp command.
chgrp command is used to change the group of a file or directory. You must own the file or be a superuser. chgrp [options] newgroup files is syntax of chgrp. Newgroup is either a group Id or a group name located in /etc/group .

Options: -h will change the group on symbolic links. -R recursively descend through directory changing group of all files and subdirectories.

Chown command.
chown command to change ownership of a file or directory to one or more users. Syntax is chown options newowner files

Options -h will change the owner on symbolic links. -R will recursively descend through the directory, including subdirectories and symbolic links.

Crontab command.
crontab command is used to schedule jobs. You must have permission to run this command by unix Administrator. Jobs are scheduled in five numbers, as follows.
Minutes 0-59 Hour Day of month month Day of week 0-23 1-31 1-12 0-6 (0 is sunday)

so for example you want to schedule a job which runs from script named backup_jobs in /usr/local/bin directory on sunday (day 0) at 11.25 (22:25) on 15th of month. The entry in crontab file will be. * represents all values.
25 22 15 * 0 /usr/local/bin/backup_jobs

The * here tells system to run this each month. Syntax is crontab file So a create a file with the scheduled jobs as above and then type crontab filename .This will scheduled the jobs.

Date command.
Date displays todays date, to use it type date at prompt.
Sun Dec 7 14:23:08 EST 1997

is similar to what you should see on screen.

Df command.
df command displays information about mounted filesystems. It reports the number of free disk blocks. Typically a Disk block is 512 bytes (or 1/2 Kilobyte). syntax is df options name

Options -b will print only the number of free blocks. -e will print only the number of free files. -f will report free blocks but not free inodes. -F type will report on an umounted file system specified by type. -k will print allocation in kilobytes. -l will report only on local file systems. -n will print only the file system name type, with no arguments it lists type of all filesystems

Du command.
du command displays disk usage.

Env command.
env command displays all the variables.

Finger command.
finger command.

PS command
ps command is probably the most useful command for systems administrators. It reports information on active processes. ps options

options. -a Lists all processes in system except processes not attached to terminals. -e Lists all processes in system. -f Lists a full listing. -j print process group ID and session ID.

Ruptime command.
ruptime command tells the status of local networked machines. ruptime options

options. -a include user even if they've been idle for more than one hour. -l sort by load average. -r reverse the sort order. -t sort by uptime. -i sort by number of users.

Shutdown command.
Shutdown command can only be executed by root. To gracefully bring down a system, shutdown command is used.

options. -gn use a grace-period of n seconds (default is 60). -ik tell the init command to place system in a state k. o s single-user state (default) o 0 shutdown for power-off. o 1 like s, but mount multi-user file systems. o 5 stop system, go to firmware mode. o 6 stop system then reboot. -y suppress the default prompt for confirmation.

Stty command
stty command sets terminal input output options for the current terminal. without options stty reports terminal settings. stty options modes < device

options -a report all options. -g report current settings. Modes 0 hang up phone. n set terminal baud. erase keyname, will change your keyname to be backspace key.

Who command
who command displays information about the current status of system.

who options file Who as default prints login names of users currently logged in.

Options -a use all options. -b Report information about last reboot. -d report expired processes. -H print headings. -p report previously spawned processes. -u report terminal usage.

Remove or Delete a File


To remove a file called abc.txt type the following command:
$ rm abc.txt

To remove all files & subdirectories from a directory (MS-DOS deltree like command), enter:
$ rm -rf mydir

To remove empty directory use rmdir and not rm:


$ rmdir mydirectory

What to Type What it Does


rm *

delete everything in a subdirectory


rm *.txt

remove only files with a .txt on the end


rm data*

remove only files that start with the word "data"


rm -r dir2

removes everything in the subdirectory "dir2"


Listing directory contents:

ls list a directory ls -l list a directory in long ( detailed ) format for example: $ ls -l drwxr-xr-x 4 cliff user 1024 Jun 18 09:40 WAITRON_EARNINGS -rw-r--r-1 cliff user 767392 Jun 6 14:28 scanlib.tar.gz ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | | | | | | | | | | | | | | | | owner group size date time name | | | | number of links to file or directory contents | | | permissions for world | | permissions for members of group | permissions for owner of file: r = read, w = write, x = execute -=no permission type of file: - = normal file, d=directory, l = symbolic link, and others... ls -a files start List the current directory including hidden files. Hidden

with "." ls -ld * List all the file and directory names in the current directory using long format. Without the "d" option, ls would list the contents of any sub-directory of the current. With the "d" option, ls just lists them like regular files.

The chmod command options are specified like this:


$ chmod [options] mode[,mode] file1 [file2 ...]

This is used to control the file mode. The concept of "permissions" is a DOS concept. To view the current file mode:
$ ls -l file

[edit] Octal numbers


See also: Octal notation of file system permissions The chmod command accepts up to four digits to represent an octal number. The octets refer to bits applied to the file owner, group and other users, respectively. Use of three digits is discouraged because it leaves the fourth as the default and this value is not fixed. The least significant digit sets/resets an additional mode for each of these three sets of bits. Experienced Unix and Linux users tend to recommend that the user of this command check the man page on the system of interest.

Particular care should be taken when a directory is the target because the effect is not intuitive. In addition, it will not work on all file types. For example, it has no effect on a symbolic link. myfile :
$ chmod 664 myfile $ ls -l myfile -rw-rw-r-- 1 57 Jul

3 10:13

myfile

Since the setuid, setgid and sticky bits are not set, this is equivalent to:
$ chmod 0664 myfile

[edit] Symbolic modes


See also: Symbolic notation of file system permissions also accepts symbolic notation, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:
chmod $ chmod [references][operator][modes] file1 ...

The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to all but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters: Reference Class Description user the owner of the file g group users who are members of the file's group o others users who are not the owner of the file or members of the group a all all three of the above, is the same as ugo
u

The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted: Operator
+ =

Description adds the specified modes to the specified classes removes the specified modes from the specified classes the modes specified are to be made the exact modes for the specified classes

The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions: Mode
r

Name read

Description read a file or list a directory's contents

w x

write execute

s t

write to a file or directory execute a file or recurse a directory tree which is not a permission in itself but rather can be used instead of x. It applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least 1 execute permission bit already set (either user, group or special other). It is only really useful when used with '+' and usually in execute combination with the -R option for giving group or other access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used "chmod -R a+rx .", whereas with 'X' you can do "chmod -R a+rX ." instead setuid/gid details in Special modes section sticky details in Special modes section

The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas.

[edit] Symbolic examples


Add the read and write permissions to the user and group classes of a directory:
$ chmod ug+rw mydir $ ls -ld mydir drw-rw---2 unixguy

uguys

96 Dec 8 12:53 mydir

For a file, remove write permissions for all classes:


$ chmod a-w myfile $ ls -l myfile -r-xr-xr-x 2 unixguy

uguys 96 Dec 8 12:53 myfile

Set the permissions for the user and the group to read and execute only (no write permission) on mydir.
$ chmod ug=rx mydir $ ls -ld mydir dr-xr-x--2 unixguy

uguys 96 Dec 8 12:53 mydir

[edit] Special modes


See also: File system permissions The chmod command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use s to represent the setuid and setgid modes, and t to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.

Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used.

[edit] Command line examples


command chmod a+r file

explanation

read is added for all chmod a-x file execute permission is removed for all chmod a+rw file change the permissions of the file file to read and write for all. On some UNIX version e.g.BSD, this will restore the permission of the file file to default e.g. -rwxr-xr-x. But it could be different on chmod +rwx file other UNIX systems, for example on Solaris, this command will not take any effect. chmod u=rw,go= read and write is set for the owner, all permissions are cleared for file the group and others change the permissions of the directory docs and all its contents to chmod -R u+w,go-w add write access for the user, and deny write access for everybody file else. chmod file removes all privileges for all sets read and write access for the owner, the group, and not for all chmod 664 file others. equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 chmod 0755 file specifies no special modes. the 4 specifies set user ID and the rest is equivalent to u=rwx chmod 4755 file (4+2+1),go=rx (4+1 & 4+1).
find path/ -type d -exec chmod a-x {} \; find path/ -type d -exec chmod a+x {} \; chmod -R u+rwX,grwx,o-rwx directory chmod -R a-x+X directory

removes execute permission for all directories (cannot list files) in tree starting from path/ (use '-type f' to match files only). allows directory browsing (ls for example) for all users if you've reset permissions for Samba write access. set a directory tree to rwx for owner directories, rw for owner files, --- for group and others. remove the execute permission on all files in a directory tree, while allowing for directory browsing.

RE: How to concatenate two strings in UNIX char1 "rahul" char2 "chavan" char3 $char1$char2 it concatinate a string in unix. RE: How to concatenate two strings in UNIX var1 abc var2 cde

var3 ${var1}${var2} echo $var3

Vous aimerez peut-être aussi