Bash script to delete folder based on text file information


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to delete folder based on text file information
# 1  
Old 01-15-2009
Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file I am new to linux and just learning so if anyone good help me out I would sure appreciated it.

#!/bin/bash
cd /month
folders=$(ls)
echo "$folders" > /file.txt
cat file.txt | \
sed -e s/time1//g -e s/time189//g > customer.txt
rm /file.txt
# 2  
Old 01-15-2009
???
Dont understand much...
You wish to remove a directory (or subdir.) ? If so you would use rmdir, but it has to be empty... The other alternative is rm -rf (man rm rmdir)
# 3  
Old 01-15-2009
Ok I am trying to do a rm -rf for every folder that is listed in my customer.txt so let's say that text file has 3 folder names in it: folder1, folder2,folder3 I want to delete those folder with a rm -rf but the problem is I don't know how to go about this.
# 4  
Old 01-16-2009
Why don't you do
for $i in somefolder/*
do
case $i in
folder1|folder2|folder3)
rm -rf $i
;;
*)
;;
esac
done
# 5  
Old 01-16-2009
I don't understand where you are getting the $1 variable from and I don't understand what the folder1|folder2|folder3 is for if you don't care can you break it down for me or point me to some tutorials.
# 6  
Old 01-16-2009
This is very simple.. really..

Code:
#!/bin/bash

for file in `cat customer.txt`; do
    # -f is for force
    # -r is for recursive
    # -v is for verbose so you can see what it is doing
    rm -rfv $file
done

# 7  
Old 01-16-2009
When I run that script it delete's the customer.txt file and it don't delete the folder's
#!/bin/bash

for file in `cat customer.txt`; do
# -f is for force
# -r is for recursive
# -v is for verbose so you can see what it is doing
rm -rfv $file
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. UNIX for Dummies Questions & Answers

Delete records based on a text file from a text file

Hi Folks, I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted. Data.txt state city zone Alabama Huntsville 4 California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies

4. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

5. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

6. Shell Programming and Scripting

Bash script for new file in ftp folder

Hello, I'm trying to make a bash script that send me e-mail if there is any new file in my ftp folder. cat inotify.sh #!/bin/sh /usr/bin/inotifywait -e create \ -mrq /home/mrowcp | while read line; do echo -n "$line " >> /var/log/inotify.log echo `date | cut -d " " -f1-4` >>... (3 Replies)
Discussion started by: mrowcp
3 Replies

7. Shell Programming and Scripting

Script that delete a File when a DEL File will be placed in same folder

Hi there, i have a question. I have a folder called /usr/test There is a file in it.... test.csv I need not a shell script that checks if there is a file called: test.del And if the file is in the same folder then the script should delete the test.csv and also the test.del. Hope... (9 Replies)
Discussion started by: Bjoern28
9 Replies

8. Shell Programming and Scripting

Parallel delete based flag from text file

Hi, I need a unix shell script for this requirement and is URGENT My input text file contains A-1 B-1 C-1 D-2 E-2 F-3 G-3 H-3 I-3 J-4 K-4 L-5 My expected result should be: if flag is 1, it has to delete A, B, C if flag is 2, it has to delete D,E if flag is 3, it has to delete... (1 Reply)
Discussion started by: moses_a
1 Replies

9. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

10. Shell Programming and Scripting

Bash snippet to find files based on a text file?

Evening all. I'm having a terrible time with a script I've been working on for a few days now... Say I have a text file named top10song.tm2, with the following in it: kernkraft 400 Imagine i kissed a girl Thriller animals hallelujah paint it black psychosocial Oi to the world... (14 Replies)
Discussion started by: DJ Charlie
14 Replies
Login or Register to Ask a Question