Remove files from subdirectories given a list of filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove files from subdirectories given a list of filenames
# 1  
Old 03-11-2012
Remove files from subdirectories given a list of filenames

Dear all,

I have a dir structure like
Code:
                                    main_dir
                  At_nn                Ag_js              Nf_hc .... 
           mcd32 mgd43...     mcd32 mgd43...         mcd32 mgd43...

and each subdir (e.g. mcd32, mgd43) contains files.

Now, i have a list (tobedelete.txt) of filenames (not the complete path). I want to delete only those files which are in the tobedelete.txt.

I am trying like
first I go to the dir main_dir and
Code:
find . -name tobedelete.txt -exec rm {};

but doesnt work.
any comment will be appreciated.
cheers.
# 2  
Old 03-11-2012
You could try the following;

Code:
for i in `cat tobedelete.txt`
do
find . -name ${i} -exec rm {} \;
done

Although to be sure it's going to remove the right files i'd do this first.

Code:
for i in `cat tobedelete.txt`
do
find . -name ${i} -print
done

Regards

DaveSmilie
# 3  
Old 03-11-2012
Quote:
find . -name tobedelete.txt -exec rm {};
but doesnt work.
There is a fundamenal understanding problem here, The "find" command posted will look for a file called "tobedelete.txt" and delete that file (not the files listed in that file).

The code posted by "gull04" is scary.

Please post the actual directory structure in machine-readable format:
# 4  
Old 03-11-2012
I've used this type of routine many times, in view of the original post made by yogeshkumkar and given that I pointed out that he should print out the list of the find command I'd just like to understand how my code was "scary".

I'm quite happy to admit that I'm as likely to screw up as the next guy, but in this case I can't really see what is wrong with the advice that I gave. As I'm always keen to learn, I'd appreciate your feedback.

I don't mean to be critical, I'm just interested to know - I wouldn't deliberately give someone poor advice. I really didn't think that my post was too far off the mark, I use "find" this way frequently - I'm quite happy to admit my shortfalls but in this case I thought the advice was OK. Based on the original post I didn't think this was poor advice, I was just trying to solve a problem for someone.

Perhaps I should have said that the code should be "rm -i" and you can check each file prior to deletion.

Code:
for i in `cat tobedelete.txt`
do
find . -name ${i} -exec rm -i {} \;
done

Regards

Dave

Last edited by gull04; 03-11-2012 at 08:24 PM.. Reason: Typo
# 5  
Old 03-11-2012
Thanks for the comments Methyl and Dave.
The answer by Dave doesnt work. it simply removes the content of the tobedeted.txt.

The actual dir structure is like

in the main_dir
there are subdirs like AAA, BBB, CCC, ....

inside AAA there are subdirs like aa1, aa2, aa3, ...
inside aa1 there are files like AAA_aa1_file1.txt, AAA_aa1_fileextra.txt, AAA_aa1_mod43a4.txt, ....
inside aa2 there are files like AAA_aa2_file1.txt, AAA_aa2_fileextra.txt, AAA_aa2_mod43a4.txt, ....

inside BBB there are subdirs like bb1, bb2, bb3, ...
inside bb1 there are files like BBB_bb1_file1.txt, BBB_bb1_fileextra.txt, BBB_bb1_mod43a4.txt, ....
inside bb2 there are files like BBB_bb2_file1.txt, BBB_bb2_fileextra.txt, BBB_bb2_mod43a4.txt, ....

and so on...up to many hundred of files. I managed to the get the filenames which I must delete.

e.g. the content of tobedeleted.txt is

AAA_aa1_fileextra.txt
BBB_bb1_file1.txt
BBB_bb1_mod43a4.txt
CCC_cc4_mfg54d2.txt
GGG_gg6_hd21s1.txt
and so on....

i hope there is a way!

Last edited by yogeshkumkar; 03-11-2012 at 09:49 PM..
# 6  
Old 03-11-2012
Hi yogeshkumkar,

What does the command Return?

Code:
for i in `cat tobedelete.txt`
do
find . -name ${i} -print
done

Just for information, the tobedeleted.txt file should be in the directory where you are running the script from or you should enter the pat to the file as well such as "for i in `/path/to/file/tobedeleted.txt`"

Regards

Dave

Last edited by gull04; 03-11-2012 at 09:54 PM..
# 7  
Old 03-11-2012
Thanks Dave,

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

the file "tobedeted.txt" is in the same dir where AAA, BBB, and so on are.

pwd: /my/path/main_dir/
find . -name "tobedeleted.txt" | xargs rm

it works only if I have full path of each filename in the tobedeted.txt

how can i create the full path for each filename?

Last edited by yogeshkumkar; 03-12-2012 at 02:14 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to list the names of the files from all the subdirectories?

Hi, I'm currently trying to print the names of all the .txt files in the subdirectories that contain the string I'm searching. I tried with this code, but it seems that it searches for the names that matches the string instead of searching for the string in the individual files and printing the... (2 Replies)
Discussion started by: nuclearpenguin
2 Replies

2. UNIX for Advanced & Expert Users

List all files in subdirectories which are modifiled recently.

Hello, I wanted to list all files in subdirectories which are modifiled recently. need to display all files with full details like hpw it display with ls -l ( date, size,..) Thanks Bala (3 Replies)
Discussion started by: balareddy
3 Replies

3. Shell Programming and Scripting

List files with *.i extension in a directory and all its subdirectories + 30days old then remove

I need to write a script to : list files with *.i extension in a directory and all its subdirectories + 30days old, save it in a file and then remove (2 Replies)
Discussion started by: lena keung
2 Replies

4. Shell Programming and Scripting

Filter only gz files from list of subdirectories

Hi, I have a very big directory structure that consists of many sub-directories inside.There are around 50 ".gz" files under this dir structure. I want to copy all the gz files alone to a seperate location. Plz help me. (2 Replies)
Discussion started by: villain41
2 Replies

5. UNIX for Dummies Questions & Answers

How to remove directory with subdirectories and files?

I'm trying to remove several directories which contains sun-dirs and files inside. I used the command rm -r <dirname> But, it always ask "examine file in directory <dirname> yes/no?" line by line. So, i need to write "y" for every line. How can i skip this step and remove all directories with... (9 Replies)
Discussion started by: ppa108
9 Replies

6. UNIX for Dummies Questions & Answers

list the files but exclude the files in subdirectories

If I execute the command "ls -l /export/home/abcde/dev/proj/code/* | awk -F' ' '{print $9}' | cut -d'/' -f6-8" it will list all the files in /export/home/abcde/dev/proj/code/ directory as well as the files in subdirectories also proj/code/test.sh proj/code/test1.c proj/code/unix... (8 Replies)
Discussion started by: shyjuezy
8 Replies

7. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

8. Shell Programming and Scripting

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. UNIX for Dummies Questions & Answers

list largest files in a directory & its subdirectories

I need to find the largest files in a directory & it's subdirectories. I'm not sure what options on ls -l will work to give me this. or is there another way to do this? Thanks, igidttam (6 Replies)
Discussion started by: igidttam
6 Replies

10. Shell Programming and Scripting

How to Remove Ctrl M characters in files from directories and its subdirectories

Hi, How to recursively remove Ctrl M characters in files from a directory and its sub directory ? I know unix2dos command is there but to remove in bunch of files ... ? Thanks (7 Replies)
Discussion started by: skdp
7 Replies
Login or Register to Ask a Question