LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-02-2006, 12:39 PM   #1
ashley75
Member
 
Registered: Aug 2003
Posts: 231

Rep: Reputation: 30
delete multiple files


Hi all,

if I have a bunch of file in one dir and I want to delete everything excpet any files that were create on april-29. how is the command line to delete multiple files at one time???

thanks
 
Old 05-02-2006, 01:32 PM   #2
pljvaldez
Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Squeeze (x86)
Posts: 6,092

Rep: Reputation: 269Reputation: 269Reputation: 269
First I would do this to give you a txt file with a list of all the files that were last MODIFIED (it's difficult to do creation date in linux from what I've read) on April 29, 2006:

Code:
#!/bin/bash

for file in *; do
        mtime=$(stat "$file" -c%y | cut -d" " -f1)
	if [ $mtime == 2006-04-29 ]; then
		echo $file >> files20060429.txt
        fi
done
Look at files20060429.txt and make sure it contains all the files you expect it to. Move it to another directory so that it doesn't get deleted.

Then I would mkdir remove and modify the script to move all the files NOT last modified on 2006-04-29 to this new folder
Code:
#!/bin/bash

for file in *; do
        mtime=$(stat "$file" -c%y | cut -d" " -f1)
	if [ $mtime != 2006-04-29 ]; then
		echo $file
		mv $file ./remove
        fi
done
Then I'd look in the directory and make sure none of the files in files20060429.txt are there by mistake. Then rm -ir remove. And it should remove all the files in the directory you created.

Sorry, forgot to mention, the code above should be put into a text file called whatevernameyouwant.sh and then chmod +x whatevernameyouwant.sh to make it executable. You invoke the script by being in the same directory and then ./whatevernameyouwant.sh

Anyway, use this at your own risk, as I'm not responsible for lost data (another reason why I recommend trying it out with cp instead of mv until you are comfortable with the script doing what you want it to do)...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete files that won't delete? di11rod Linux - Security 7 10-19-2005 09:14 PM
delete files suguname Linux - Newbie 3 07-05-2005 08:49 AM
Delete Files imsajjadali Red Hat 15 07-23-2004 01:12 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM
How to delete the destination files while the source files deleted in cp -u ? myunicom Linux - General 4 09-26-2003 01:13 PM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration