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
# 8  
Old 03-11-2012
Hi yogeshkumkar,

Not a problem, glad that I could help.

Regards

Dave

---------- Post updated at 01:44 AM ---------- Previous update was at 12:59 AM ----------

Hi yogeshkumkar,

What output do you get from the find with the "-print" statement?

Regards

Dave
# 9  
Old 03-12-2012
@gull04
Your script contains a couple of common problems:
1) Uses "for" with an open-ended list.
This breaks when the command line gets too long.
Also, the "for" breaks if any filename contains a space character.
2) The "find" is not specific to files

We can turn the script round to make it a bit more robust:
For the diagnostic version we could display what we are searching for,
because I too am having trouble understanding the content of "tobedelete.txt" (or is it "tobedeleted.txt"?). There is nothing in your code which would overwrite the file.

Code:
if [ -f "tobedelete.txt" ]
then
    cat tobedelete.txt | while read filename
    do
           echo "${filename}"
           find . -type f -name "${filename}" -print
           echo ""
    done
else
      echo "File does not exist: tobedelete.txt"
fi

@yogeshkumkar
Please post what Operating System and version you have and what Shell you are using.
This User Gave Thanks to methyl For This Post:
# 10  
Old 03-12-2012
@methyl,

You're absolutely right in your approach to making the script more robust and I must admit there were several basic assumptions made by me that were possibly optimistic.

I did assume;

Code:
IFS='
'

And again I'd say that I took yogeshkumkar at face value when I gave the advice, not that that is any excuse for sloppiness. I should have tested for file types incase there was a link or the likes, should have enclosed the variable to ensure that it worked around spaces etc.

I work in a fairly pressured production/dr/test/development environment. So have four of every machine from M9000's to T5120's where I always have the facility to test - possibly it has made me a little cavalier.

I'll try to remember that not everyone can restore Pb sized file systems in minutes if things go pear shaped
This User Gave Thanks to gull04 For This Post:
# 11  
Old 03-12-2012
@yogeshkumkar
I believe that we are waiting for some answers.

@gull04
Somebody with your experience of decent sized Sun servers is most welcome to continue to contribute to this board.

Personally I have never messed with IFS . There is always a better way.
What is a "Pb sized file system"? To me a "Tb" is quite big enough thank you.
# 12  
Old 03-12-2012
@methyl

Thanks for the vote of confidence, Pb Petabyte 1024Tb - dead easy with ZFS. BTW thanks for being patient with me on the forum, I'm prepared to try - "never been wrong, never done anything".

Regards

Dave
This User Gave Thanks to gull04 For This Post:
# 13  
Old 03-14-2012
Thanks all,

I managed it. It is simple. first create the list with full paths given a list of filenames as

Code:
for i in `cat /path/to/the/filenames.txt`;
  do 
    find /path/to/the/parent/dir/to/search/ ${i} -print;
  done >& filenames_withfullpath.txt

and then simply delete the files from the list as

Code:
find /path/to/the/parent/dir/to/search/ -name "filenames_withfullpath.txt" | xargs cat | xargs rm

cheers,
Yogesh
# 14  
Old 03-14-2012
@yogeshkumkar
DANGEROUS SCRIPT
The first "find" command posted would be a syntax error on some O/S because there is no "-name" parameter. On my O/S it produces a list of EVERY file and directory in the directory named in the first parameter. Thus your script is in danger of deleting EVERY file regardless of whether they appear in "filenames.txt".


I've commented about using "for" with open-ended lists in an earlier post.
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