Deleting a File with No Extension

December 13, 2006 at 09:06:02
Specs: Unix, P4 4G Ram
I need to create a script that will remove a file if there is no extension. The file is generated with a random name, based on current date. The filename is passed in from a C program.

I have tried this to search for a file extension, but will return the filename if there is no extension. However if there is an extension, the extension is returned and I get errors.

ext=$(echo $to_filename | awk -F. '{ print $NF }')
rm $ext


See More: Deleting a File with No Extension

Report •


#1
December 13, 2006 at 11:27:24
There's any number of ways of doing this; I'm assuming that the filename contains only alpha and numeric characters inaddition to special chars + and -. I delete any file that contains a .

#!/bin/sh

ls -1|while read file
do
if expr "$file" : '[a-zA-Z0-9\-\+]*\.' > /dev/null
then
rm $file
fi
done


Report •

#2
December 13, 2006 at 11:39:16
You can use:
ls -1 | nawk -F. '{ if (length($2) == 0) {print $1;}}'

To find files that have no extensions. Put them in a file and loop them to get rid of them.

Hope it helps.
Thanks.
The Network Department


Report •

#3
December 13, 2006 at 12:22:36
BTW, you can simply do the following as well:

rm `ls -1 | awk '!/\./'`

Thanks.


Report •

Related Solutions

#4
December 14, 2006 at 06:03:51
Thanks, fellas. Just a couple of problems with these though. The first solution lists files with *.extension. The last two list directories along with files that do not have extensions

Report •

#5
December 14, 2006 at 06:57:58
In that case you can simply replace ls -1 with -al:

rm `ls -al | grep -v "^d" | awk '!/\./'`

Thanks.


Report •

#6
December 14, 2006 at 07:26:51
Ok, thanks a lot

Report •


Ask Question