Вы находитесь на странице: 1из 7

Delete files older than X days

==============================
find /path/to/files* -mtime +5 -exec rm {} \;
find ./ -type f -mtime +62 -print0 | xargs -0 -I file /bin/rm file -f
Delete blank lins in vi
=======================
Use the following options
<esc>:g/^$/d to delete blank lines that do not contain spaces.
If it contains spaces, then use <esc>:g/^ *$/d
Maintain log file
=================
exec >> /home/nwadhwa/env_sync/ExportLog.log 2>&1
Above command directs all log (infor + error) to the log file given. Use this as
the first line of your code
UNIX general commands
=====================
stty -a
print terminal line settings
Eg:
hhdtora20:/home/gchakrav/lrn:VMDEVDB->stty -a
speed 38400 baud; rows 60; columns 157; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = <undef>; eol = <undef>; eol2
= <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
rm -r <folder name>
Note: This command should be used with care. If folder containing deleted folder
has no files left, then that's also removed by this command (since
we use recursive)
rm -f `ls`
Shortcut to delete all files in a folder
Also use - rm -f `ls -l | grep -v .tex`
touch <filename>
update access and modification time of files. Also used to create empty files
wc -l < file.txt
if '<' not used (wc -l file.txt), output contains no. of lines and filename -
3 file.txt
If '<' is used you get just 3
cut -d',' -f1 ddd.txt
-d option is for delimiter, followed by the delimiter. -f1 cuts the first word
grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
Outputs the first and sixth fields, delimited by a colon
cut -c1-3 file.txt
Outputs the first three characters of every line of the file file.txt
du -sh /media_lib/vmware/scv/
du is disk usage. To get the summary of a grand total disk usage size of an dire
ctory use the option -s as above
Eg: 84G /media_lib/vmware/scv/
Using -h option with du command provides results in Human Readable Format
Eg: du -h /media_lib/vmware/scv/
33K /media_lib/vmware/scv/vmuatdb/edw_return_flow
10G /media_lib/vmware/scv/vmuatdb/edw_return_flow_archive
3.0K /media_lib/vmware/scv/vmuatdb/etm_from_hh
1.0K /media_lib/vmware/scv/vmuatdb/env_sync
11G /media_lib/vmware/scv/vmuatdb
(summary level)
2.0K /media_lib/vmware/scv/vmproddb/edw_return_flow
70G /media_lib/vmware/scv/vmproddb/edw_return_flow_archive
24M /media_lib/vmware/scv/vmproddb/etm_to_hh/discard
1.3G /media_lib/vmware/scv/vmproddb/etm_to_hh/processed
74G /media_lib/vmware/scv/vmproddb
(summary level)
6.6M /media_lib/vmware/scv/vmqatdb/etm_to_hh
1.0K /media_lib/vmware/scv/vmqatdb/etm_to_hh_archive
5.0K /media_lib/vmware/scv/vmqatdb/edw_return_flow
(summary level)
df -h .
Gives the overall disk space on file system
Eg: df -h .
Filesystem Size Used Avail Use% Mounted on
gdm-a:/media_lib 739G 646G 86G 89% /media_lib
df -h /home/vmwacct
Filesystem Size Used Avail Use% Mounted on
hhdldappfs01:/sharedfs/home/vmwacct
562G 322G 235G 58% /home/vmwacct
sort <file>
Sorts an ascii file's contents in order
sort -u <file> removes duplicates
sort -n <file> for file containing numbers
sort -nr <file> for sorting numerically in reverse order
sort -n file1 file2 for multiple files
sort -nu file1 file2 for Sort, merge and remove duplicates
sort -t"," -k1,1 file
sort file on the basis of 1st field (-t is delimiter and not required if fields
delimited by a space or a tab)
'-k' is used to specify the keys on the basis of which the sorting has to be don
e. -k1,1 is first field, -k2,2 is second field, -k2n,2 is second
field numerically
variations -
sort -t"," -k1,2 -k3n,3 file - sort a file based on the 1st and 2nd field, and n
umerically on 3rd field on a file containing 5 columns
sort -t"," -k1,1 -k2n,2 file - sort the file alphabetically on the 1st field, nu
merically on the 2nd field
sort -t"," -k2nr,2 file - Sort the file numerically on the 2nd field in reverse
order
tail -f FILENAME
View growing log file in real time using tail command
grep -n
the -n option prints the filename and the line of the match. Is very useful when
you quickly want to look up what that line is (if especially an
error since it quickly tells you what type of error is on that line)
Other useful grep options -
-c for count - prints number of matching counts per file
-i for ignore case
-o for ONLY matching lines for the given pattern
-v for invert match so lines not matching the pattern are the ones printed
> <filename>
this truncates the file specified. > is a redirection filter that can be used qu
ickly set files to zero bytes.
Handling Passwords
The -s option causes input coming from a terminal do not be displayed on the scr
een. This is useful for password handling (readpass.sh):
#!/bin/bash
read -s -p "Enter Password : " my_password
echo
echo "Your password - $my_password"
You can time out read command using the -t option. It causes read to time out an
d return failure if a complete line of input is not read within TIMEOUT seconds.
For example, if no input provided within 10 second, program will be aborted (do
main2.sh):
#!/bin/bash
read -t 10 -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_nam
e
whois $domain_name
Trapping Signals
================
Signals are software interrupts sent to a program to indicate that an important
event has occurred. The following are some of the more common
signals you might encounter and want to use in your programs:
SIGHUP 1 Hang up detected on controlling terminal or death of controlling
process
SIGINT 2 Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT 3 Issued if the user sends a quit signal (Ctrl + D).
SIGFPE 8 Issued if an illegal mathematical operation is attempted
SIGKILL 9 If a process gets this signal it must quit immediately and will
not perform any clean-up operations
SIGALRM 14 Alarm Clock signal (used for timers)
SIGTERM 15 Software termination signal (sent by kill by default).
When you press the Ctrl+C or Break key at your terminal during execution of a sh
ell program, normally that program is immediately terminated, and your
command prompt returned. This may not always be desirable. For instance, you may
end up leaving a bunch of temporary files that won't get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syn
tax:
$ trap commands signals
Example:
$ trap "rm -f $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 2
you can remove some files and then exit if someone tries to abort the program fr
om the terminal
From the point in the shell program that this trap is executed, the two files wo
rk1$$ and dataout$$ will be automatically removed if signal number 2
is received by the program
Signal number 1 is generated for hangup: Either someone intentionally hangs up t
he line or the line gets accidentally disconnected.
You can modify the preceding trap to also remove the two specified files in this
case by adding signal number 1 to the list of signals:
$ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2
Now these files will be removed if the line gets hung up or if the Ctrl+C key ge
ts pressed.
The commands specified to trap must be enclosed in quotes if they contain more t
han one command. Also note that the shell scans the command line at
the time that the trap command gets executed and also again when one of the list
ed signals is received.
So in the preceding example, the value of WORKDIR and $$ will be substituted at
the time that the trap command is executed. If you wanted this
substitution to occur at the time that either signal 1 or 2 was received you can
put the commands inside single quotes:
$ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2
doTail() {
while :
do
trap break INT
tail -F "${1}"
done
}
doTail /var/log/messages
Ignoring Signals:
If the command listed for trap is null, the specified signal will be ignored whe
n received. For example, the command:
$ trap '' 2
Specifies that the interrupt signal is to be ignored. You might want to ignore c
ertain signals when performing some operation that you don't want
interrupted. You can specify multiple signals to be ignored as follows:
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is
not equivalent to writing the following, which has a separate
meaning of its own:
$ trap 2
If you ignore a signal, all subshells also ignore that signal. However, if you s
pecify an action to be taken on receipt of a signal, all subshells will still ta
ke the default action on receipt of that signal.
Resetting Traps:
After you've changed the default action to be taken on receipt of a signal, you
can change it back again with trap if you simply omit the first
argument; so
$ trap 1 2
resets the action to be taken on receipt of signals 1 or 2 back to the default.
Killing a Job
=============
You can terminate Unix jobs in different ways. A simple way is to bring the job
to foreground and terminate it, with control-c for example.
The Unix command kill can be used to terminate a background process. You can fol
low kill either by the job number (prefixed with %) or the process
identification number (PID). A common misconception is that the -9 (SIGKILL sign
al) is required to terminate a process. This is a bad practice
because the SIGKILL signal does not allow the process to gracefully terminate an
d makes it immediately terminate. This can cause problems with memory
management. The proper way to kill a process is with the -2 (SIGINT) signal whic
h allows the process to perform cleanup:
%kill -2 %2
The %2 specifies the PID of the process. To kill using a job number, the percent
sign is omitted:
%kill -2 1367
If the -2 signal does not work, the process may be blocked or may be executing i
mproperly. In this case, use -1 (SIGHUP), -15 (SIGTERM), and then at
last resort -9 (SIGKILL).
jobs
The command jobs is available in many shells and reports the jobs running, the j
ob numbers, the process name (and if you want the process group id
with the option -l). You can use it like this:
%jobs
ps
The command ps will print information about processes currently running. Actuall
y, it is a little more complicated than that. The command prints
information about processes controlled by the current terminal and current effec
tive user id. The process identification number needed by some
commands for controlling the process will be given in this case in the first col
umn of output.
%ps
Using job numbers
Now that you can identify a job's number using jobs or ps, you can use them to c
ontrol jobs. If you have several jobs running in background, you can
select one to bring to foreground by using its job number. Instead of using bare
fg you add the job number like this:
%fg 2
To move a started job to background
If you start a job in the foreground, you can move it to background. First, you
stop the job with control-z, then use the command bg to send the
stopped job to background. So, we might start the web browser Lynx, stop it, sen
d it to the background like this:
%lynx
^z
%bg
Redirection in Unix
===================
If you want to send a job to background use "&" but be careful when this job als
o logs to a log file.
Eg: sub.ksh & >/tmp/logfile 2>&1 #THIS WILL NOT WORK
The "&" has to appear after the log file
Eg: sub.ksh >/tmp/logfile 2>&1 & #THIS WILL WORK
DATE
====
To display date
%date
For formatting date -
Use the date command with a format like this:
date +"%m/%d/%Y %H:%M:%S $HOSTNAME"
To get hundredths of seconds, you may need to do some text processing like this:
DATE=date +'%m/%d/%Y %H:%M:%S.%N'
DATE=${DATE%???????}
DATE="$DATE $HOSTNAME"
This is because date offers seconds, nanoseconds, and nothing in between!
PATH and SID
PATH=$PATH:$HOME/bin:$ORACLE_HOME:$ORACLE_HOME/bin:$ASM_HOME:$ASM_HOME/bin
export PATH
export ORACLE_SID=VMDEVDB
echo $ORACLE_SID
For Oracle to work properly, ORACLE_HOME should be set to /apps01/app/oracle/pro
duct/10.2.0/db_1 whilst ORACLE_PATH should be
/apps01/app/oracle/product/10.2.0/db_1/bin
SET Commands in VI
==================
After you do a search in vim you get all the occurrences highlighted, how can yo
u disable that?
to disable
set nohlsearch OR :noh
or to switch
set hlsearch!
Add the following to your .vimrc:
"This unsets the "last search pattern" register by hitting return
nnoremap <CR> :noh<CR><CR>
Thus, after your search, just hit return again in command mode, and the highligh
ting disappears
To clear the last used search pattern:
:let @/ = ""
Turn On or Off Color Syntax Highlighting In vi
:syntax off
OR
:syntax on
Remove all white lines in the file
:g/^$/ d
DIFFERENT SHELLS
================
In BASH - function fname() {}
In KSH - fname() {}
$ STATUS
========
Last command successful? - $?
Parameters are named $1, $2... $n
FTP using scripts
=================
One method is to do this -
ftp -n $HOST <<DO_THIS
quote USER $USER
quote PASS $PASS
ls
quit
DO_THIS
Note: REMEMBER DO_THIS should end in a newline and cannot have a space " DO_THIS
". That will result in script error!
Another method -
ftp -n $HOST <<DO_THIS
user $USER $PASS
ls
quit
DO_THIS
Word Count trick
================
wc -l <filename>
prints number of lines PLUS filename
If you'd like to leave out the filename, pipe it to the command -
wc -l < file.txt
Word Count in compressed files
==============================
Use zcat!
zcat 201401_ADI_Lit_CDO.txt.gz | wc -l
594
Note: If zcat doesn't work, try GZCAT!
The $ in UNIX
=============
$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invo
ked. Here n is a positive decimal number corresponding to the position of
an argument (the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments,
$* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives t
wo arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the
process ID under which they are executing.
$! The process number of the last background command.

Вам также может понравиться