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 to extract file names from a fiel and remove them

Status
Not open for further replies.

hmehta

IS-IT--Management
Jun 5, 2002
27
US
How to remnove files in a file with awk comand. For exampe if a file has the folowing file names at eights field.

file1
file2
file3
file4

I want to extract all these file names and remove them from the directory.
 
#!/bin/sh
filenames=`awk ' { print $8 }' filename`
for all in $filenames
do
if [ -f "$all" ] ; then
echo "Removing $all."
rm $PWD/$all || echo "Could not remove filename $all."
fi
done
 
If you want to delete files with spaces or names like "-t" then this simple method works well too. You might want to try {system( "rm -i -- \"" $0 "\"" )} if you worry about deleting the wrong files. These kinds of scripts are scary.

awk '{system( "rm -- \"" $0 "\" 2> /dev/null" )}' file

Cheers,
ND [smile]
 

xargs rm < filename

where filename is the name of file containing the files to be deleted..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top