Solved delete files to a particular directory

August 29, 2012 at 05:55:30
Specs: Windows 7
- the script moves files passed in the argument to Particular folder(TRASH)
- the script accepts wildcards
- the script displays the number of files moved using the following format:

empty line
<x> files have been moved to Folder(TRASH)
empty line

- the script accepts the word TRASH as an argument: in this case, it deleted all files older than 2 weeks present in the TRASH directory
- the script requires at least 1 argument; if no argument is supplied the following message needs to be displayed:

empty line
Usage: <script_name> file
empty line
ex: del my_dummy_file.txt
empty line

- the script can be called from any location (without using the full path)
- possible name for the script: del

During the testing phase the behavior of the script needs to be checked also on directories and soft links.


See More: delete files to a particular directory

Report •

✔ Best Answer
September 3, 2012 at 11:20:21
Let's assume your script is called script.ss

First, just a suggestion: make sure your shell invocation, #!/bin/ksh, is on line one column 1. The comments will go under that.

Second, If your plan is to pass regular expressions as the second argument, any meta character has to be escaped or the argument has to be surrounded by double quotes. For example, if you want to find all the files with a txt extension:

# method one
script.ss yourdir \*.txt

# method 2
script.ss yourdir "*.txt"



#1
August 29, 2012 at 08:12:10
What specifically do you need help with?

Report •

#2
August 29, 2012 at 08:56:53
He needs someone to do his homework for him?

Report •

#3
August 31, 2012 at 09:23:59
Hey rajangupta:

I see you asked in another forum for somebody to do your homework. It's not working any better there than it is here. What's sad is if you made even a little effort somebody would have pity and help you out.


Report •

Related Solutions

#4
September 3, 2012 at 00:55:07
Hi guys,

It seems that you have feel offended. It was just a conversatiopn opener. My bad. . I am sorry that if any of you felt offended by my behaviour but i assure you that i am putting my 100% to make this happen.
Anyways i have made the script but some of the parts are still not working for me. Can you please help me out with atleast such detailings.

attached are the script and the testing i have already completed.

waiting for reply. Thanks for being so kind to atleast reply to my post.


Report •

#5
September 3, 2012 at 00:58:43
********SCRIPT**********

#!/bin/ksh


Init(){
SCRIPT=`basename $0 | sed 's/\..*//g'`
SCRIPTDIR=/u01/appl/p1elkpe/upc_scripts/pst/sarvpriya
#TRASH_DIR=/backup/BACKUP_EL603/TRASH/TRASH1
TRASH_DIR=/u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/TRASH
CLEAN_PERIOD=15
Source_DIR=$1
RegexFileName="$2"

}

#Main

#check for number of parameters, number ofp arameters should always be greater than 1 or equal to 1. and less than 3.
#either $1 should be trash literal word trash or $1 should be a path and $2 should be regular expression

if [ $# -lt 1 ] || [ $# -gt 2 ]
then
echo "bad usage. Delscript [TRASH| (path where files have to be deleted)( regex/filename)]";
exit
fi

if [ $# -eq 1 ] && [ $1 != "TRASH" ] && [ $# -eq 1 ] && [ $1 != "trash" ]
then
echo "bad usage. Delscript [TRASH/trash| (path where files have to be deleted)( regex/filename)]";
exit
fi


Init $1 $2

[ $1 == "TRASH" ] || [ $1 == "trash" ] && {
find $TRASH_DIR -mtime +$CLEAN_PERIOD 2>/dev/null | xargs /bin/rm -f || { echo "Problem deleting the files in the $TRASH_DIR"; exit; }
echo "Files older than $CLEAN_PERIOD has been deleted from $TRASH_DIR"
exit
}

#Identify_files_to_be_moved $2

cd $1 2>/dev/null || { echo "There is some problem in the directory you passed as an argument. Please check it and try again"; exit; }
find . \( ! -name . -prune \) -type f -name "$2" 2>/dev/null | gawk '{print "Files to be moved-" $1}' > $SCRIPTDIR/$SCRIPT.5

[ -s $SCRIPTDIR/$SCRIPT.5 ] || { echo "No files found with the matching regex/filename"; exit; }

#Ask the user whether to input these files are not

echo "Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :"
echo "`cat $SCRIPTDIR/$SCRIPT.5`"
echo "`egrep -c . $SCRIPTDIR/$SCRIPT.5` files found matching with regex/filename"

read userinput

case $userinput in
y|Y) break;;
n|N) echo "You choose not to delete the files. Hence existing from script"; exit;;
q|Q) echo "You choose to quit. Hence existing from script"; exit;;
*) echo "Existing script.Not a valid option. Please try again."; exit;;
esac


TSTAMP=`date '+%Y%m%d%H%M%S'`
tarfilename=$TSTAMP.tar


#add the files in the tarfile //this shud be in the trash directory
cat $SCRIPTDIR/$SCRIPT.5 | gawk -F'-' '{print $2}' | xargs tar -cfl $TRASH_DIR/$tarfilename 2>/dev/null || { echo "There is some problem creating tar files. Either the files doesnt exist."; exit; }

rm tmp2
#now delete the files
cat $SCRIPTDIR/$SCRIPT.5 | gawk -F'-' '{print $2 }' | xargs rm 2>/dev/null || { echo "problem deleting the files. Keep in mind that the tar file with name $tarfilename has been created though"; exit;}
echo "files deleted from $Source_DIR and moved to TRASH. The tarfile created is $tarfilename";



Report •

#6
September 3, 2012 at 00:59:07
*******TEST PERFORMED*******

Script is placed in directory /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya
Trash Directory is /backup/BACKUP_EL603/TRASH
Clean period = 15(current configuration)

Script usage
Delscript.sh [<path of the files> <regex/filename> | trash|TRASH]
The script will tar the files matching the regex/filename as a command line parameter($2) from the directory(CLP $1). The tar file will be made in the TRASH directory. Once the tar file is made successfully, then the files will be deleted from the directory.
Once the script identifies the files to be moved, it prompts the user for his approval to delete the files. If the user types y/Y, then only the files will be tared and moved. If the user enters n/N, then the files are not deleted and script finishes. If the user presses Q/q, the script finishes as well.
Other than the above valid input, in all the cases, it displays bad option and it quits.
In case everything goes correct, the files selected are moved and tarred in the TRASH directory. The name of the tarfile is the <current timestamp>.tar

If the CLP passed is trash/TRASH, then script will delete all files(tar or any other) from the TRASH directory defined in the script.
User of the script can only choose one of the above operation i.e either move the files or clean the trash directory at one time.
Notes:
1) Any soft link matching the regex/filename is not included. Hard links will be tared and eventually deleted.
2) If during the process i.e after the script identifies the files to be deleted and before they are deleted, if any other process deletes the file, then following would happen
 If the tar file was not created by that time, then script will throw error and exit.
 If the tar file was created by the time the file got deleted, the script will not give any error during the delete operation as we wanted to delete and if its already deleted then its OK. This is implemented using the rm –f option.
3)

Tescases executed.
1) Pass the script incorrect number of arguments.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh bad usage. Delscript [TRASH| (path where files have to be deleted)( regex to identify the filenames)]

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh adada
bad usage. Delscript [TRASH/trash| (path where files have to be deleted)( regex/filename)]

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya "tmp*" trash
bad usage. Delscript [TRASH| (path where files have to be deleted)( regex/filename)]

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya "tmp*" trash dadfa
bad usage. Delscript [TRASH| (path where files have to be deleted)( regex/filename)]

2) Pass the script incorrect directory.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpiya "tmp*"
There is some problem in the directory you passed as an argument. Please check it and try again

3) User opts for “n” when prompted for confirmation to delete the files.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ls -ltr
total 7
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_243
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_244
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_245
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_246
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_247
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:57 1312_3433_248
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 tmp1

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
n
You choose not to delete the files. Hence existing from script

4) User opts for “N” when prompted for confirmation to delete the files.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
N
You choose not to delete the files. Hence existing from script.

5) User enters anything other than y/Y or n/N, the script exists saying bad option.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
t
Existing script.Not a valid option. Please try again
6) User enters Q when prompted for confirmation to delete files.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
q
You choose to quit. Hence existing from script
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]>
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]>
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]>
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]>
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]>
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
Q
You choose to quit. Hence existing from script

7) Success scenario: Everything goes fine.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ls -ltr
total 9
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 tmp1
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312__3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312_3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_243
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_244
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_245
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_246
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_247
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 12:20 1312_3433_248

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/TRASH$]> ls -ltr
total 0
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
6 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_248
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
y
files deleted from /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source and moved to TRASH. The tarfile created is 20120229133410.tar

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/TRASH$]> ls -ltr
total 9
-rw-rw-r-- 1 p1elkpe cmptl 4096 Feb 29 13:34 20120229133410.tar

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/TRASH$]> cd ../source
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ls -lt
total 3
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312_3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312__3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 tmp1
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]>

8) The test case verifies script behavior when the file matching regular expression gets deleted before the tar file is created.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ls -ltr
total 8
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 tmp1
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312__3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312_3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:43 1312_3433_247
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:43 1312_3433_246
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:43 1312_3433_245
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:43 1312_3433_244
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:43 1312_3433_243

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
5 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> rm 1312_3433_245

y
tar: ./1312_3433_245: No such file or directory
There is some problem creating tar files. Either the files doesnt exist.

9) The same scenario but the file gets deleted after the tar file is created and files moved. In this case there is no error. Operation completes.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "1312_3433_2*"
5 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./1312_3433_245
Files to be moved-./1312_3433_246
Files to be moved-./1312_3433_244
Files to be moved-./1312_3433_243
Files to be moved-./1312_3433_247
Y
files deleted from /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source and moved to TRASH. The tarfile created is 20120229135253.tar


10) If the regex passed, matches a soft link then the script will not include the soft link. In case of hard links, the normal logic applies.

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ln 1312__3433_333 tmphardlink

[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ln -s 1312__3433_333 tmp4
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source$]> ls -ltr
total 9
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 tmp1
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 11:59 1312_3433_333
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:58 tmp2
-rw-rw-r-- 1 p1elkpe cmptl 0 Feb 29 13:58 tmp3
lrwxrwxrwx 1 p1elkpe cmptl 14 Feb 29 13:59 tmp4 -> 1312__3433_333
-rw-rw-r-- 2 p1elkpe cmptl 18 Feb 29 13:59 tmphardlink
-rw-rw-r-- 2 p1elkpe cmptl 18 Feb 29 13:59 1312__3433_333
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "tmp*"
4 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./tmphardlink
Files to be moved-./tmp3
Files to be moved-./tmp1
Files to be moved-./tmp2
Soft link tmp4 is not selected by script.
[p1elkpe@ /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya$]> delscript.sh /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source "tmp*"
4 files found matching with regex/filename
Do you want to move the following files, If yes then enter y/Y else enter n/N(this will exit the script) Enter q|Q to quit :
Files to be moved-./tmphardlink
Files to be moved-./tmp3
Files to be moved-./tmp1
Files to be moved-./tmp2
y
files deleted from /u01/appl/p1elkpe/upc_scripts/pst/sarvpriya/source and moved to TRASH. The tarfile created is 20120229142234.tar
11)
12)



Report •

#7
September 3, 2012 at 01:27:52
How do i make the script accepts the wildcards as well??

Report •

#8
September 3, 2012 at 11:20:21
✔ Best Answer
Let's assume your script is called script.ss

First, just a suggestion: make sure your shell invocation, #!/bin/ksh, is on line one column 1. The comments will go under that.

Second, If your plan is to pass regular expressions as the second argument, any meta character has to be escaped or the argument has to be surrounded by double quotes. For example, if you want to find all the files with a txt extension:

# method one
script.ss yourdir \*.txt

# method 2
script.ss yourdir "*.txt"


Report •

Ask Question