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
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.
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.
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
Response Number 6
Name: LiteBright Date: December 14, 2006 at 07:26:51 Pacific
Summary: How can I delete a file with the name -C in unix? I tried below and get the error. > rm "-C" rm: illegal option -- C Usage: rm [-Rfir] file ... ...
Summary: Taken directly from the man pages of rm > man rm Users often wonder how to deal with filenames that begin with ``-'', since arguments that begin with ``-'' are usually treated as flags. The special a...