Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I delete files and directories by using a source file?

Status
Not open for further replies.

1971240z

Technical User
Jul 24, 2002
20
US
I need to delete files and directories by using a source file. I can not use the rm –r command to remove the files and directories. The file that I have is in reverse order. How can I use this file to delete all the files and directories?

Example input file.

/export/home/user1/test6
/export/home/user1/test5/test_file 2.doc
/export/home/ user1/test5/testfile
/export/home/ user1/test5
/export/home/ user1/test4/testfile2
/export/home/ user1/test4/testfile
/export/home/ user1/test4
/export/home/ user1/test3/test file 2.doc
/export/home/ user1/test3/testfile
/export/home/ user1/test3
/export/home/ user1/test2/test file 2
/export/home/ user1/test2/testfile
/export/home/ user1/test2
/export/home/ user1/test1/test file 2
/export/home/ user1/test1/testfile
/export/home/ user1/test1/test2/test file 2
/export/home/ user1/test1/test2/testfile
/export/home/ user1/test1/test2/test3/test file 2
/export/home/ user1/test1/test2/test3/testfile
/export/home/ user1/test1/test2/test3
/export/home/ user1/test1/test2
/export/home/ user1/test1
 
Hi:

Assume the above file and directory listing is in data.file. How about reading the file and if it's a directory, delete it?

#!/bin/ksh

# untested
while read line
do
if [[ -d $line ]]
then
rm -r $line
fi
done < data.file
 
could you elaborate on WHAT exactly needs to be done. Your statements are somewhat self-contradicting:

> I can not use the rm –r command to remove the files and directories. The file that I have is in reverse order. How can I use this file to delete all the files and directories? vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Vlad:

Given:

/export/home/user1/test1/test2/test3/testfile
/export/home/user1/test1/test2/testfile.1
/export/home/user1/test1/test2

The first two objects are files and the last one is a directory. I'm assuming all the directories are listed in the file. Perhaps that is a bad assumption?

If my assumption is good, and the user has the proper permissions, the first two objects are skipped, and performing an rm -r on /export/home/user1/test/test2 eliminates directory and associated children under test2.

Do you see something wrong with my assumption?

Regards,

Ed
 
Ed,
yeah, that's exactly how I was reading the posting, but I didn't know if you could assume that ALL the directories for listed files where present.

And then again - the OP's statement &quot;I can not use the rm –r command to remove the files and directories&quot; - didn't make sense.

Your 'read' and your solution is fine if the OP wanted to delete all the directories listed in a given file. I'm not quite sure if that's what the OP wanted though and how you're going to do it without using 'rm -r'.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
What I need to do is delete all the files in a dir then delete the dir. I have a file that is sorted in reverse order. I need to read this file into a script that will look at the file line by line and determine if the line is a file or a dir. If it is a dir it will remove it with the rmdir command else use the rm command to remove the file. I cannot use the rm -r command. The reason for this is that I do not what to remove the dir if it is not empty. The rm –r command will just delete everything no questions asked. This is very dangerus when you are deleting massive amounts of data using a file. I know what to do I just do no know how to write it in a script.

Thank You,
 
If that's the case, then just use 'rm' for files and 'rmdir' for directories. If directory is NOT empty, it [ rmdir ] will simply fail.

Modifying Ed's script:

#!/bin/ksh

# untested
while read line
do
if [[ -d $line ]]
then
# error messages to trash
rmdir $line 2> /dev/null
else
rm $line
fi
done < data.file
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thank You for all your help.

I have it working.

Thanks again
 


Hi why not simply

rm -f `cat file`
rmdir `cat file`


 
i propose:

find thedir -type f >todo

edit todo, in vi issue the cmds

:g/^/m0<enter>
vertically reverse the file to->bottom
:%s/.*/rm -f &/<enter>
prepend the rm cmd
:x<enter>
write and quit

then exec todo: sh todo

repeat changing: (find) -type d and (in vi) rm -f to rmdir

rm `cat file` will fail if file contains more names as the configured max #-args typically 1024 | 2048 ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
tdatgod,
you're right - that's more elegant.

jamisar,
man xargs
The forum is called 'UNIX Scripting' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
thanks vlad.

xargs is a beatifull tool ... i know it.
did you realize what i has to do ?

a) reverse the listing
b) first delete plain-files
c) then delete dirs
d) you sure never get the error
cmd: argument list too big
try it:

mkdir qqq
AAA=&quot;0 1 2 3 4 5 6 7 8 9&quot;
for a in $AAA
do for b in $AAA
do for c in $AAA
do for d in $AAA
do for e in $AAA
touch qqq/qqq$a$b$c$d$e
done;done;done;done;done

rm qqq/qqq* #you will get the error

sure: rm -rf qqq will work, because it works recursively!!
using: c-lib func opendir() and readdir()

mit freundlichen Gruessen ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
not that it matters any more, but... what's the point? What does it have to do with the original posting?

Mit freundlichen Grüßen vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
thanks vlad.
:q!<enter> ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top