How to delete more than 7 days file from Target folder?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete more than 7 days file from Target folder?
# 1  
Old 11-16-2012
How to delete more than 7 days file from Target folder?

Hi Team,
I have to remove files which are more than 7 days in my target directory.
I have this script but using this i faced some problems like, removed library files of unix.
Code:
for abc in `find /xxx/abc/OutputFiles/xxxx_*.txt -type f -mtime +5|xargs ls 1`
do
   echo "xxx files are:"$abc
   rm $abc
done

Thanks in Advance.
Regards,
Harris

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 11-16-2012 at 12:45 PM.. Reason: code tags...
# 2  
Old 11-16-2012
Hello harris,

I'm not sure if you mean 7 days (as in your text) or 5 days (in your code), but you could do this in a single command:
Code:
find /xxx/abc/OutputFiles/xxxx_*.txt -type f -mtime +7 -print -exec rm {} \;

This will list each file as it is deleted. If you with to use xargs you could:
Code:
find /xxx/abc/OutputFiles/xxxx_*.txt -type f -mtime +7 | xargs -tn 1 rm

.... and it will do the same.


I do have a question about your find though. Is there really a set of directories that will match xxxx*.txt? That would seem to be the file names. If there are many, when the shell expansion happens, it can blow the command up, so you may be better with:
Code:
find /xxx/abc/OutputFiles -name "xxxx_*.txt" ..... etc.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. UNIX for Advanced & Expert Users

Rsync with --delete but do not delete peer dirs on target

rsync with --delete won't honor the delete if the source is something/*. I want the delete to work, but not to delete directories on the target that are peer to the intended directory. For example, using these source and target file structures: Source on desktop: ~/ Money/ ... (4 Replies)
Discussion started by: JavaMeister
4 Replies

3. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

4. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

rsync delete specific files - from different target folder

Hi, I need to use rsync to delete multiple files(only specified files not all) using --delete option, these files are located in different target folders. Instead of running rsync command multiple times for each file, can we achieve this with one time execution? your help is much... (0 Replies)
Discussion started by: MVEERA
0 Replies

6. Shell Programming and Scripting

rsync delete single file from the target folder

Hi We want to delete a single file from the destiantion directory using rsync. Is it possible to do this ? or Do we have any alternate approaches in rsync( for ex applying some filters ..etc) For ex: ----------------------------------------------- Source (Folder) ... (3 Replies)
Discussion started by: MVEERA
3 Replies

7. Shell Programming and Scripting

Shell Script for moving 3 days old file to Archive Folder

Hi Experts, I have a "Source" folder which may contain some files. I need a shell script which should move all files which are older than 3 days to "Archive" folder. Thanks in Advance... (4 Replies)
Discussion started by: phani333
4 Replies

8. Shell Programming and Scripting

Delete a file after 3 days

Hi.. Am using diff to compare 2 directories(A & B) and as ouput i get a list of files which are found only in directory B ( I used grep & sed to create the list). Now I want to delete the files which are found only in dir B after 3 days. Please help me with this. Thanks CoolAtt (7 Replies)
Discussion started by: coolatt
7 Replies

9. Shell Programming and Scripting

delete file older than N days

Hi, All, I'd like to delete files older than 1 day. I thought the following command find /your_directory -mtime +1-exec rm -f {} \; will do the work, but not so, it seems like it won't delete files unless it is 2 days old or older. the files between 1 day and 2 days old does not... (7 Replies)
Discussion started by: ericaworld
7 Replies

10. UNIX for Advanced & Expert Users

setting a file to delete itself when it is 'n' days old

How would I in unix or linux set a file up to delete itself of the system after it's become ten days old (2 Replies)
Discussion started by: cubs0729
2 Replies
Login or Register to Ask a Question