Why "ls *.c" is at least dumb, and possibly dangerous
groups.google.com
groups.google.com
1) Is he implying that launching a perl interpreter is somehow a more lightweight operation than launching ls and having it stat() half a dozen files? Having ls check if a file exists 99% of the time won't even touch the disk. If you're working in that directory, those inodes will probably be cached. Running stat() on a cached inode is hardly more expensive than a system call. Read: cheap. Now perl launching will probably load a few libs and check a few files before even getting to his one-liner.
2) Am I the only one, who in my ~10 years of running unix-like operating systems have never created a filename with a newline char in it? Like even when opening obscure filesystems, never.
3) Does he think unlink() skips the check for the file's existence?
4) If he wanted to overcomplicate things and make sure everything's kosher, why not:
find . -maxdepth 1 -type f -name '*.c' -delete
Whenever I see anything piped to a perl one liner, 4 times out of 5 it's some sort of hack that can be done with standard utils.It also grinds my gears when people take longer to think of making something more efficient than the amount of CPU time they'll save in their lives of using that 'more efficient' solution. So he spent a minute coming up with this and ten minutes posting about it. Now, will he save roughly 11 minutes of execution time in his life while running this "superior" command (also count that it takes longer to type)? If not, he wasted his time.
From his sig: "Smalltalk/Perl/Unix consulting". When you have a large-enough hammer....
2) If cleaning up after malicious users, sure why not. Doing this in your source tree or during normal usage? I don't know.
3) He mentioned ls stat()ing files as an inefficiency since the shell does it and ls does as well.
4)
FIND(1) BSD General Commands Manual FIND(1)
NAME
find -- walk a file hierarchy
... -delete
Delete found files and/or directories. Always returns true.
This executes from the current working directory as find recursesRead the context of the thread. It is a reply to this:
http://groups.google.com/group/comp.unix.shell/msg/30e16d21d...
Randal is pointing out that piping the output of ls to the perl interpreter is dangerous and shows the safer way to do what the original poster wanted.
"Well, there was a very embarassing error for Apple a few years ago for the 10.3 to 10.4 upgrade as I recall. Apparently, if you had a space in your home disk name, all sorts of files got deleted.
So, the moral is, no matter how rare you think something is, that's NO EXCUSE for not doing the right thing for all possible characters."
http://groups.google.com/group/comp.unix.shell/msg/504a75cc7... http://groups.google.com/group/comp.unix.shell/msg/0385fc2d1...
You guys are insane.
You learn something new (and vaguley horrifying) every day.
$> touch -- --
$> rm --
usage: rm [-f | -i] [-dPRrvW] file ... unlink file
$> rm -- --
$> touch \
$> touch \\ \ \ . \-
favorites of malware authors everywhere.
For example, to delete all .o files in the current directory and its subdirectories:
find . -name "*.o" -print0 | xargs -0 rm -f find . -name "*.o" -delete
?Or, you can use zsh.
rm **/*.o
grep foo **/*.{java,sql,xml}
# Where is that blasted IBM utility?
ls **/*pmt*(x.) # list e(x)ecutable files (.) that have "pmt" in their name.
# Show me all the "Setup" dirs in my project
ls -d **/Setup(/)Actually the Mac uses (or at least "used") a hidden files named "Icon\r" (yes, an embedded carriage return) for its custom folder icons. I'm still not sure that counts as "legitimate".
desktop=$(test -z $(ls -A1 /Volumes/Documents/Desktop | \
sed -e '/^.DS_Store$/d' -e '/^Icon^M$/d'))
The ^M was a literal CR, which would get munged by all my usual editors except vim. I'm sure there's some sturdier way to escape it...I remember choosing to use ls intentionally, but not what sucked about the alternatives.
I periodically have to cleanup abandoned and overflowing unix mailboxes where every message is it's own file. You'd think a simple "rm *" would work, but it doesn't, because you get an error message saying that there are "too many arguments". From my perspective there's one argument, the asterisk. Very confusing. I end up having to use xargs to get it work. Seems like a poor design.
To be honest, I wouldn't care about my shell not handling newlines in files, I'd care about my users creating files with newlines in them. Space is natural language, newline is not.
Waisting brain cycles to save CPU cycles (in the wrong places) is shameful, insulting and boring.
It would be amazing if there weren't tons of other, much more significant, things that deserved attention in the relevant script, system, or the developer's life. (sign)
With regard to why this matters, it's not uncommon for badly written scripts to interpolate filenames directly into commands. Consider this Perl fragment:
$sessionFiles = `ls /tmp/session_*`;
system("rm -f $sessionFiles");
and what it would do with a file called: "/tmp/session_blah; chmod +s"It matters. Leaving a bug around is disgusting and wasteful especially when you can fix it.
* would be amazing if there weren't tons of other, much more significant, things that deserved attention in the relevant script, system, or the developer's life. (sign)*
Fixing bugs and writing programs that have as few bugs as possible is very important.
If your goal is to make your system as robust as engineerically possible, you should most likely pay more attention in some directions and less in other. It's not only about quantity - you better have no critical bugs then have only-a-very-few critical ones.
I have not written nor seen any bash script that is neat, clean and fairly clear. It all seems like a very shady hack that someone think is smart.
ipython / iruby can and do make much nicer shells out of the box.
Every time. Until you're an expert.