Deleting files using find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting files using find command
# 1  
Old 10-30-2008
Deleting files using find command

I want to find the files and delete all the files except the last file.
I am using find command , I am sending the find output to a file and getting all the lines except the last one and sending it to the remove command . This is not working. can anyone help me out to do it in the find command itself. Please send me the reply ASAP.

This is one script
#! /bin/ksh
find . -name headdump* >delete.txt
rm -f | cat delete.txt | sed -n '$d; p'

Can I get the result using xargs or exec options in find to get it.The below script will delete all results. Can I filter the output here.

#! /bin/ksh
find . -name heapdump* -exec rm '{}' \; -print >delete.txt
# 2  
Old 10-30-2008
try
Code:
find . -name heapdump* -exec rm {} \ ;

this will delete all the files outputted by find command
or
Code:
find . -name heapdump*|xargs rm

# 3  
Old 10-30-2008
Retain the last file from the Search result

I want to retain the last file .I should not delete the last file. Please send me the command for leaving the last file found.
# 4  
Old 10-30-2008
do you have command called tac installed??
# 5  
Old 10-30-2008
No

NO.I guess we dont ve it.
# 6  
Old 10-30-2008
Please clarify what you mean by the "last file". The order of the output from "find" is not much use for operations based on the age of the file.
If you want to retain the most recently created file, "find" is not the right way.
# 7  
Old 10-30-2008
@Methyl

I guess the last file is the recent file only. Could you kindly help me out if i cannot do it using the find command.
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