(?) The Answer Gang (!)


By Jim Dennis, Ben Okopnik, Dan Wilder, Breen Mullins, Mitchell Bruntel, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to [email protected]


(?) about Unix command rm

From Jane Liu

Answered By Mike Orr, Ben Okopnik, Dan Wilder

(?) I have a question about rm command. Would you please tell me how to remove all the files excepts certain files like anything ended with .c?

(!) [Mike] The easiest way (meaning it will work on any Unix systems anywhere), is to move those files to a temporary directory, then delete "everything", then move those files back.

mkdir /tmp/tdir
mv *.c /tmp/tdir
rm *
mv /tmp/tdir/* .
rmdir /tmp/tdir
(!) [Ben] The above would work, but seems rather clunky, as well as needing a lot of typing.
(!) [Mike] Yes, it's not something you'd want to do frequently. However, if you don't know a lot about Unix commands, and are hesitant to write a shell script which deletes a lot of files, it's a good trick to remember.
(!) [Ben] It's true that it is completely portable; the only questionable part of my suggestion immediately below might be the "-1" in the "ls", but all the versions of "ls" with which I'm familiar support the "single column display" function. It would be very easy to adapt.
My preference would be to use something like
rm $(ls -1|grep -v "\.c$")
because the argument given to "grep" can be a regular expression. Given that, you can say things like "delete all files except those that end in 'htm' or 'html'", "delete all except '*.c', '*.h', and '*.asm'", as well as a broad range of other things. If you want to eliminate the error messages given by the directories (rm can't delete them without other switches), as well as making "rm" ask you for confirmation on each file, you could use a "fancier" version -
rm -i $(ls -AF1|grep -v "/$"|grep -v "\.c$")
Note that in the second argument - the only one that should be changed - the "\" in front of the ".c" is essential: it makes the "." a literal period rather than a single-character match. As an example, lets try the above with different options.
In a directory that contains

testc
test-c
testcx
test.cdx
test.c
".c" means "'c' preceded by any character" - NO files would be deleted.
"\.c" means "'c' preceded by a period" - deletes the first 3 files.
"\.c$" means "'c' preceded by a period and followed by the end of the line" - all the files except the last one would be gone.
Here's a script that would do it all in one shot, including showing a list of files to be deleted:
See attached misc/tag/rmx.bash.txt
(!) [Dan] Which works pretty well up to some limit, at which things break down and exit due to $skip being too long.
For a less interactive script which can remove inordinate numbers of files, something containing:
ls -AF1 | grep -v /$ | grep -v $1 | xargs rm
allows "xargs" to collect as many files as it can on a command line, and invoke "rm" repeatedly.
It would be prudent to try the thing out in a directory containing only expendable files with names similar to the intended victims/saved.
(!) [Ben] Possibly a good idea for some systems. I've just tried it on a directory with 1,000 files in it (created just for the purpose) and deleted 990 of them in one shot, then recreated them and deleted only 9 of them. Everything worked fine, but testing is indeed a prudent thing to do.
(!) [Dan] Or with some typists. I've more than once had to resort to backups due to a slip of the fingers (the brain?) with an "rm" expression.
(!) [Ben] <*snort*> Never happened to me. No sir. Uh-uh. <Anxious glance to make sure the weekly backup disk is where it should be>
I just put in that "to be deleted" display for, umm, practice. Yeah.
<LOL> Good point, Dan.

(?) Thanks a million! It worked.

I have another question: My shell script is in a file called hw1d.sh. When I run sh hw1d.sh, the output shows on the screen. But the command details won't show. Is there a way I can capture the detailed command lines and output at the same time?

(!) [Ben] For one thing, you shouldn't be running your script as "sh ..."; simply make it executable via "chmod +x <scriptname>" and run it. Other than that (I think I understand what you're asking here), you can add "-v" to the hashbang line so it looks like this -
#!/bin/bash -v
This will print out each line as it is read.
(!) [Mike] Or -x, which is what I use. They do slightly different things. Consider this program.

#!/bin/bash -v
TOWHOM="world"
echo "Hello"
echo $TOWHOM
# This is a comment.
Now running it:

$ ./hello.sh
#!/bin/bash -v
TOWHOM="world"
echo "Hello"
Hello
echo $TOWHOM
world
# This is a comment.
Now change -v to -x and run it.
$ ./hello.sh
+ TOWHOM=world
+ echo Hello
Hello
+ echo world
world
The variable was expanded, there's a "+ " before each program line, and the comments are omitted. It looks like -v shows the commands before they're interpreted and -x shows them after.
(!) [Ben] For more details on shell scripting, see my "Introduction to Shell Scripting" articles in LG53-57 and 59.

He got the issue numbers wrong, but no sense worrying about that, here they are. -- Heather

(?) Thanks!

For practice purpose, I create file -cfile and try to rename it to cfile. I figured out one way:

 >cat <\-cfile >cfile

But I just couldn't delete the old file -cfile because shell always interprets as option. Is there a way I can do this?

(!) [Dan] Yes.
rm -- -cfile
From "man rm":
GNU STANDARD OPTIONS [ ... ] -- Terminate option list.
(!) [Ben] Given that "there's more than one way to do it",
rm ./-cfile
also works. As you have found out, it's not a good idea to create filenames with non-alphanumeric characters at the beginning: just because you can, really does not mean that you should...


This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 62 of Linux Gazette February 2001
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


[ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7 [ Index of Past Answers ]