Time To Earn

Saturday, August 24, 2013

Unix Scripts You May Need Day to Day

CleanUp Your Server :

find . -mtime +85 -exec ls -ltr {} \;
find . -mtime +77 -exec rm -rf {} \;
find . -type f -name *.Z* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.dat.Z* -mtime +30 -exec rm -f {} \;
find . -type f -name *.gz* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.gz* -mtime +30 -exec rm -f {} \;
find . -type f -name *.dmp* -mtime +30 -exec ls -ltr {} \;
find . -type f -name *.dmp* -mtime +30 -exec rm -f {} \;
find . -type f -name *.dmp -mtime +2 -exec compress {} \;
find . -type f -name *.dat -mtime +2 -exec compress {} \;
find . -mtime +93 -exec ls -ltr {} \;
find . -mtime +93 -exec mv {} ./archive/ \;
find . -mtime +93 -exec rm -rf {} \;


search exact word :
grep -nw '101' autogen_dimensions.xml
8:         <PVAL>101</PVAL>

Print a particular line with line number in Unix :
awk 'NR==8' autogen_dimensions.xml

# substitute "foo" with "bar" ONLY for lines which contain "baz" {selective substitution}
sed '/baz/s/foo/bar/g' sourcefile > target_file

How Do I Add User Jerry To A Secondary Group Called Sales?
Type the following command:
# useradd -G sales -m jerry
# passwd jerry
How Do I Add /bin/ksh as A Shell While Creating A User Account?
Use the following syntax:
useradd -m -s /bin/ksh usernameHow Do I Setup HomeDirectory Path While Creating A User Account?
Use the following syntax:
useradd -m -s /bin/ksh -d /path/to/home/user usernameYou can pass all those options as follows:
# useradd -d /nas/users/v/vivek -m -s /bin/bash -c "Vivek Gite" vivek
# passwd vivek

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/

To see the port
netstat -an | grep portno

Key Gen :
ssh-keygen -t rsa

./dos2ux fap_item.txt   > fap_item.txt.bak

bad interpreter issue:
perl -i -pe 's/\r//g' inputfile
dos2unix inputfile

symlink creation:
ln -s mysql-advanced-5.5.15-linux2.6-x86_64 mysql
--> pipe the find command with grep with the help of xargs

sed -e '/[^0-9]$/{N;s/\n//;}' testing.txt > new.txt

uuencode tail_file.txt tail_file.txt | mailx -m -s "test" mail_id

$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'


pbrun mailq
/var/spool/mqueue is empty
                Total requests: 0

Unix Top 10 Files:
find /start/dir -type f -exec du {} \; | sort -rn | head -10

find ./ -type d -name .svn -exec rm -rf {} \;


 sed –i '5,7 d' file.txt

 sed –i '$ d' file.txt

  sed '1 d' file.txt > new_file.txt
$> mv new_file.txt file.txt
 sed –n '4 p' test

 $ ln -s 1.3 latest

 $ ln -nsf 1.2 latest

 find . -perm 644

find . –iname "error"–print ( -i is for ignore )
find . -name"*.tmp" -print | xargs rm–f

find . -maxdepth1 -type f -newer first_file

find . -type f -cmin 15 -prune

find . -size +1000c -exec ls-l {} \;

Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block.
to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than,"
and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as in below example of find:

find . -size +10000c -size-50000c-print
find . -mtime +10 -size +50000c -execls -l{} \;


No comments:

Post a Comment