Tuesday, November 19, 2013

Delete Files Older Than n Days on Linux


The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.
Command Syntax
find /path/to/files* -mtime +5 -exec rm {} \;
Note that there are spaces between rm, {}, and \;
Explanation
  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.

Monday, October 14, 2013

Reading self path/name/parameter from bash

Below is script and test run to get parameter,path,script file name etc.

#!/bin/bash

echo
echo "arguments called with ---->  ${@}     "
echo "\$1 ---------------------->  $1       "
echo "\$2 ---------------------->  $2       "
echo "path to me --------------->  ${0}     "
echo "parent path -------------->  ${0%/*}  "
echo "my name ------------------>  ${0##*/} "
echo
exit

# ------------- Test script------------- #

$  
/home/myscript/my_setting.sh
"'My name is'" "'Ryan'"
# ------------- RESULTS ------------- # arguments called with ---> 'My name is' 'Ryan' $1 ----------------------> 'My name is' $2 ----------------------> 'Ryan' path to me --------------> /home/myscript/my_setting.sh parent path -------------> /home/myscript my name -----------------> my_setting.sh

Thursday, August 22, 2013

Script converting seconds to time format

Here is simple script to convert seconds to time (hh:mm:ss) format.

#!/bin/bash
convertsectotime() {
 ((h=${1}/3600))
 ((m=(${1}%3600)/60))
 ((s=${1}%60))
 printf "%02d:%02d:%02d\n" $h $m $s
}
#TIME1="36"
echo "enter seconds:"
read TIME1
echo $(convertsectotime $TIME1)



Wednesday, July 31, 2013

Operators in Bash script

1 String comparison operators

(1) s1 = s2
(2) s1 != s2
(3) s1 < s2
(4) s1 > s2
(5) -n s1
(6) -z s1

(1) s1 matches s2
(2) s1 does not match s2
(3) __TO-DO__
(4) __TO-DO__
(5) s1 is not null (contains one or more characters)
(6) s1 is null

2 Arithmetic operators

+
-
*
/
% (remainder)

3 Arithmetic relational operators

-lt (<)
-gt (>)
-le (<=)
-ge (>=)
-eq (==)
-ne (!=)

Tuesday, July 16, 2013

Splitting string using cut command in Linux

cut command can be used to split the string with specific split character

command:

echo "18:30:00"|cut -d ':' -f1
18

echo "18:30:00"|cut -d ':' -f2

30

echo "18:30:00"|cut -d ':' -f3

00

Sunday, July 14, 2013

split string in bash

here is example using sed command.

echo "my name is;chotelal" | sed -e 's/;/\n/g'
myname is
chotelal


File test option in shell

These are the options to be used in conditional statement to check file attributes.

-e
file exists

example:
      
      if [ -b "$filename" ]
      then
        echo "$filename exist."
      fi

-a
file exists
This is same in effect to –e, but now it is discouraged to use.
-f
file is a regular file (not a directory or device file)
-s
file is not zero size
-d
file is a directory
-b
file is a “block device”
-c
file is a “character device”
-p
file is a “pipe”
-h
file is a “symbolic link”

-L
file is a symbolic link
-S
file is a “socket”
-r
file has read permission (for the user running the test)
-w
file has write permission (for the user running the test)
-x
file has execute permission (for the user running the test)

-k
sticky bit set
Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access.  
-O
you are owner of file
-G
group-id of file same as yours
-N
file modified since it was last read
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
f1 -ef f2
files f1 and f2 are hard links to the same file
!
"not" -- reverses the sense of the tests above (returns true if condition absent).