Send Email with attachment in Unix script
#======================================================
=====
#Email Body
#======================================================
=====
MESSAGE="Hi,
Thanks
Naren
"
echo =======================================================
========
echo "Emailing output files to $email_id"
echo =======================================================
========
(echo "$MESSAGE"; uuencode $output.txt)| mail -s
"Test Email subject : $today_date" $email_id
#======================================================
=====
Connect to Oracle and execute select statement from UNIX
ORACLE_PW=`echo "$1"`
OUTFILE_PATH=`$ORACLE_HOME/bin/sqlplus -s ${ORACLE_PW} << GET_OUTFILE
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET ECHO OFF
select outfile_name
from fnd_concurrent_requests
where request_id = ${REQ_ID}
AND requested_by = ${USER_ID};
exit;
GET_OUTFILE`
2nd method
FCP_LOGIN=`echo $1 | awk '{print $3}'|sed "s/FCP_LOGIN=\"//; s/\"//"`
get_path() {
path=`echo "$FCP_LOGIN
set pagesize 0
select directory_path
from all_directories where directory_name ='Test_OUTBOUND';
exit;"|sqlplus -s`
if [ $? -ne 0 ]
then
echo "Unable to find the Directory Path"
return 1
fi
return 1
}
get_path
echo 'Path ' $path
Find files from unix prompt
find . -name "*.txt" -print
Find the word in all UNIX files under a directory
In below example search for word 'POSPAY_FTP' in all files under a directory
grep -r 'POSPAY_FTP' *
in short grep -r 'string1' directory path
Check for directory existence and is writable.
Example
OUT_DIR=$XXP_TOP/data/outbound
if [ ! -d ${OUT_DIR} -o ! -w ${OUT_DIR} ]
then
echo "================================================================="
echo " ERROR: Directory ${OUT_DIR} does not exist or is not writeable"
echo "======================================================================"
exit 20
fi
-----------------------------------------------------------
Connect to Oracle and execute select statement from UNIX
ORACLE_PW=`echo "$1"`
OUTFILE_PATH=`$ORACLE_HOME/bin/sqlplus -s ${ORACLE_PW} << GET_OUTFILE
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET ECHO OFF
select outfile_name
from fnd_concurrent_requests
where request_id = ${REQ_ID}
AND requested_by = ${USER_ID};
exit;
GET_OUTFILE`
2nd method
FCP_LOGIN=`echo $1 | awk '{print $3}'|sed "s/FCP_LOGIN=\"//; s/\"//"`
get_path() {
path=`echo "$FCP_LOGIN
set pagesize 0
select directory_path
from all_directories where directory_name ='Test_OUTBOUND';
exit;"|sqlplus -s`
if [ $? -ne 0 ]
then
echo "Unable to find the Directory Path"
return 1
fi
return 1
}
get_path
echo 'Path ' $path
Find files from unix prompt
find . -name "*.txt" -print
Find the word in all UNIX files under a directory
In below example search for word 'POSPAY_FTP' in all files under a directory
grep -r 'POSPAY_FTP' *
in short grep -r 'string1' directory path
Check for directory existence and is writable.
Example
OUT_DIR=$XXP_TOP/data/outbound
if [ ! -d ${OUT_DIR} -o ! -w ${OUT_DIR} ]
then
echo "================================================================="
echo " ERROR: Directory ${OUT_DIR} does not exist or is not writeable"
echo "======================================================================"
exit 20
fi
-----------------------------------------------------------
Find Command in Unix and Linux Examples
Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy. The syntax of find command is
Let see some practical exercises on using find command.
1. How to run the last executed find command?
This will execute the last find command. It also displays the last find command executed along with the result on the terminal.
2. How to find for a file using name?
This will find all the files with name "sum.java" in the current directory and sub-directories.
3. How to find for files using name and ignoring case?
This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.
4. How to find for a file in the current directory only?
This will find for the file "sum.java" in the current directory only
5. How to find for files containing a specific word in its name?
It displayed all the files which have the word "java" in the filename
6. How to find for files in a specific directory?
This will look for the files in the /etc directory with "java" in the filename
7. How to find the files whose name are not "sum.java"?
This is like inverting the match. It prints all the files except the given file "sum.java".
8. How to limit the file searches to specific directories?
You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?
b. How to print the files in the current directory and two levels down to the current directory?
c. How to print the files in the subdirectories between level 1 and 4?
9. How to find the empty files in a directory?
10. How to find the largest file in the current directory and sub directories
The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories
Another method using find is
12. How to find files based on the file type?
a. Finding socket files
b. Finding directories
c. Finding hidden directories
d. Finding regular files
e. Finding hidden files
13. How to find files based on the size?
a. Finding files whose size is exactly 10M
b. Finding files larger than 10M size
c. Finding files smaller than 10M size
14. How to find the files which are modified after the modification of a give file.
This will display all the files which are modified after the file "sum.java"
15. Display the files which are accessed after the modification of a give file.
16. Display the files which are changed after the modification of a give file.
17. How to find the files based on the file permissions?
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".
18. Find the files which are modified within 30 minutes.
19. Find the files which are modified within 1 day.
20. How to find the files which are modified 30 minutes back
21. How to find the files which are modified 1 day back.
22. Print the files which are accessed within 1 hour.
23. Print the files which are accessed within 1 day.
24. Display the files which are changed within 2 hours.
25. Display the files which are changed within 2 days.
26. How to find the files which are created between two files.
So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.
1. How to find the permissions of the files which contain the name "java"?
Alternate method is
2. Find the files which have the name "java" in it and then display only the files which have "class" word in them?
3. How to remove files which contain the name "java".
This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.
find [pathnames] [conditions]
Let see some practical exercises on using find command.
1. How to run the last executed find command?
!find
This will execute the last find command. It also displays the last find command executed along with the result on the terminal.
2. How to find for a file using name?
find -name "sum.java" ./bkp/sum.java ./sum.java
This will find all the files with name "sum.java" in the current directory and sub-directories.
3. How to find for files using name and ignoring case?
find -iname "sum.java" ./SUM.java ./bkp/sum.java ./sum.java
This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.
4. How to find for a file in the current directory only?
find -maxdepth 1 -name "sum.java" ./sum.java
This will find for the file "sum.java" in the current directory only
5. How to find for files containing a specific word in its name?
find -name "*java*" ./SUM.java ./bkp/sum.java ./sum.java ./multiply.java
It displayed all the files which have the word "java" in the filename
6. How to find for files in a specific directory?
find /etc -name "*java*"
This will look for the files in the /etc directory with "java" in the filename
7. How to find the files whose name are not "sum.java"?
find -not -name "sum.java" . ./SUM.java ./bkp ./multiply.java
This is like inverting the match. It prints all the files except the given file "sum.java".
8. How to limit the file searches to specific directories?
find -name "sum.java" ./tmp/sum.java ./bkp/var/tmp/files/sum.java ./bkp/var/tmp/sum.java ./bkp/var/sum.java ./bkp/sum.java ./sum.java
You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?
find -maxdepth 2 -name "sum.java" ./tmp/sum.java ./bkp/sum.java ./sum.java
b. How to print the files in the current directory and two levels down to the current directory?
find -maxdepth 3 -name "sum.java" ./tmp/sum.java ./bkp/var/sum.java ./bkp/sum.java ./sum.java
c. How to print the files in the subdirectories between level 1 and 4?
find -mindepth 2 -maxdepth 5 -name "sum.java" ./tmp/sum.java ./bkp/var/tmp/files/sum.java ./bkp/var/tmp/sum.java ./bkp/var/sum.java ./bkp/sum.java
9. How to find the empty files in a directory?
find . -maxdepth 1 -empty ./empty_file
10. How to find the largest file in the current directory and sub directories
find . -type f -exec ls -s {} \; | sort -n -r | head -1
The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories
find . -type f -exec ls -s {} \; | sort -n -r | tail -1
Another method using find is
find . -type f -exec ls -s {} \; | sort -n | head -1
12. How to find files based on the file type?
a. Finding socket files
find . -type s
b. Finding directories
find . -type d
c. Finding hidden directories
find -type d -name ".*"
d. Finding regular files
find . -type f
e. Finding hidden files
find . -type f -name ".*"
13. How to find files based on the size?
a. Finding files whose size is exactly 10M
find . -size 10M
b. Finding files larger than 10M size
find . -size +10M
c. Finding files smaller than 10M size
find . -size -10M
14. How to find the files which are modified after the modification of a give file.
find -newer "sum.java"
This will display all the files which are modified after the file "sum.java"
15. Display the files which are accessed after the modification of a give file.
find -anewer "sum.java"
16. Display the files which are changed after the modification of a give file.
find -cnewer "sum.java"
17. How to find the files based on the file permissions?
find . -perm 777
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".
18. Find the files which are modified within 30 minutes.
find . -mmin -30
19. Find the files which are modified within 1 day.
find . -mtime -1
20. How to find the files which are modified 30 minutes back
find . -not -mmin -30
21. How to find the files which are modified 1 day back.
find . -not -mtime -1
22. Print the files which are accessed within 1 hour.
find . -amin -60
23. Print the files which are accessed within 1 day.
find . -atime -1
24. Display the files which are changed within 2 hours.
find . -cmin -120
25. Display the files which are changed within 2 days.
find . -ctime -2
26. How to find the files which are created between two files.
find . -cnewer f1 -and ! -cnewer f2
So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.
1. How to find the permissions of the files which contain the name "java"?
find -name "*java*"|xargs ls -l
Alternate method is
find -name "*java*" -exec ls -l {} \;
2. Find the files which have the name "java" in it and then display only the files which have "class" word in them?
find -name "*java*" -exec grep -H class {} \;
3. How to remove files which contain the name "java".
find -name "*java*" -exec rm -r {} \;
This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.