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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read list of filenames from text file and remove these files in multiple directories
# 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!! >Smilie
# 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 07: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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

2. UNIX for Beginners Questions & Answers

awk GSUB read field values from multiple text files

My program run without error. The problem I am having. The program isn't outputting field values with the column headers to file.txt. Each of the column headers in file.txt has no data. MEMSIZE SECOND SASFoundation Filename The output results in file.txt should show: ... (1 Reply)
Discussion started by: dellanicholson
1 Replies

3. Shell Programming and Scripting

Read multiple text files and copy data to csv

hi i need to extract lines from multiple files to a csv file. for example, i have these 3 files file1.txt date:29dec1980 caller:91245824255 called:8127766 file2.txt date:11apr2014 caller:9155584558 called:8115478 file3.txt date:25jun2015 caller:445225552 called:8117485 (30 Replies)
Discussion started by: lp.descamps
30 Replies

4. Shell Programming and Scripting

There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name. ex. abc1234_050201 abc1234_050206 abc1234_050208 xyz34_050204 xyz34_050210 xyz34_050218 thanks (4 Replies)
Discussion started by: grand_sam
4 Replies

5. UNIX for Advanced & Expert Users

ls -ltr a list of filenames-with-spaces within a text file

OS: RHEL 5.8 shell: bash 3.2.25 Directory /home/guest/ contains these files: file a file b file c fileD fileE fileF testFile.txt I'm trying to find the syntax to run ls -ltr against this list of files that is contained within a text file, testFile.txt. The file testFile.txt has... (4 Replies)
Discussion started by: uschaafm
4 Replies

6. Shell Programming and Scripting

Remove files from subdirectories given a list of filenames

Dear all, I have a dir structure like 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... (15 Replies)
Discussion started by: yogeshkumkar
15 Replies

7. Shell Programming and Scripting

Remove filenames beginning with multiple dots

hi all, I want to remove filenames beginning with multiple dots.how I can do this. Thanks in advance (5 Replies)
Discussion started by: sriharsharavi
5 Replies

8. Shell Programming and Scripting

Remove Duplicate Filenames in 2 very large directories

Hello Gurus, O/S RHEL4 I have a requirement to compare two linux based directories for duplicate filenames and remove them. These directories are close to 2 TB each. I have tried running a: Prompt>diff -r data1/ data2/ I have tried this as well: jason@jason-desktop:~$ cat script.sh ... (7 Replies)
Discussion started by: jaysunn
7 Replies

9. Shell Programming and Scripting

a remove script taken in input a file which contain a list of directories

Hi, I'm looking to delete some files from directories. I've just put in a file the location of these files. e.g: in file supprs.txt there is: /usr/host/t1.txt /etc/dev/u1.java /home/new/files/view.c Is it possible to take this file "supprs.txt" as a parameter in a shell command ? (2 Replies)
Discussion started by: yeclota
2 Replies

10. 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
Login or Register to Ask a Question