UNIX FILE SYSTEM
to invoke the manual just type the command man along with command you wish to seek help on.
Syntax : $ man ls
ls
List information about FILEs, by default the current directory.
SYNTAX
ls [Options]... [File]...KEY
Sort entries alphabetically if none of -cftuSUX nor --sort.
-a, --all Do not hide entries starting with .
-A, --almost-all Do not list implied . and ..
-b, --escape Print octal escapes for nongraphic characters
--block-size=SIZE Use SIZE-byte blocks
-B, --ignore-backups Do not list implied entries ending with ~
-c Sort by change time; with -l: show ctime
-C List entries by columns
--color[=WHEN] Control whether color is used to distinguish file
types. WHEN may be `never', `always', or `auto'
-d, --directory List directory entries instead of contents
-D, --dired Generate output designed for Emacs' dired mode
-f Do not sort, enable -aU, disable -lst
-F, --classify Append indicator (one of */=@) to entries
-r, --reverse Reverse order while sorting
-R, --recursive List subdirectories recursively
-s, --size Print size of each file, in blocks
ls -al
total 109
drwxr-xr-x 18 root root 4096 Jun 9 21:12 ./
drwxr-xr-x 18 root root 4096 Jun 9 21:12 ../
drwxr-xr-x 2 root root 4096 Jun 9 21:14 bin/
drwxr-xr-x 3 root root 1024 Jun 9 20:32 boot/
File Type | Meaning |
- | Regular file |
d | Directory file |
C | Character Special file |
b | Block Special file |
l | Block special file |
s | Sempahore |
P | Named pipe |
m | Shared memory file |
lc and lf
lc and lf are look alikes of ls.tThe first one displays the files in columnar fashion,wheres the second puts a* after all executable files and a/ after all sub-directories prewent in the current directory.
$lc
Carribeans chap3.bpb chap6.bpb kiwis pommies
Chap1.bpb chao4.bpb cricket mainfile springboks
$lf
Carribeans* chap3.bpb chap6.bpb kiwis pommies
Chapp1.bpb chap4.bpb cricket* mainfile* Springboks
clear
Clear terminal screen
date
date command return the current date and time of the day.
$ date
$ date +%D
04/12/98
In this example %D stands for the date in mm/dd/yy format. Other formatting characters are listed below. Your version of date may not support all of these, and may support others. Check your man pages for a list of formatting characters.
Formatting characters | |||
%A | the full weekday name (Sunday, Monday etc.) | %n | a newline character |
%a | the abbreviation for the weekday (Sun, Mon etc.) | %p | the equivalent of AM or PM |
%B | the full month name (January, February etc.) | %R | the 24-hour time (for example, 13:22) |
%b | the abbreviated month name (Jan, Feb etc.) | %r | the 12-hour time with AM/PM notation (11:53:29 AM) |
%C | the first two digits of the year (00 to 99) | %S | the seconds (00 to 59) |
%D | the date in the form mm/dd/yy | %T | the 24-hour time (14:53:29) |
%d | the two-digit day of the month (01 to 31) | %U | the week number in the year (00-53) |
%H | the hour in a 24-hour clock (00 to 23) | %u | the weekday number (Monday = 1, Sunday = 7) |
%I | the hour in a 12-hour clock (01 to 12) | %w | the weekday number (Sunday = 0, Saturday = 6) |
%j | the numeric day of the year (001 to 366) | %Y | the four-digit number of the year (1999) |
%M | the minute (00 to 59) | %y | the two-digit year (99) |
%m | the month number (01 to 12) | %Z | the time zone name (PDT, EDT) |
It is frequently necessary to include spaces in a date format so it is usual to include the date format string in quotes. In the following example, the quotes are necessary because of the space in the format string.
$ date "+%D %R"
04/12/98 13:22
This ability to format the date fields can be used to create log file names that contain a date stamp. In the following version of logit.sh, the log file name is created from the year, month, day, and hour. This ensures that log files don't become too large to be easily handled.
mkdir
Create new folder(s), if they do not already exist.
SYNTAX : mkdir [Options] folder...
mkdir "Name with spaces"
OPTIONS -m, --mode=MODE set permission mode (as in chmod), not rwxrwxrwx – umask -p, --parents no error if existing, make parent irectories as needed
--verbose print a message for each created directory
mkdir creates the standard entries . (dot) for the current folder
and .. (dot dot) for its parent
Change Directory - change the current working directory to a specific Folder.
SYNTAX : cd [-LP] [directory]
OPTIONS
-P : Do not follow symbolic links
-L : Follow symbolic links (default)
If directory is not given, the value of the HOME
shell variable is used.
If the shell variable CDPATH
exists, it is used as a search path.
If directory begins with a slash, CDPATH
is not used.
If directory is `-', this will change to the previous directory location (equivalent to $OLDPWD
).
The return status is zero if the directory is successfully changed, non-zero otherwise.
Examples
move to the sybase folder
$ cd /usr/local/sybase
$ pwd
/usr/local/sybase
Change to another folder
$ cd /var/log
$ pwd
/var/log
Quickly get back
$ cd –
$ pwd
/usr/local/sybase
move up one folder
$cd ..
$ pwd
/usr/local/
$ cd (Back to your home folder)
rmdir
Remove folder(s), if they are empty.
SYNTAX :rmdir [options]... folder...
OPTIONS
--ignore-fail-on-non-empty
ignore each failure that is solely because the directory is non-empty
-p, --parents remove explicit parent directories if being emptied
--verbose output a diagnostic for every directory processed
--help display this help and exit
--version output version information and exit
Cat
To create a file.
Syntax: cat > <filename>
Save the file pressing ctrl+d
Display the contents of a file (concatenate)
SYNTAX : cat [Options] [File]...
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonblank output lines
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank never more than one single blank line
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
With no FILE, or when FILE is -, read standard input.
Examples:
Display a file
$ cat myfile
Concatenate two files:
$ cat file1 file2 >> file3.dat
Put the contents of a file into a variable
$my_variable=`cat $myfile.txt`
tee
Redirect output to multiple files, copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy.
SYNTAX : tee [options]... [file]...OPTIONS
`-a'
`--append'
Append standard input to the given files rather than overwriting them.
`-i' `--ignore-interrupts'
Ignore interrupt signals.
Example: ps -ax tee processes.txt more
If a file being written to does not already exist, it is created.
If a file being written to already exists, the data it previously contained is overwritten unless the `-a' option is used
touch : This command is also used to create new file.
$ touch sample
This creates a file called ‘sample’. However ,the size of the file would be zero bytes since touch does not allow you to store anything in a file. Then does touch serve any purpose? Yes ,when we want to create several empty files quickly. This can be done by saying,
$touch sample1 sample2 sample3 sample4
You would agree that this is a refreshingly simple way of creating empty files.
head [-number -n number] filename
-number | The number of the you want to display. |
-n number | The number of the you want to display. |
filename | The file that you want to display the x amount of lines of. |
Use this command to look at the head of a file. For example,
$ head essay.txt
displays the first 10 lines of the file essay.001 To see a specific number of lines, do this:
$ head -20 essay.txt
tail
This displays the first 20 lines of the file.
Delivers the last part of the file.
SYNTAX
tail [+ number] [-l] [-b] [-c] [-r] [-f] [-c number -n number] [file]
+number | |||||||||
-l | Units of lines. | ||||||||
-b | Units of blocks. | ||||||||
-c | Units of bytes. | ||||||||
-r | Reverse. Copies lines from the specified starting point in the file in reverse order. The default for r is to print the entire file in reverse order. | ||||||||
-f | Follow. If the input-file is not a pipe, the program will not terminate after the line of the input-file has been copied, but will enter an endless loop, wherein it sleeps for a second and then attempts to read and copy further records from the input-file. Thus it may be used to monitor the growth of a file that is being written by some other process. | ||||||||
-c number | The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes, to begin the copying:
The origin for counting is 1; that is, -c+1 represents the first byte of the file, -c-1 the last. | ||||||||
-n number | Equivalent to -c number, except the starting location in the file is measured in lines instead of bytes. The origin for counting is 1; that is, -n+1 represents the first line of the file, -n-1 the last. | ||||||||
file | Name of the file you wish to display |
EXAMPLES
tail myfile.txt - would list the last 10 (default) lines of the file myfile.txt
rm
Remove files (delete/unlink)
SYNTAX :rm [options]... file...
OPTIONS
-d, --directory unlink directory, even if non-empty (super-user only)
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
-r, -R, --recursive remove the contents of directories recursively
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
To remove a file you must have write permission on the file and the folder where it is stored.
rm -rf will recursively remove folders and their contents
The OWNER of a file does not need rw permissions in order to rm it.
cal
Display a calendar
SYNTAX : cal [-mjy] [[month] year]
options:
-m Display monday as the first day of the week.
-j Display julian dates (days one-based, numbered from January 1).
-y Display a calendar for the current year.
A single parameter specifies the 4 digit year (1 - 9999) to be displayed.
Two parameters denote the Month (1 - 12) and Year (1 - 9999).
If arguments are not specified, the current month is displayed.
A year starts on 01 Jan.
who
Print who is currently logged in
SYNTAX : who [options] [file] [am i]
OPTIONS
`-m'
Print the current user id, name and domain
(Same as `who am i')
`-q'
`--count'
Print only the login names and the number of users logged on.
Overrides all other options.
`-s'
Ignored; for compatibility with other versions of `who'
Who am i Print the current user id and name (`id -un')
wc
Print byte, word, and line counts, count the number of bytes, whitespace-separated words, and newlines in each given FILE, or standard input if none are given or for a FILE of `-'.
SYNTAX : wc [options]... [file]...
OPTIONS
-c
--bytes
--chars
Print only the byte counts.
-w
--words
Print only the word counts.
-l
--lines
Print only the newline counts.
-L
--max-line-length
Print only the length of the longest line per file,
and if there is more than one file it prints the
maximum (not the sum) of those lengths.
`wc' prints one line of counts for each file, and if the file was given as an argument, it prints the file name following the counts.
If more than one FILE is given, `wc' prints a final line containing the cumulative counts, with the file name `total'. The counts are printed in this order: newlines, words, bytes.
By default, each count is output right-justified in a 7-byte field with one space between fields so that the numbers and file names line up nicely in columns. However, POSIX requires that there be exactly one space separating columns. You can make `wc' use the POSIX-mandated output format by setting the `POSIXLY_CORRECT' environment variable.
By default, `wc' prints all three counts. Options can specify that only certain counts be printed. Options do not undo others previously given, so
wc --bytes --words
will print both the byte counts and the word counts.
cp
Copy one or more files to another location
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Syntax
cp [options]... Source Dest
cp [options]... Source... Directory
Key
-a, --archive same as -dpR
-b, --backup make backup before removal
-d, --no-dereference preserve links
-f, --force remove existing destinations, never prompt
-i, --interactive prompt before overwrite
-l, --link link files instead of copying
-p, --preserve preserve file attributes if possible
-P, --parents append source path to DIRECTORY
-r copy recursively, non-directories as files
--sparse=WHEN control creation of sparse files
-R, --recursive copy directories recursively
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-V, --version-control=WORD override the usual version control
-x, --one-file-system stay on this file system
--help display this help and exit
--version output version information and exit.
Example - copy home directory to floppy
$ cp -f /mnt/floppy/* /home/simon
mv
Move or rename files or directories.
SYNTAX
mv [options]... Source Dest
mv [options]... Source... Directory
If the last argument names an existing directory, `mv' moves each other given file into a file with the same name in that directory. Otherwise, if only two files are given, it renames the first as the second. It is an error if the last argument is not a directory and more than two files are given.
OPTIONS
-b
--backup
Make a backup of each file that would otherwise be overwritten or
removed.
-f
--force
Remove existing destination files and never prompt the user.
-i
--interactive
Prompt whether to overwrite each existing destination file,
regardless of its permissions. If the response does not begin with `y' or `Y', the file is skipped.
-S SUFFIX
--suffix=SUFFIX
Append SUFFIX to each backup file made with `-b'.
The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.
-u
--update
Do not move a nondirectory that has an existing destination with
the same or newer modification time.
-v
--verbose
Print the name of each file before moving it.
-V METHOD
--version-control=METHOD'
Change the type of backups made with `-b'. METHOD can be:
t, numbered make numbered backups
nil, existing numbered if numbered backups exist, simple otherwise
never, simple always make simple backups
--help display help and exit
--version output version information and exit
Examples
Rename the file apple as orange.doc:
mv apple orange.doc
Move orange.doc to the Documents folder:
mv orange.doc ~/Documents/orange.doc
Rename a bunch of file extensions
e.g. change *.txt into *.htm
for f in *.txt; do mv ./"$f" "${f%txt}htm"; done
`mv' can move only regular files across filesystems.
If a destination file exists but is normally unwritable, standard input is a terminal, and the `-f' or `--force' option is not given, `mv' prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response does not begin with `y' or `Y', the file is skipped.
pwd
Print Working Directory
SYNTAX
pwd [-LP]
OPTIONS (shell builtin)
-P : The pathname printed will not contain symbolic links.
-L : The pathname printed may contain symbolic links
The default action is to show the current folder as an absolute path.
All components of the path will be actual folder names - none will be symbolic links.
cut
Divide a file into several parts (columns)
Writes to standard output selected parts of each line of each input file, or standard input if no files are given or for a file name of `-'.
SYNTAX : cut [OPTION]... [FILE]...
In the options below, BYTE-LIST, CHARACTER-LIST, and FIELD-LIST are one or more
numbers or ranges (two numbers separated by a dash)
Bytes, characters, and fields are are numbered starting at 1 and
separated by commas. Incomplete ranges may be given: `-M'
means `1-M'; `N-' means `N' through end of line or last field.
OPTIONS
`-b BYTE-LIST'
`--bytes=BYTE-LIST'
Print only the bytes in positions listed in BYTE-LIST. Tabs and
backspaces are treated like any other character; they take up 1
byte.
`-c CHARACTER-LIST'
`--characters=CHARACTER-LIST'
Print only characters in positions listed in CHARACTER-LIST. The
same as `-b' for now, but internationalization will change that.
Tabs and backspaces are treated like any other character; they
take up 1 character.
`-f FIELD-LIST'
`--fields=FIELD-LIST'
Print only the fields listed in FIELD-LIST. Fields are separated
by a TAB character by default.
`-d INPUT_DELIM_BYTE'
`--delimiter=INPUT_DELIM_BYTE'
For `-f', fields are separated in the input by the first character
in INPUT_DELIM_BYTE (default is TAB).
`-n'
Do not split multi-byte characters (no-op for now).
`-s'
`--only-delimited'
For `-f', do not print lines that do not contain the field
separator character.
`--output-delimiter=OUTPUT_DELIM_STRING'
For `-f', output fields are separated by OUTPUT_DELIM_STRING The
default is to use the input delimiter.
chmod
Change access permissions
Usage: chmod [Options]... MODE[,MODE]... File...
chmod [Options]... NUMERIC_MODE File...
chmod [Options]... --reference=RFILE File...
options:
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
-c, --changes like verbose but report only when a change is made
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display help and exit
--version output version information and exit
chmod changes the permissions of each given file according to MODE, which can be either an octal number representing the bit pattern for the new permissions or a symbolic representation of changes to make, (+-= rwxXstugoa)
Numeric mode:
From one to four octal digits
Any omitted digits are assumed to be leading zeros.
The first digit = selects attributes for the set user ID (4) and set group ID (2) and save text image (1)S
The second digit = permissions for the user who owns the file: read (4), write (2), and execute (1)
The third digit = permissions for other users in the file's group: read (4), write (2), and execute (1)
The fourth digit = permissions for other users NOT in the file's group: read (4), write (2), and execute (1)
The octal (0-7) value is calculated by adding up the values for each digit
User (rwx) = 4+2+1 = 7
Group(rx) = 4+1 = 5
World (rx) = 4+1 = 5
chmode mode = 0755
Examples
Allow read permission to everyone:
chmod 444 file
Make a file readable and writable by the group and others:
chmod 066 file
Allow everyone to read, write, and execute the file:
chmod 777 file
Chmod | |||
Permission | Owner | Group | Other |
Read | |||
Write | |||
Execute | |||
Symbolic Mode
The format of a symbolic mode is `[ugoa...][[+-=][rwxXstugo...]...][,...]'.
Multiple symbolic operations can be given, separated by commas.
A combination of the letters `ugoa' controls which users' access to the file will be changed:
The user who owns it (u)
Other users in the file's group (g)
Other users not in the file's group (o)
All users (a)
If none of these are given, the effect is as if `a' were given, but bits that are set in the umask are not affected.
all users (a) is effectively
user + group + others
The operator '+' causes the permissions selected to be added to the existing permissions of each file; '-' causes them to be removed; and '=' causes them to be the only permissions that the file has.
The letters 'rwxXstugo' select the new permissions for the affected users:
Read (r),
Write (w),
Execute (or access for directories) (x),
Execute only if the file is a directory or already has execute permission for some user (X),
Set user or group ID on execution (s),
Save program text on swap device (t),
The permissions that the user who owns the file currently has for it (u),
The permissions that other users in the file's group have for it (g),
Permissions that other users not in the file's group have for it (o).
Examples
Deny execute permission to everyone:
chmod a-x file
Allow read permission to everyone:
chmod a+r file
Make a file readable and writable by the group and others:
chmod go+rw file
Allow everyone to read, write, and execute the file and turn on the set group-ID:
chmod =rwx,g+s file
Notes:
When chmod is applied to a directory:
read = list files in the directory
write = add new files to the directory
execute = access files in the directory
chmod never changes the permissions of symbolic links. This is not a problem since the permissions of symbolic links are never used. However, for each symbolic link listed on the command line, chmod changes the permissions of the pointed-to file. In contrast, chmod ignores symbolic links encountered during recursive directory traversals.
This page documents the GNU version of chmod.
tr
Translate, squeeze, and/or delete characters
SYNTAX
tr [options]... Set1 [Set2]
`tr' copies standard input to standard output, performing one of the following operations:
* translate, and optionally squeeze repeated characters in the result,
* squeeze repeated characters,
* delete characters,
* delete characters, then squeeze repeated characters from the result.
The SET1 and (if given) SET2 arguments define ordered sets of characters, referred to below as SET1 and SET2. These sets are the characters of the input that `tr' operates on. The `--complement' (`-c') option replaces SET1 with its complement (all of the characters that are not in SET1).
Specifying sets of characters
-----------------------------
The format of the SET1 and SET2 arguments resembles the format of regular expressions; however, they are not regular expressions, only lists of characters. Most characters simply represent themselves in these strings, but the strings can contain the shorthands listed below, for convenience. Some of them can be used only in SET1 or SET2, as noted below.
Backslash escapes
A backslash followed by a character not listed below causes an
error message.
\a Control-G.
\b Control-H.
\f Control-L.
\n Control-J.
\r Control-M.
\t Control-I.
\v Control-K.
\OOO The character with the value given by OOO, which is 1 to 3 octal digits, \\ A backslash.
Ranges
The notation `M-N' expands to all of the characters from M through N, in ascending order. M should collate before N; if it doesn't,an error results. As an example, `0-9' is the same as `0123456789'. Although GNU `tr' does not support the System V syntax that uses square brackets to enclose ranges, translations specified in that format will still work as long as the brackets in STRING1 correspond to identical brackets in STRING2.
Repeated characters
The notation `[C*N]' in SET2 expands to N copies of character C. Thus, `[y*6]' is the same as `yyyyyy'. The notation `[C*]' in STRING2 expands to as many copies of C as are needed to make SET2 as long as SET1. If N begins with `0', it is interpreted in octal, otherwise in decimal.
Character classes The notation `[:CLASS:]' expands to all of the characters in the (predefined) class CLASS. The characters expand in no particular order, except for the `upper' and `lower' classes, which expand in ascending order. When the `--delete' (`-d') and `--squeeze-repeats' (`-s') options are both given, any character class can be used in SET2. Otherwise, only the character classes
`lower' and `upper' are accepted in SET2, and then only if the corresponding character class (`upper' and `lower', respectively) is specified in the same relative position in SET1. Doing this specifies case conversion. The class names are given below; an error results when an invalid class name is given.
`alnum' Letters and digits.
`alpha' Letters.
`blank' Horizontal whitespace.
`cntrl' Control characters.
`digit' Digits.
`graph' Printable characters, not including space.
`lower' Lowercase letters.
`print' Printable characters, including space.
`punct' Punctuation characters.
`space' Horizontal or vertical whitespace.
`upper' Uppercase letters.
`xdigit' Hexadecimal digits.
Equivalence classes
The syntax `[=C=]' expands to all of the characters that are equivalent to C, in no particular order. Equivalence classes are a relatively recent invention intended to support non-English alphabets. But there seems to be no standard way to define them or determine their contents. Therefore, they are not fully implemented in GNU `tr'; each character's equivalence class consists only of that character, which is of no particular use.
Translating
-----------
`tr' performs translation when SET1 and SET2 are both given and the `--delete' (`-d') option is not given. `tr' translates each character of its input that is in SET1 to the corresponding character in SET2. Characters not in SET1 are passed through unchanged. When a character appears more than once in SET1 and the corresponding characters in SET2
are not all the same, only the final one is used. For example, these two commands are equivalent:
tr aaa xyz
tr a z
A common use of `tr' is to convert lowercase characters to
uppercase. This can be done in many ways. Here are three of them:
tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
tr a-z A-Z
tr '[:lower:]' '[:upper:]'
When `tr' is performing translation, SET1 and SET2 typically have the same length. If SET1 is shorter than SET2, the extra characters at the end of SET2 are ignored.
On the other hand, making SET1 longer than SET2 is not portable;POSIX.2 says that the result is undefined. In this situation, BSD `tr'pads SET2 to the length of SET1 by repeating the last character of SET2 as many times as necessary. System V `tr' truncates SET1 to the length
of SET2.
By default, GNU `tr' handles this case like BSD `tr'. When the `--truncate-set1' (`-t') option is given, GNU `tr' handles this case like the System V `tr' instead. This option is ignored for operations other than translation.
Acting like System V `tr' in this case breaks the relatively common
BSD idiom:
tr -cs A-Za-z0-9 '\012'
because it converts only zero bytes (the first element in the
complement of SET1), rather than all non-alphanumerics, to newlines.
Squeezing repeats and deleting
------------------------------
When given just the `--delete' (`-d') option, `tr' removes any input characters that are in SET1.
When given just the `--squeeze-repeats' (`-s') option, `tr' replaces each input sequence of a repeated character that is in SET1 with a single occurrence of that character.
When given both `--delete' and `--squeeze-repeats', `tr' first performs any deletions using SET1, then squeezes repeats from any remaining characters using SET2.
The `--squeeze-repeats' option may also be used when translating, in which case `tr' first performs translation, then squeezes repeats from any remaining characters using SET2.
Here are some examples to illustrate various combinations of options:
* Remove all zero bytes:
tr -d '\000'
* Put all words on lines by themselves. This converts all
non-alphanumeric characters to newlines, then squeezes each string of repeated newlines into a single newline:
tr -cs 'a-zA-Z0-9' '[\n*]'
* Convert each sequence of repeated newlines to a single newline:
tr -s '\n'
* Find doubled occurrences of words in a document. For example, people often write "the the" with the duplicated words separated by a newline. The bourne shell script below works first by converting each sequence of punctuation and blank characters to a single newline. That puts each "word" on a line by itself. Next it maps all uppercase characters to lower case, and finally it runs `uniq' with the `-d' option to print out only the words that were adjacent duplicates.
#!/bin/sh
cat "$@" \
tr -s '[:punct:][:blank:]' '\n' \
tr '[:upper:]' '[:lower:]' \
uniq -d
eg: $ cut -f2 -d';' filename tr a-z A-Z
The above command will cut field number 2 from the file 'filename' which has fields delimited by a ';'. The standard output is given to the translate command which
converts all the lower case to upper case letters.
passwd
Modify a user password.
SYNTAX
passwd [options...]
OPTIONS
-d, --delete delete the password for the named account (root only)
-f, --force force operation (effectively calls `chfn'?)
-k, --keep-tokens keep non-expired authentication tokens
-l, --lock lock the named account (root only)
-S, --status report password status on the named account (root only)
--stdin read new tokens from stdin (root only)
-u, --unlock unlock the named account (root only)
-?, --help Show this help message
--usage Display brief usage message
If no options are specified - passwd will change the password of the currently logged in user - will prompt for the old and new passwords.
Unmask:-What unix does is it permissions.Unmask stands for user file creation mask ,the term mask implying wchich permissions to mask or hide.The umask value tells unix which of the three permissions are to be denied rather than granted.The current value of unmask can be easily determined by just typing unmask .
$umask
0022
Here,the first 0 indicates that what follows is an octal number.The three digits that follow the first zero refer
To the permissions to be cenied to the owner,group and others.
Can we not change the current umsk value? Very easily.All that we have to say is something like.
$ umask 242
This would see to it that here onwards any new file that you create would have the permissions 424(666-242) and any directory that you create have the permissions 535(777-242).
bc
Once you type bc at prompt,you are in the calculator mode,and the $ the prompt disappears .That is the only way the reserved and laconic Unix indicates to you that it has braced itself to carry out even the most mind boggling calculations.
The input to the calculator is taken line by line.Enter an expression and Unix supplies the result as shown here:
$bc
10/2 *2
10
2.5 * 2.5+2
8.25
Quit
$bc
Scale=1
2.25+1
3.35
$bc
Ibase=2
Obase=16
11010011
89275
1010
A
Quit
$bc
Sqrt(196)
14
S(3.14)
Unimplemented
$bc –l
Scale=2
S(3.14)
0
$bc
For(i=1;i<=5;i=i+1)
1
2
3
4
5
Quit
expr
Another utility that aids computation in Unix is expr.
$expr 100 + 5
150
$expr 3\*2
6
factor
There is another math related command available in Unix. When factor is invoked without and argument, it waits for a number to be typed in. If you type in a positive number less than 2^46 (about 7.2e13) it will factorise the number and print its prime factors ;each one is printed the proper number of times .then it waits for another number. It exits if it encounters a zero or any non-numeric character.
For example,
$factor
15
3
5
18
2
3
3
q
$
df
This command reports the free as well as the used disk space for all the file systems installed your machine.
$df
/ (/dev/root ): 12970 blocks 27857 i-nodes.
dfspace
It reports the free disk space in terms of megabytes and percentage of total disk space.
$/etc/dfspace
: Disk space 6.32 MB of 137.74 MB available(4.59%).
Total Disk Space : 6.32 MB of 137.74 MB available(4.59%).
du
Du reports the disk space used by specified files and directories. For example:
$du
226 ./backup
418 ./fa/backup
1182 ./fa
4 ./check
16 ./dbf
1662
Here du is reporting the number of blocks used by the current directory(denoted by ‘.’) and those used by subdirectories within the current directory .Thus,when invoked without any arguments it assumes that blocks occupied by current directory and the directories lying within it are to be reported.
If we specify a directory then du descends down this directory locating any subdirectories lying in it and reports the blocks used by the directory and the subdirectories.
For example
$du /dev
2 /dev/String
4 /dev/rdsk
4 /dev/dsk
2 /dev/mouse
20 /dev
Thus ,the number of blocks occupied by each within /dev, as well as those occupied by /dev are displayed. If we want only the blocks occupied by the directory and not those occupied by the sub-directories within it we can say,
$du –s /dev
Du is often used to single out directories that occupy large amounts of disk space .Unused and redundant files directories can then be eliminated from ,thereby freeing the valuable disk space.
ulimit
$ulimit
20797152
This implies that the user can not create a file whose size is bigger than 2097152 bytes, or 2048 KB.If you happen to create a file which exceeds this size would be curtailed to 2048 KB and the program creating this file would be aborted.
A user can reduced this value by saying,
$ulimit
Here onwards no file can be created whose size is bigger than 512 bytes .Once reduced this value remains effective till the user doesn’t log out .thus this change will be effective only for the current session and the system will return to its default value when you log out. An ordinary user only reduce the ulimit value and is never permitted to increase it.A super-user is an exception to the rule and can increase or decrease this value.
file
$file *
a.out: iAPX 386 executable not stripped
ban: empty
bin: directory
cal.out: ascii directory text
catfile : commands text
clean: c program text
File commands recognizes several types of files as shown in above output .Before determining the content it of course reads each file .we typed an English paragraph in the file story, a few Unix commands in the file cmd and some garbage in the file trash. And, file being what it reported story to be an ‘English text file’, cmd to be a ‘command text file’ and trash to be an ‘ascii text file ‘.File also recognizes empty files and directories and reports them accordingly. Note how the contents of the file a.out are being reported.
Can you make out much from that message? Frankly ,I cannot.
Links With Dos
If you want to read some Dos formatted floppy,you simply have to say:
$dosdir a:
This will give you the list of all files on the floppy similar to the way DIR lists them .If we want to see the contents of a directory ‘project’ we will have to say:
$dosdir a:/proj
Note that the drive specification in this commands is followed by a’/’ and not the customary ‘\’ that we employ in DOS.
In order to see the contents of a Dos file, we can use
$doscat a:filename
Similarly, dosmkdir,dosrmkdir, dosrm and also function on Dos formatted disks as their counterparts, mkdir,rmdir,rm and ls work in Unix environment.Not only this,we can also format a floppy so that Dos understands it using the command dosformat.
Though all these utilities are useful ,the most important utility in this are is doscp ,since it is the one which would help you move files from Dos to Unix and vice versa.The trailing cp in doscp suggests that it is a function for copying files .But that’s only half the picture .doscp copies a Unix file onto a disk which is in DOS format,with all necessary conversions to make it readable by DOs.For example
$ doscp trial a:
Will copy the file trial from current Unix directory to the Dos formatted floppy in A drive.
Likewise, we can copy several files at time :
$doscp $HOME/proj/*.prg a:/progs
This copies all ‘.prg’ files from the proj directory in the home directory to the progs directory in drive A.
sort
The sort command can be used for sorting the contents of a file. The simplest form of sort command would be
$sort myfile
This would sort the contents of myfile and display the sorted output on the screen. If we want we can sort the contents of several files at once short as in
$sort file1 file2 file3
Instead of displaying the sorted output on the screen we can store it in a file by saying,
$ sort –o result file1 file2 file3
The above command sorts the three files file1,file2 7 file3 and saves the result in a file called result.
Option | Meaning |
-b | Ignores leading spaces and tbs |
-c | Checks if files are are already sorted.If they are,sort does nothing |
-d | Sorts in directory order(ignores punction) |
-f | Ignores case |
-m | Merges files that have already been sorted. |
-n | Sorts in numeric order. |
-ofile | Stores output in file.the default is to send output to standard output. |
-r | Reversed sort |
-tc | Separates fields with character(default is tab) |
-u | Unique output:if merge creates identical lines,uses only the first. |
+n[-m] | Skips n fields before sorting and then sorts through filed m. |
grep
grep is an acronym for globally search a regular expression and print it’.the command search the specified input fully (globally) for a match with the supplied pattern and display it.Whgile forming the patterns to be searched we can use shell metacharacters ,or regular expressions , as professional Unix users call them. Some metacharacters are like’’, ‘?’,’[]’ and ‘!’that we learnt .
Let us begin with the simplest example of usage of grep .
$grep picture newsfile
This would search the world ‘picture’ in the file newsfile and if found, the lines containing it would be displayed on the screen.
We can use grep to search a pattern in several files.for exaple,
$ grep picture newsfile storyfile
Here ,the word ‘picture’ would be searched in both the files, newsfile and storyfile and if found, the lines containing it would b displayed along with the name of the fiule where it occurred .this way we would be able to make out the file which the liens is being listed.
Options | Meaning |
-c | Rerurns only the number of matches,without quoting the text |
-i | Ignores case while searching |
-l | Returns only filenames containing a match ,without quoting the text. |
-n | Returns line number of matched text, as well as the text itself. |
-s | Suppresses error message |
-v | Returns lines that do not match the text |
Redirection
Redirection is a very useful tool and is frequently used to redirect the output of a process running in the background .
Can we make out what the following commands would achieve?
(a) $ls >filelist
(b) $cat par3 apr4 apr5 >message
(c) $Cat file1 > file1
(d) $cat<file1> file1
(e) $cat >file<file1
(f) $date; who
(g) $(date ;who>logfile
(h) $(date ;who) >logfile
Operator | Action |
>file | Make file the standard ouput |
<file | Make file the standard input |
>>file | Make file the standard output,appending to it if it already exists |
<<word | Take shell input up to the first line containing word or upto end of file |
n>file | Make file the output for file descriptor n. |
1>&2 | Redirect standard ouput to standard eror |
Cmd1 cmd2 | Make standard output of cmd1 the standard input cmd2 |
Piping
The Philosophy of Unix commands is that each does a small job but does it quite comprehensively. Quite often a single Unix
Commands does not suffice to solve a problem or do a ask .
That’s the time we can try joining commands together .the chief tools for this are redirection and the pipe. If you have to Unix from some other operating system you probably know these facilities. However, their true power is trapped best(arguably) by the Unix commands.
Let us now see a few examples of piping.
$ls wc –l
Here the output of ls becomes the input to wc which
Promptly counts the number of lines it receives as input and display this count on the screen.
$ls wc –l .countfile
$who sort
$who sort >sortedlist
Write
The write command can be used by any user to write something on some else’s terminal, provided the recipient of the message permits communication.
$write user3
Good morning.
Ctrl+d
On executing this command the message would be relayed to the user whose login name is user2.
finger
finger is one command that tells you which users are connected and which,if any can receive messages.It displays a list of all those who havelogged in and palces a* next to those terminals where mesg is set to –n
$finger –i
Login TTy When idle
Veena *tty01 Fri Oct 13 17:25 8 minutes12 seconds
Prafull tty3f Fri Oct 13 17:21 49 seconds
User3 tty3a Fri Oct 13 16:59
wall
this command can only be used by the superuser.among the various privileges enjoyed by him is the ability to write to any user on the network .Wall enables the superuser to ‘write to all’ irrespective of whether the user have given write permission to their terminal or not .The superuser,or the system administrator can take a number of such liberties, and not without reason. If the Unix system is going to be shutdown in say 10 minutes time,it is upto the system administrator to warn all connected users and notify them to save whatever they are working on .He does so using wall .The wall command must be given as:
#/etc/wall
System shutting down in 10 minutes.
You are advised to take the required backups.
Ctrl+d
All users who are logged in,will hear a beep and see a message flashed on their screens as follows:
Broadcast message from root(tty01) on Unix May 15 9:37 1966
System shutting down in 10 minutes.
You re advised to take the required backups.
The wall program resides in /etc directory and since this directory is not in our path we have to give the command as /etc/wall rather than just wall.Observe that the command prompt is shown as # and not the usual $,a s that is the prompt the superuser works at.
Using mail you can quickly and efficiently circulate memos and other written information to your co-workers ,including
Directions for the out of town party this Saturday and the least meanderings .You can even and receive mail from people outside your organization ,if you and they use networked computers.
Write requires that the user to whom the message is to be sent not only to be logged in,but also open to message.
Unlike this, mail can be sent to users who have logged in currently or even to users who haven’t logged in currently.
In case the user has logged in at several terminals the
Moment mail is sent to this user it becomes available at all the terminals.
$mail user2
Subject :Unix course
It is overhead that the system administrator plans to conduct a 4 day crash course in Unix.
Ctrl+d
If in one go you want to send the same mail to more than one user, you can say
$ mail user2 user3 user4
Subject :Meeting will be held on 17.08.05 at 4:30 p.m. in
the room No.-B006.
If you are to mail a program written by you then you of course can not be expected to type in the program after you have issued the mail command as in the above cases.In such cases you c an use input redirection as shown below:
$ mail user2 user3 veena < mynote
Now the contents of the file mynote would be promptly mailed to user2 user3 and veena.
What is vi?
The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.]
The UNIX vi editor is a full screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
2. Insert mode in which entered text is inserted into the file.
In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode.
While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.
To Get Into and Out Of vi
To Start vi
To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.
* | vi filename | edit filename starting at line 1 |
vi -r filename | recover filename that was being edited when system crashed |
To Exit vi
Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.
* | :x<Return> | quit vi, writing out modified file to file named in original invocation |
:wq<Return> | quit vi, writing out modified file to file named in original invocation | |
:q<Return> | quit (or exit) vi | |
* | :q!<Return> | quit vi even though latest changes have not been saved for this vi call |
Moving the Cursor
Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.
If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.
In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.
j or <Return> | move cursor down one line |
k [or up-arrow] | move cursor up one line |
h or <Backspace> | move cursor left one character |
l or <Space> | move cursor right one character |
0 (zero) | move cursor to start of current line (the one with the cursor) |
$ | move cursor to end of current line |
w | move cursor to beginning of next word |
b | move cursor back to beginning of preceding word |
:0<Return> or 1G | move cursor to first line in file |
:n<Return> or nG | move cursor to line n |
:$<Return> or G | move cursor to last line in file |
Screen Manipulation
The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.
^f | move forward one screen |
^b | move backward one screen |
^d | move down (forward) one half screen |
^u | move up (back) one half screen |
^l | redraws the screen |
^r | redraws the screen, removing deleted lines |
Adding, Changing, and Deleting Text
Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.
Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.
* | u | UNDO WHATEVER YOU JUST DID; a simple toggle |
The main purpose of an editor is to create, add, or modify text for a file.
Inserting or Adding Text
The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.
* | i | insert text before cursor, until <Esc> hit |
I | insert text at beginning of current line, until <Esc> hit | |
* | a | append text after cursor, until <Esc> hit |
A | append text to end of current line, until <Esc> hit | |
* | o | open and put text in a new line below current line, until <Esc> hit |
* | O | open and put text in a new line above current line, until <Esc> hit |
Changing Text
The following commands allow you to modify text.
* | r | replace single character under cursor (no <Esc> needed) |
R | replace characters, starting with current cursor position, until <Esc> hit | |
cw | change the current word with new text, | |
cNw | change N words beginning with character under cursor, until <Esc> hit; | |
C | change (replace) the characters in the current line, until <Esc> hit | |
cc | change (replace) the entire current line, stopping when <Esc> is hit | |
Ncc or cNc | change (replace) the next N lines, starting with the current line, |
Deleting Text
The following commands allow you to delete text.
* | x | delete single character under cursor |
Nx | delete N characters, starting with character under cursor | |
dw | delete the single word beginning with character under cursor | |
dNw | delete N words beginning with character under cursor; | |
D | delete the remainder of the line, starting with current cursor position | |
* | dd | delete entire current line |
Ndd or dNd | delete N lines, beginning with the current line; |
Cutting and Pasting Text
The following commands allow you to copy and paste text.
yy | copy (yank, cut) the current line into the buffer |
Nyy or yNy | copy (yank, cut) the next N lines, including the current line, into the buffer |
p | put (paste) the line(s) in the buffer into the text after the current line |
Other Commands
Searching Text
A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.
/string | search forward for occurrence of string in text |
?string | search backward for occurrence of string in text |
n | move to next occurrence of search string |
N | move to next occurrence of search string in opposite direction |
| ||||||||||||
Customizing vi: the .exrc file
The .exrc file in your login directory is a collection of vi commands to customize that environment. For example, an .exrc file containing:
:set showmode
:set ic
tells vi to show the words INPUT MODE in the lower right-hand corner of the screen when in Insert mode, and then tells the editor to ignore case during searches. To temporarily tell vi to respect case during searches, enter :set noic before searching. When you leave and reenter vi, your .exrc file takes effect again so that case is ignored during searches.
:set showmode | show when you are in insert mode |
:set ic | ignore case when searching |
:set noic | turn ignore case off |
:set nu | turn on line numbering |
:set nonu | turn line numbering off |
No comments:
Post a Comment