Thursday, 30 August 2012

Basic UNIX Commands Continue..




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]
    • cpio -i copy in files who names match selected patterns.
    • If no pattern is used all files are copied in.
    • It is used to write to a tape.
cpio -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
    • -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
    • 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.
    • /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
    • mt -t /dev/rmt/0mnb rew will rewind the tape in this device.
    • mt -t /dev/rmt/0mnb offl will eject the tape in this device.


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-rw-    1 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                             0-23
Day of month           1-31
month                          1-12
Day of week             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.
    • s single-user state (default)
    • 0 shutdown for power-off.
    • 1 like s, but mount multi-user file systems.
    • 5 stop system, go to firmware mode.
    • 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.