General discussion

Locked

csh scripting

By kleeusmale ·
Hi all,

I'm very new to scripting. Hope someone can help me with the problem that I'm having.

I've a list of files called flist.txt containing name of files
e.g. 11111
22222
33333
44444
55555
and a lot more...

and I have different directories that matches these files.
e.g. /mnt/db/0a/11111_qk
/mnt/db/0b/11111_qa
/mnt/db/0c/11111_qq
/mnt/db/0d/22222_qa
/mnt/db/0e/22222_qb
/mnt/db/0z/44444_qp
/mnt/db/0t/55555_qu
/mnt/db/0t/55555_qr

What I need to do is to find the directories where the file matches the flist.txt and remove the directories.

I have no idea how to go about doing this. Can anyone help?

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Ok, I have to ask some questions

by DanLM In reply to csh scripting

1). you have one file that contains a list of files, correct?

2). Looking at your example, these files listed also have a matching directory? Looking at your example, your directory names seem to have a qalifier on the end that makes them slightly different then the actual file names? Is this correct?

3). Do you know the approximate location of these directories you want to delete? Ie, are they spread out all over the server or are they under a specific directory? Ie:
/dataarea

If you can kick me back some feed back on questions, it really shouldnt be to difficult? A couple variables, loop, and the find command.

I just need some clarifications, thats all. I appoligise for the request for additional information.

Dan

Followup Question: Think you might want to use this same script again, ie: meaning you would have a different file name containing the list of files? And possibly having the directories in a different location?

Collapse -

Answers

by kleeusmale In reply to Ok, I have to ask some qu ...

(1) Correct.
(2) Correct.
(3) They are located at /mnt/db/<different_dir>. I want to delete /mnt/db/<different_dir> if the filename under the <different_dir> is like the list in flist.txt

The flist.txt content could vary from time to time but the location is as (3). The list range is from 1000-3000.
There are approx. 1000 directories and all of these took up 25Gb of the diskspace. Does these give any problem?

Collapse -

ok, here is what I put together

by DanLM In reply to Answers

#!/bin/sh
#
# Leave this as period, this means you must place both this script and the text file #in /mnt/db directory
HighLevelDir=".";
#
# Replace the flist.txt with the name of the #file that will contain the list of files. #This must reside in the same directory as
# the script at the very same level.
#
for FileToCheckFor in `cat flist.txt`
do
WorkVar="${FileToCheckFor}*";
echo "ding ding ding find $HighLevelDir -type d -name $WorkVar -print";
for DirFound in `find $HighLevelDir -type d -name "$WorkVar" -print`
do
echo "foudn $DirFound";

if [ -e "${DirFound}/$FileToCheckFor" ]
then
#
# When after running this with these echo's in it, and you feel

#that it is only finding files you want.
# uncomment the rm line
echo "wack goes directory ${DirFound}"
#`rm -rf ${DirFound}`
fi

done
done


---------------------------------------------
for some bloody reason, I couldn't get the damn find to work any other way then placing it in the /mnt/db directory. I tested this against 3 directories. The file contained 2 lines representing the files I was looking for.
111111
222222

File 111111 was placed in two directories, and the file 222222 was placed in one. Each of these directories were deleted with this script. I also tested this script with some files not being found, and only the directories that I expected to be deleted did.

The logic of this script is as follows.
High level loop reads your file line by line. It then use's what is in that line to perform the find command from the current directory structure down looking for directory names that have the qualifier of the file name in it. This is in a loop in case your file is in multiple directories. The directory is not deleted unless the file is actually in it.

Run this, test this. If you have any problems with it. Private message me, I'll remove the block from my profile so that you can.

I don't know how well this will hold up to thousands of files, or how long it will run. And I apologize for the sloppiness of the script, I have done better. But, it should work.

roflmao, and for gods sakes. Please forgive my echo statements... They were put in for echoing variables so I could see what was being built into them. I just have a tendency to be ummmm, unorthodox in the way I do these types of displays.

Dan

Collapse -

structure

by kleeusmale In reply to Answers

not sure if u got my file structures right...but here's the structure.

$ ll /mnt/db
total 112
drwxrwxr-x 3 99 tty 8192 Feb 6 2006 00at
drwxrwxr-x 3 99 tty 8192 Feb 6 2006 00bs
drwxrwxr-x 3 99 tty 8192 Aug 19 2006 00xa
drwxrwxr-x 3 99 tty 8192 Feb 22 2006 00dz

$ ll /mnt/db/00at
total 1936
-rwxrwxr-x 1 99 tty 774142 Feb 6 2006 11111_qa
-rw-rw-r-- 1 99 tty 7156 Feb 6 2006 oth_file
-rw-rw-r-- 1 99 tty 5068 Feb 6 2006 td
-rw-rw-r-- 1 99 tty 72 Feb 6 2006 parameter
drwwrwwr-x 1 99 tty 128 Feb 6 2006 verified_file

$ ll /mnt/db/00xa
total 1936
-rwxrwxr-x 1 99 tty 774142 Feb 6 2006 22222_qa
-rw-rw-r-- 1 99 tty 7156 Feb 6 2006 oth_file
-rw-rw-r-- 1 99 tty 5068 Feb 6 2006 td
-rw-rw-r-- 1 99 tty 72 Feb 6 2006 parameter
drwwrwwr-x 1 99 tty 128 Feb 6 2006 verified_file

$ ll /mnt/db/00dz
total 1936
-rwxrwxr-x 1 99 tty 774142 Feb 6 2006 11111_qk
-rw-rw-r-- 1 99 tty 7156 Feb 6 2006 oth_file
-rw-rw-r-- 1 99 tty 5068 Feb 6 2006 td
-rw-rw-r-- 1 99 tty 72 Feb 6 2006 parameter
drwwrwwr-x 1 99 tty 128 Feb 6 2006 verified_file

Collapse -

Ok, I see why this isnt working

by DanLM In reply to structure

there is a couple reasons.

1). I origionally thought that files would be like 111111, and the directory would be 111111_qk. It is the other way around.

2). Actualy, I see nowhere where your file names and directory names are simular.

Truthfully, I do beleive this complicates things abit and totally changes the logic of the program.

Unlike the logic I posted which looks for directories that have a simularity in the actual name. You need to find simply what directory the file resides in, and then delete that directory. Is that correct?

Dan

Collapse -

reply

by kleeusmale In reply to Ok, I see why this isnt w ...

yes, need to find what directory the file resides & delete that directory.

Back to Software Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums