Computing.Net > Forums > Unix > Deleting a File with No Extension

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Deleting a File with No Extension

Reply to Message Icon

Name: LiteBright
Date: December 13, 2006 at 09:06:02 Pacific
OS: Unix
CPU/Ram: P4 4G Ram
Product: Intel
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 13, 2006 at 11:27:24 Pacific
Reply:

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


0

Response Number 2
Name: rais
Date: December 13, 2006 at 11:39:16 Pacific
Reply:

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


0

Response Number 3
Name: rais
Date: December 13, 2006 at 12:22:36 Pacific
Reply:

BTW, you can simply do the following as well:

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

Thanks.


0

Response Number 4
Name: LiteBright
Date: December 14, 2006 at 06:03:51 Pacific
Reply:

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


0

Response Number 5
Name: rais
Date: December 14, 2006 at 06:57:58 Pacific
Reply:

In that case you can simply replace ls -1 with -al:

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

Thanks.


0

Related Posts

See More



Response Number 6
Name: LiteBright
Date: December 14, 2006 at 07:26:51 Pacific
Reply:

Ok, thanks a lot


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Deleting a File with No Extension

Deleting a file in unix www.computing.net/answers/unix/deleting-a-file-in-unix-/7581.html

find file with no extension www.computing.net/answers/unix/find-file-with-no-extension/2810.html

How to delete a file named -x www.computing.net/answers/unix/how-to-delete-a-file-named-x/3350.html