Need a script to delete multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need a script to delete multiple files
# 1  
Old 01-06-2006
Error Need a script to delete multiple files

Hello,

I am working with about 500,000 text files and 90% of them are duplicates. I need a way to delete the duplicate ones.

The files are email messages with the following file name examples:

20040129-1457 This is the Subject line.txt
20040129-1457 This is the Subject line-1.txt
20040129-1457 This is the Subject line-2.txt
20040129-1457 This is the Subject line-3.txt
20040129-1457 This is the Subject line-4.txt
20040129-1459 This is the other subject line.txt
20040129-1459 This is the other subject line-1.txt
20040129-1459 This is the other subject line-2.txt
20040129-1459 This is the other subject line-3.txt
20040129-1459 This is the other subject line-4.txt
20040129-1459 This is the other subject line-5.txt

etc etc etc...

Is there any way to delete the ones that are named with the serial at the end?

these files are on a Macintosh running OSX 10.4.3 and it seems that the finder cant handle that many files effectively. A unix script would work better i think.

Thanks for any help.

Ashu
# 2  
Old 01-07-2006
Based on your inputs,
Code:
#!/bin/ksh

ls | while read line; do
        if [ -f "$line" ]; then
                fname=$(echo ${line%%.txt})
                rm -f "${fname}"-*.txt
        fi
done

--NOTE--
The '-f' in rm is to supress all the error messages that you *will* get with this script.
--/NOTE--
# 3  
Old 01-16-2006
Quote:
Originally Posted by blowtorch
Based on your inputs,
Code:
#!/bin/ksh

ls | while read line; do
        if [ -f "$line" ]; then
                fname=$(echo ${line%%.txt})
                rm -f "${fname}"-*.txt
        fi
done

--NOTE--
The '-f' in rm is to supress all the error messages that you *will* get with this script.
--/NOTE--
That works but it is really slow.

The files are actually closer to 1.1 million files at this point.

I was able to use something like this:

gholkar:/1 ashu$ ls | grep "[1-9].txt$"
20040129-1457 FMP Password-1.txt
20040129-1457 FMP Password-2.txt
20040129-1457 FMP Password-3.txt
20040129-1459 Dude-1.txt
20040129-1459 Dude-2.txt

That works to show me the files, but when i add in the xargs to rm the files i get this:

gholkar:/1 ashu$ ls | grep "[1-9].txt$" | xargs rm
rm: 20040129-1457: No such file or directory
rm: FMP: No such file or directory
rm: Password-1.txt: No such file or directory
rm: 20040129-1457: No such file or directory
rm: FMP: No such file or directory
rm: Password-2.txt: No such file or directory
rm: 20040129-1457: No such file or directory
rm: FMP: No such file or directory
rm: Password-3.txt: No such file or directory
rm: 20040129-1459: No such file or directory
rm: Dude-1.txt: No such file or directory
rm: 20040129-1459: No such file or directory
rm: Dude-2.txt: No such file or directory

so how do i work around the spaces in the file names?
# 4  
Old 01-16-2006
Try this :

ls | grep "[1-9].txt$" | xargs -I {} rm "{}"


If there is a space in the file name, then rm assumes it as more than one file.. so enclose the file name in double quotes to resolve the problem.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with column delete from multiple files

sample .csv files with 7 columns.I want to delete the last column from each of the below files but retain their file names (1_ContractDocuments.csv,2_ContractDocuments.csv etc.) There can be more files like 3_ContractDocuments.csv , 4_ContractDocuments.csv . Can you please help source .csv... (5 Replies)
Discussion started by: paul1234
5 Replies

2. Shell Programming and Scripting

Find files not matching multiple patterns and then delete anything older than 10 days

Hi, I have multiple files in my log folder. e.g: a_m1.log b_1.log c_1.log d_1.log b_2.log c_2.log d_2.log e_m1.log a_m2.log e_m2.log I need to keep latest 10 instances of each file. I can write multiple find commands but looking if it is possible in one line. m file are monthly... (4 Replies)
Discussion started by: wahi80
4 Replies

3. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

4. Homework & Coursework Questions

Script to delete multiple users

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi This is my first post.I am learning Unix and finding it difficult to get a handle on the scripting side of... (5 Replies)
Discussion started by: dom17
5 Replies

5. UNIX for Dummies Questions & Answers

How To Delete Multiple Files At Once?

I Want to delete the following files together,what command should i pass for that? (Note:- All Start With .) .bash_logout .bashrc .bash_profile .rtorrent.rc ... (3 Replies)
Discussion started by: anime12345
3 Replies

6. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

7. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

8. Shell Programming and Scripting

script to delete multiple files in remote machine

Requirement Several files in remote machines ought to be deleted via sh. Name of the files to be deleted are know Approach 1) script was written with ftp (requires credential) and delete command. File names were passed as array(iterated via for loop-with ftp+delete commands enclosed within... (1 Reply)
Discussion started by: vkalya
1 Replies

9. Shell Programming and Scripting

Script to delete all data from multiple files

its urgent!!!!!!111 i need a script which can delete data from multiple files. plz if anybody knows the script plz write a mail to me : (Email addresses are not allowed) (5 Replies)
Discussion started by: uni_ajay_r
5 Replies

10. UNIX for Dummies Questions & Answers

when I try to run rm on multiple files I have problem to delete files with space

Hello when I try to run rm on multiple files I have problem to delete files with space. I have this command : find . -name "*.cmd" | xargs \rm -f it doing the work fine but when it comes across files with spaces like : "my foo file.cmd" it refuse to delete it why? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question