Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-09-2007
fxvisions fxvisions is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 30 October 2007, 5:24 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 a combination of find, pipe, and xargs may work, but would like to consult some gurus. Thanks in advance!! >Linux
Sponsored Links
    #2  
Old Unix and Linux 03-10-2007
srikanthus2002's Unix or Linux Image
srikanthus2002 srikanthus2002 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 14 October 2011, 3:22 PM EDT
Location: Can u guess...!
Posts: 161
Thanks: 0
Thanked 0 Times in 0 Posts
first you have to copy those files into a directory or any where else
Let us consider "fil1" and fil2 and so on......


find / -type f -name "fil*" -exec rm {} \;

if any help ...let us know
Sponsored Links
    #3  
Old Unix and Linux 03-10-2007
dennis.jacob's Unix or Linux Image
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2017, 12:24 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Please give a try on this:

find dir_name -name file -print -exec tar -cvf arch.tar {} \; -exec rm -f {} \;
Ex:
find ./test_dir -name log_file* -print -exec tar -cvf log_arch.tar {} \; -exec rm -f {} \;

thank you.
    #4  
Old Unix and Linux 03-10-2007
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by fxvisions
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 ,

Does the file contain file names or file extensions?
Quote:
to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like a combination of find, pipe, and xargs may work, but would like to consult some gurus.

The best method will depend on the format of the file, the sanity of the filenames, etc.

The best way might be to move the files to another directory, then use whatever archiving method you like on that directory.

This code will retain the directory structure in the archive directory, so that multiple files of the same name can be accommodated:

Code:
archivedir=$HOME/arc
[ -d "$archivedir" ] || mkdir -p "$archivedir" || exit 1
while IFS= read -r file
do
  find . -name "$file" |
    while IFS= read -r f
    do
      dir=$archivedir/${f%/*}
      [ -d "$dir" ] || mkdir -p "$archivedir" || continue
      mv "$f" "$dir"
    done
done < FILE_WITH_LIST

Sponsored Links
    #5  
Old Unix and Linux 08-07-2008
axel1973 axel1973 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 7 August 2008, 6:06 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
another solution maybe

i found this solutions in some forum lately...
it reads a list of files (with or without pathnames) from a file and removes/deletes them using rm.


Code:
xargs rm -rf </lists/blah.txt

this solutions seems to have a problem if using with files that contains SPACES and stuff..

others use this

Code:
 cat filename.txt | while read file; do rm "$file"; done

this worked for me on files with spaces.

Last edited by axel1973; 08-07-2008 at 06:06 AM..
Sponsored Links
    #6  
Old Unix and Linux 08-07-2008
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by axel1973 View Post
i found this solutions in some forum lately...
it reads a list of files (with or without pathnames) from a file and removes/deletes them using rm.


Code:
xargs rm -rf </lists/blah.txt

this solutions seems to have a problem if using with files that contains SPACES and stuff..

Set IFS to a newline first, then it will work.
Quote:
others use this

Code:
 cat filename.txt | while read file; do rm "$file"; done


There is no need for cat:

Code:
while read file; do rm "$file"; done < filename.txt

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove files from subdirectories given a list of filenames yogeshkumkar Shell Programming and Scripting 15 03-15-2012 01:09 PM
Shell Script Needed to Read a text from a list files kashriram Shell Programming and Scripting 4 11-13-2009 06:44 PM
Remove Duplicate Filenames in 2 very large directories jaysunn Shell Programming and Scripting 7 10-20-2009 08:34 PM
a remove script taken in input a file which contain a list of directories yeclota Shell Programming and Scripting 2 09-09-2008 10:56 AM
read list of filenames from text file, archive, and remove fxvisions Shell Programming and Scripting 5 03-20-2007 09:56 PM



All times are GMT -4. The time now is 11:55 PM.