[Courses][Linux comands] Finding things with locate, find, and
grep
lc-Kurse at LuftHans.com
lc-Kurse at LuftHans.com
Tue Feb 24 03:07:48 EST 2004
Am 23. Feb, 2004 schwätzte Peter Clay so:
> (warning: this is pedantic and not nearly as readable as Carla's original
> post)
Mine's pretty pedantic as well, but I really want to know if I'm overlooking
something :).
> Now is probably a good time to mention that find can be both useful and
> dangerous in conjunction with xargs:
>
> pete at platinum:~/t$ touch wanted
> pete at platinum:~/t$ touch junk.png
> pete at platinum:~/t$ touch 'wanted junk.png'
> pete at platinum:~/t$ ls -l
> -rw-rw-r-- 1 pete pete 0 Feb 23 09:59 junk
> -rw-rw-r-- 1 pete pete 0 Feb 23 09:59 wanted
> -rw-rw-r-- 1 pete pete 0 Feb 23 09:59 wanted junk.png
> pete at platinum:~/t$ find . -name '*.png' | xargs rm
> rm: cannot remove `junk.png': No such file or directory
> pete at platinum:~/t$ # huh?
> pete at platinum:~/t$ ls -l
> -rw-rw-r-- 1 pete pete 0 Feb 23 09:59 wanted junk.png
lufthans at fs:/tmp/fred$ touch wanted
lufthans at fs:/tmp/fred$ touch junk.png
lufthans at fs:/tmp/fred$ touch 'wanted junk.png'
lufthans at fs:/tmp/fred$ ls -l
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:43 junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:43 wanted
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:43 wanted junk.png
lufthans at fs:/tmp/fred$ find . -name \*.png -exec rm {} \;
lufthans at fs:/tmp/fred$ ls -l
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:43 wanted
"\*.png" and "'*.png'" are functionally equivalent.
> In trying to locate and delete all my .png files, I've deleted something
> accidentally (wanted) and not deleted something I wanted to delete ("wanted
> junk.png"). UNIX allows spaces in filenames but doesn't always handle them
> sensibly. GNU find allows you to dodge this landmine:
>
> pete at platinum:~/t$ find . -name '*.png' -print0 | xargs -0 rm
>
> That command uses null characters as seperators rather than spaces.
> For extra bonus security holes, you can put newlines in filenames and
> cause mayhem for other people's find commands. For example, it used to be
> common to have a cron job to remove 'core' files nightly, something of the
> form:
>
> find / -name core | xargs rm
>
> .. running from root's crontab. It is possible to construct some filenames
> which will cause that command to remove e.g. /etc/passwd, preventing
> anyone from logging into the system. I'm going to leave it as an exercise
> for the reader in how to do that.
lufthans at fs:/tmp/fred$ touch wanted
lufthans at fs:/tmp/fred$ touch junk.png
lufthans at fs:/tmp/fred$ touch 'wanted junk.png'
lufthans at fs:/tmp/fred$ touch 'wanted
> junk.png'
lufthans at fs:/tmp/fred$ ls -lb
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:46 junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:46 wanted
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:46 wanted\ junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:46 wanted\njunk.png
lufthans at fs:/tmp/fred$ find . -name \*.png -exec rm {} \;
lufthans at fs:/tmp/fred$ ls -lb
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:46 wanted
lufthans at fs:/tmp/fred$ touch wanted
lufthans at fs:/tmp/fred$ touch junk.png
lufthans at fs:/tmp/fred$ touch 'wanted junk.png'
lufthans at fs:/tmp/fred$ touch 'wanted
junk.png'
lufthans at fs:/tmp/fred$ ls -lb
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:48 junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:48 wanted
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:48 wanted\ junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:49 wanted\njunk.png
lufthans at fs:/tmp/fred$ find . -name w\*junk.png -exec rm {} \;
lufthans at fs:/tmp/fred$ ls -lb
insgesamt 0
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:48 junk.png
-rw-r--r-- 1 lufthans lufthans 0 2004-02-24 01:48 wanted
Am I overlooking anything?
ciao,
der.hans
--
# https://www.LuftHans.com/ http://www.AZOTO.org/
# A Polish friend of mine got an offer for a free account from AOL. The
# login ID was "HELLO" and the passwd "CYMBAL". She says "cymbal" is like
# a Polish word for "sucker". 'Hello sucker', a greeting from AOHell :).
More information about the Courses
mailing list