Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a list of directories that I want to go into and delete particular files. For example, the directory names I have are as below:

091513
090213
082612
071611
020908
011009
...
062308

Each of these directories has 4 files in them and the file names are:

dealer_score_01.txt
dealer_score_02.txt
dealer_score_03.txt
dealer_score_04.txt

I want to write a script that would go into each of these directories and delete:

dealer_score_01.txt
dealer_score_03.txt

Can someone please help me with that?

share|improve this question
    
man find, read the exec and name parts –  dawud Sep 18 '13 at 7:15

1 Answer 1

You don't have to go into each directory separately. Just pass the file names you want deleted to find and delete them with the -delete option.

find . -type f \
\( -name 'dealer_score_01.txt' -or -name 'dealer_score_03.txt' \) -delete

Leave out -delete to make sure you delete the right files.

share|improve this answer
    
Hi Slhck, Thanks for the reply. I want to delete these files only in certain directories.If i use the command above, it would delete these files in all the directories. Is there any way to delete only in few directories by specifying the directory names in which i want these files to be deleted? –  user255223 Sep 18 '13 at 8:55
1  
You can just specify the directory names instead of . for the find command. For example find 091513 090213 -type f … –  slhck Sep 18 '13 at 9:08
    
How many directories can I specify at a time in the find command? I have more than 200 directories that I want to do this with... –  user255223 Sep 18 '13 at 12:08
1  
You can specify as many as you want. How do you get the list of directories in the first place? In a text file? From a command? Maybe you should clarify your question and tell us what exactly you need to do. –  slhck Sep 18 '13 at 12:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.