Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Topics
Input mode
Command mode
dw To delete a word
CTRL-D , CTRL-U To advance one half screen and to backup one half screen
y,p To yank or delete the text; To get back the deleted text
UNIX Commands
cd cmp passwd
pwd comm mkdir
chown
rmdir chgrp
umask
chmod
The first digit represents the owner, the second represents the group and the third
represents other users. The number 7 represents all three types of permission (i.e., read,
write and execute), 6 stands for read and write only, 5 stands for read and execute, 4 is
read only, 3 is write and execute, 2 is write only, 1 is execute only and 0 is no permissions.
Options:
-a => lists all entries including those starting with a period (.).
-i => displays inode numbers along with file names (only on systems
that support inode numbers, such as POSIX-compliant and UNIX
systems).
-l => displays permissions, links, owner, group, size, time, name;
-rw-rw-rw- 1 root dir 104 Dec 25 19:32 file
Options:
The next nine characters are in three groups of three; they describe the permissions on the file.
The first group of three describes owner permissions; the second describes group
permissions; the third describes other (or world) permissions.
r Permission to read file w Permission to write to file x Permission to execute
file
a Archive bit is on (file has not been backed up) c Compressed file
s System file h Hidden file t Temporary file
Eg: To grant read, write and execute permission to user, group and other on the file
test.ksh
chmod ugo+rwx test.ksh
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
chmod 400 file - Read by owner
chmod 040 file - Read by group
chmod 004 file - Read by world
chmod 200 file - Write by owner
chmod 020 file - Write by group
chmod 002 file - Write by world
chmod 100 file - execute by owner
chmod 010 file - execute by group
chmod 001 file - execute by world
When new groups are setup on a system or when files are copied to a new
system, we may need to change the group to which a particular file belongs.
find "C:/Omnimark" -name "*.java => To find all the files which has extension .java
-mtime number => matches if someone modified the file during the 24-hour period beginning
number days ago.
find "C:/Omnimark" -name "*.ksh" -mtime -2 => To find all the files with extension *.ksh and
which are modified within 2 days
-exec command ; => takes all arguments between -exec and the semicolon as a command line,
replacing any argument which is exactly {} (that is, the two brace characters) with the current
path name. It then executes the resulting command line, treating a return status of zero from
this command as a successful match, non-zero as failure. The terminal semicolon must be
quoted or escaped and delimited with white space.
find ${SgmDir} -name "*.sgm" -mtime +90 -level 0 -exec rm "{}" ";
The above example deletes all the sgm files present in the input directory which are 90 days older
rm -f -r "{}" "; The rm command can be replaced by this command to delete recursively and
forcibly
-atime number => matches if someone accessed the file during the 24-hour period beginning
number days ago.
-ctime number => matches if the file was creation during the 24-hour period beginning number
days ago.
The above example will find the file named new_data anywhere in the entire file system and print
it in the file found. Since it may take a long time to complete this command, it is run in the
background
Working With Files
Find For finding files
The above example finds for a directory belonging to user sha with a name beginning with garden
To find all files which have the extension .c and which were modified more than three days ago and less than nine
days ago:
This finds all read-only files (on Windows systems) which have tmp in either part of their names and deletes all
such files. Various parts of this expression are quoted to protect them from interpretation by the shell.
head <filename>
tail <filename>
wc - Word Count
wc [Options] filename
Options - Display no. of lines, words, characters
-l Display no. of lines
-w Display no. of words
-c Display no. of characters
e.g., $ wc test.c
20 200 5678
20 lines, 200 words, 5678- characters
nl - no. of lines in the file and temply lists out the file. Similar to wc -l < filename >
UNIX Commands
touch Updates access, modification or change times of a file
-a update access time
-m update modification time
-c prevents creating the file
e.g., $ touch f1
* The current system date & time stamp is put on the file f1
* If f1 does not exist then it is created with 0 bytes
If files are same no output is sent to the terminal, or else The line number and
the byte at which the first difference occurs is reported
e.g., umask 022 - Files normally created with 777 mode is assigned 755
permission. The value of each digit is subtracted from the corresponding "digit"
specified by the system for the creation of a file.
$ pack <filename>
e.g., $ pack try - Creates a file try.z which is packed
- Normally the executables are packed
- The size is reduced by 25 - 40 %
Options -f filelist
Normal File Types
- C program text
- assembler program text
- commands text
- ASCII text
- English text
e.g., $ file test.c
Tools : Filters & Utilities
Grep Tr
Fgrep Sed
Egrep Tac
Look Od
Cut Dc
Paste Bc
Join Tee
Sort Script
Uniq Cal
Cmp Date
Comm Zcat
Diff Zmore
Patch Zgrep
Dircmp zdiff
Tools : Filters & Utilities
Grep - Searches through one or more files for lines containing a target
and then prints all of the matching lines it finds
Options :
-i ignore case
e.g.,
$ grep -ni func1 *.c Prints all the lines and line numbers in
files *.c that match pattern func1
ignoring the case
SED: Stands for Stream Editor. It is very useful for filtering text files
Sed 1d data => Deletes the first line of the file data
Sed 1,4 d data => To delete the first 4 lines
Sed /New York/ d states => Removes all the lines containing New York
from the file states
Sed s/1998/1999/g meetings => To replace the text 1998 to 1999
Sed n 10,20p file => To print lines 10 through 20 only
Redirection
Metacharacter Performs
< input
> output write
Metacharacter Performs
| piping
e.g.,
e.g.,
Options
b ignore leading blanks and tabs
c only check if input file is already sorted
d dictionary order letters, digits, blanks
f ignore case
i ignore non-printable characters
n numeric sorting
r reverse order
tp use p as separator
u output only lines with unique keys
o filenameout save output in filenameout
e.g.,
$ tsort psortfile
Processing Tabular Data
Cut
Options:
e.g.,
5511 - pid
$ps
3432 2 0 : 24 -sh
5765 2 0 : 03 ps
5511 2 0 : 51 somecommand
$ kill 5511
$ ps
pid tty time command
3432 2 0 : 24 -sh
5985 2 0 : 03 ps
$ stubborn-cmd &
Stat parameters:
D means that the process is waiting for a turn to write to disk
I - that the process is currently idle & has been inactive for longer than 20 secs
J - process is in jail its scope of operation is limited to a certain predefined are of the system.
R process is runnable though not necessarily running currently
S process is sleeping and has been inactive for less than 20 secs
T - process has been stopped
Z process is dead. Such a process is called zombie
E trying to exit
< - process with high priority
+ - indicates a foreground process
Timely Execution :
real 2.0
user 0.4
sys 0.3
nohup Protecting a process from hanging and quit signals
or interrupts
The standard output is sent to nohup.out
e.g.,
$ nohup du / &
820
Sending output to nohup.out
$ logout
nice * Executes at specified priority
* Default priority is 24
$ at 5 pm
echo ^G ^G Time to logoff >/dev/tty04
^D
$
User Backup Utilities :
Options :
$ tar cvf /dev/fd096 * copies all files from the current directory
onto the backup medium /dev/fd096
$ tar xvf /dev/fd096 Extracts all the files from the backup medium
/dev/fd096 onto the current directory
tar xvf /dev/fd096 try.c Extracts the file try.c from /dev/fd096
Lists the files *.c and copy onto the device /dev/rmt/0n
Copies all the files mentioned in filelst onto the catridge tape
Extracts all the files from device c0s0 and creates necessary required directories
dd {option = value }
Options Values
e.g.,
$ doscp a:try.c . copies the file try.c from the floppy onto
the current working directory
Output Related Commands :
$ pr myfile
prepare myfile
nov 24 : 11 : 31 1987 myfile page 1
$ pr -t myfile
suppresses the header
make program takes the file named makefile or Makefile as its input.
makefile details:
- the names of the files that make up the program system
- their interdependencies
- how to regenerate the program system
Example :
final
At The at tool is a program that can read commands from either standard
input the keyboard or a file for later execution. You normally use it to schedule
one-off jobs.
Cron The cron tool is a system daemon that UNIX uses for handling timing.
You can schedule jobs to run regularly using a specially formatted crontab file.
Job Scheduling
Using at:
You can use the at command to schedule jobs that you only want to run once, at a specific
date and time. You start an at session using the syntax
At time [date]
You can specify time in HHMM OR HH:MM format, and you can optionally add a date in
MMDDYYYY format. If you prefer, you can specify time using the keywords midnight, noon
and teatime. Teatime is considered to be 4:00 pm.
At 6 am Friday
Echo Dont forget the meeting
CTRL-D
At f scriptfile 6 am Monday
Purpose of Crontab
Cron is designed to maintain a list of commands that the system needs to run at a given
time interval.
For example if you have a script that generates statistics and needs to be run every couple
of hours or everyday cron can do it for you. Or for example if you have a script that sends a
newsletter every month you can use cron to run the script that sends the newsletter every
month or week.
Crontab commands
When your logged in to your server you can use program cron using the following commands:
When you enter the edit mode (crontab -e) and start adding tasks to your cron file you
need to consider the following syntax:
The asterisk (*) symbolizes that every instance of that field (i.e. every minute, hour, day,
month, weekday) will be used in the command.
crontab -e
This will open the crontab file and let you edit it. By default this file will be opened with the
VI editor and you will need to press the Insert key on your keyboard to be able to write in
that file.
When you are finished adding your commands to the crontab file you need to save and exit.
If you are using VI as your editor you need to issue the following commands:
Press the Esc (Escape key) on your keyboard to enter the command mode of VI
After you pressed Escape then type the following characters :wq! and press Enter.
Remember you have to type this characters (remove the quotes): :wq!.
Job Scheduling
Now to list your crontab job just issue the following command: crontab -l
If you need to add another crontab job or even more all you need to do is follow the same
steps as above and just ad another line to the file.
REMEMBER: Every crontab job needs to be placed on its own line in the file and after the
last line you need to insert a non-braking character (press Enter).
The crontab syntax goes a little beyond its boundaries and has more advance meaning for
some users.
For example if you wish to use more then one instance of one column you will separate
those instances by a comma , or if you wish to use a time period lets say for example
from Sunday till Tuesday you will use dash -.
Run the script on the first, fifteenth and twentieth of every month.
0 0 1,15,20 * * /home/your_username/run-me.sh >/dev/null 2>&1
Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.
0 */3 * * * /home/your_username/run-me.sh >/dev/null 2>&1