👤
Home Man
Search
Today's Posts
Register

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

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 03-09-2007
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!! >
# 2  
Old 03-10-2007
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
# 3  
Old 03-10-2007
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 03-10-2007
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

# 5  
Old 08-07-2008
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..
# 6  
Old 08-07-2008
Quote:
Originally Posted by axel1973
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

👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I custom sort the files in a directory using the filenames in a text file.? KMusunuru UNIX for Beginners Questions & Answers 5 02-15-2017 08:49 PM
awk GSUB read field values from multiple text files dellanicholson UNIX for Beginners Questions & Answers 1 06-05-2016 10:37 PM
Read multiple text files and copy data to csv lp.descamps Shell Programming and Scripting 30 04-05-2016 05:34 PM
There are multiple filenames in the directory.How to return the the lastest files for each file name grand_sam Shell Programming and Scripting 4 02-18-2015 10:08 PM
ls -ltr a list of filenames-with-spaces within a text file uschaafm UNIX for Advanced & Expert Users 4 11-28-2012 07:28 AM
Remove files from subdirectories given a list of filenames yogeshkumkar Shell Programming and Scripting 15 03-15-2012 01:09 PM
Remove filenames beginning with multiple dots sriharsharavi Shell Programming and Scripting 5 08-03-2011 12:00 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 09:45 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password