Deleting files using find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting files using find command
# 8  
Old 10-30-2008
HTML Code:
One method example assuming files are in current directory.   Obviously adapt to your environment and test before deleting anything.

#!/bin/ksh
# Count files
FILE_COUNT=0
FILE_COUNT=`ls -1tdr heapdump* 2>/dev/null|wc -l`
if [ ${FILE_COUNT} -eq 0 ]
then
        exit
fi
# Calculate how many files to process
FILE_MAX=`expr ${FILE_COUNT} - 1`
#
COUNTER=0
ls -1tdr heapdump*|while read FILENAME
do
        COUNTER=`expr ${COUNTER} + 1`
        if [ ${COUNTER} -le ${FILE_MAX} ]
        then 
                # Process file
                ls -ald "${FILENAME}"
        fi
done
# 9  
Old 11-05-2008
Assuming to be in present working directory,i think this should help you.
it will search for all files name heapdump,sed will remove the last file,and awk will generate the rm command and ultimately piping to shell.

Solution:

find heapdump* |sed '$d'|awk '{print "rm -f "$1}'|sh

Steps:
Created four files heapdump1,heapdump2,heapdump3,heapdump4

robot$:ls -ltr
-rw-r--r-- 1 robot mqm 0 Nov 5 2008 heapdump1
-rw-r--r-- 1 robot mqm 0 Nov 5 2008 heapdump2
-rw-r--r-- 1 robot mqm 0 Nov 5 2008 heapdump3
-rw-r--r-- 1 robot mqm 0 Nov 5 2008 heapdump4

robot$:find heapdump*
heapdump1
heapdump2
heapdump3
heapdump4

robot$:find heapdump* |sed '$d'
heapdump1
heapdump2
heapdump3
(Excluded the last file heapdump4)

robot$:find heapdump* |sed '$d'|awk '{print "rm -f "$1}'
rm -f heapdump1
rm -f heapdump2
rm -f heapdump3

& finally
robot$:find heapdump* |sed '$d'|awk '{print "rm -f "$1}'|sh
will remove all files except heapdump4 !
Hope this is helps Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command to find a word from list of files

I need to find a word '% Retail by State' in the folder /usr/sas/reports/RetailSalesTaxallocation. When I tried like below, -bash-4.1$ cd /usr/sas/reports/RetailSalesTaxallocation -bash-4.1$ find ./ -name % Retail by State find: paths must precede expression: Retail Usage: find ... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

2. Shell Programming and Scripting

[Solved] Issue with deleting files through find

Hi, I have a script similar to this #!/bin/ksh cd /orcl/bir/eod_badfiles find ./ -type f -name "*.csv" -mtime +6 -exec rm -f {} \; find ./ -type f -name "*.bad" -mtime +6 -exec rm -f {} \; cd /orcl/bir find ./ -type f -name "*.log" -mtime +6 -exec rm -f {} \; This was working fine in one... (5 Replies)
Discussion started by: Gangadhar Reddy
5 Replies

3. UNIX for Dummies Questions & Answers

Crontab deleting files command question

Hello out there, Our system has a pdf generator that creates pdf files. We dont need them pas 120 days. So I have this command in my crontab. I currently set it to "0" for testing. But normally have it set to -mtime 120 to remove files out of the folders from PDF out to several other potential... (7 Replies)
Discussion started by: vsekvsek
7 Replies

4. Red Hat

find . -name '*.req' -mtime +2 -exec rm {} \; not deleting files

i want to remove *.req files from directory /opt/FFCL8001/oracle/inst/apps/FFCL8001_lhrho/logs/appl/conc/log i executed command find . -name '*.req' -mtime +2 -exec rm {} \; but it is running since hours and free space in /opt is same as old 7.4 GB . why it is not removing files ? (5 Replies)
Discussion started by: rehantayyab82
5 Replies

5. Shell Programming and Scripting

what is the find to command to find the files created last 30 days

what is the find to command to find the files created last 30 days (5 Replies)
Discussion started by: rajkumar_g
5 Replies

6. Shell Programming and Scripting

Deleting all empty files in sub directories with a command

Hello Friends, Im trying to delete empty files in subdirectories with a command. I can find them checking only one directory in each step and then show them with my command like below moreover i could not add removing part: ls -l */* | awk '{if ($5==0) printf "%3s %2d %s... (5 Replies)
Discussion started by: EAGL€
5 Replies

7. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

8. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies

9. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

10. UNIX for Dummies Questions & Answers

Deleting files with zero length using ls command

Hi, How can I use ls command to Delete all files with zero length in a given path using ls command (I guess awk is required!)? Thanks. (6 Replies)
Discussion started by: GNMIKE
6 Replies
Login or Register to Ask a Question