|
Home > Archive > Unix Shell > November 2006 > Is my code wrong?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Bo Yang 2006-11-29, 1:17 pm |
| I am now being mad now!
I want to clear up my /home directory, to delete
all subdir but the 'yb' dir and then copy the
/usr/portage to here.
so I do following:
cd /home
find ./ -maxdepth 1 ! -name yb -exec rm -fr {} \; ; cp -fr /usr/portage/
./
but it delete all in /home, all my valuable data!
God, what is wrong!
| |
| Lew Pitcher 2006-11-29, 1:17 pm |
|
Bo Yang wrote:
> I am now being mad now!
> I want to clear up my /home directory, to delete
> all subdir but the 'yb' dir and then copy the
> /usr/portage to here.
>
> so I do following:
>
> cd /home
> find ./ -maxdepth 1 ! -name yb -exec rm -fr {} \; ; cp -fr /usr/portage/
> ./
>
> but it delete all in /home, all my valuable data!
> God, what is wrong!
The command
find ./ -maxdepth 1 ! -name yb
will match everything in the cwd that isn't named yb.
../. is in the cwd, and isn't named yb
../. is the cwd
Thus, your command matched the cwd, and since you instructed it to
delete (-fr) all matches, you deleted the cwd
:-(
Next time, test your command using echo, as in
find ./ -maxdepth 1 ! -name yb -exec echo rm -fr {} \;
and see what it will do
HTH
--
Lew
| |
| johngnub 2006-11-29, 1:17 pm |
| One more simple common pet trick, do your find with -exec ls -l {} \;,
To see what will be done. I often limit my finds with a -type f,
example `find /tmp -type f -name "fun" -exec ls {} \;
Then change the ls to rm , useing the -rf is the thin ice, use with
care. the rm -i, interactive is handy, and boring. 2 cents, JB
Lew Pitcher wrote:
> Bo Yang wrote:
>
> The command
> find ./ -maxdepth 1 ! -name yb
> will match everything in the cwd that isn't named yb.
>
> ./. is in the cwd, and isn't named yb
>
> ./. is the cwd
>
> Thus, your command matched the cwd, and since you instructed it to
> delete (-fr) all matches, you deleted the cwd
>
> :-(
>
> Next time, test your command using echo, as in
> find ./ -maxdepth 1 ! -name yb -exec echo rm -fr {} \;
> and see what it will do
>
> HTH
> --
> Lew
| |
| Stephane CHAZELAS 2006-11-29, 1:17 pm |
| 2006-11-29, 22:37(+08), Bo Yang:
> I am now being mad now!
> I want to clear up my /home directory, to delete
> all subdir but the 'yb' dir and then copy the
> /usr/portage to here.
>
> so I do following:
>
> cd /home
> find ./ -maxdepth 1 ! -name yb -exec rm -fr {} \; ; cp -fr /usr/portage/
> ./
>
> but it delete all in /home, all my valuable data!
> God, what is wrong!
Had you used the portable way instead of -maxdepth it wouldn't
have happened:
find . ! -name . -prune ! -name yb -exec rm -rf {} +
Note that cp -r often doesn't do what you want. I would use pax, tar
or cpio instead.
pax -rw /usr/portage .
(cd /usr && tar cf - portage) | tar xpf -
--
Stéphane
| |
| Bruce Barnett 2006-11-29, 7:22 pm |
| Bo Yang <struggle@mail.nankai.edu.cn> writes:
> but it delete all in /home, all my valuable data!
> God, what is wrong!
It's too late, but you need to learn how to break a problem down into steps:
1) Backup everything.
2) Get list of files you want to delete.
3) Make sure the list contains only the files you want to delete.
4) Delete the files.
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
| |
| Kaz Kylheku 2006-11-30, 1:33 am |
| Bo Yang wrote:
> I am now being mad now!
> I want to clear up my /home directory, to delete
> all subdir but the 'yb' dir and then copy the
> /usr/portage to here.
>
> so I do following:
>
> cd /home
> find ./ -maxdepth 1 ! -name yb -exec rm -fr {} \; ; cp -fr /usr/portage/
> ./
Dumbass, next time run a harmless command first to verify. Would it
have killed you to run this instead?
find .-maxdepth 1 ! -name yb -exec echo rm -rf {} \;
A little echo would show you what rm commands are going to be run.
Here is another one idea: how about doing it interactively? List your
directory to a file:
ls > delete-list
Now open up the delete list in vi and remove all of the items from it
that you want to keep. Join the remaining lines into one with:
:%j
Now insert "rm -rf" at the beginning of that line:
Irm -rf [ESC]
Then execute the command by piping it to your shell with !!sh {enter}
But maybe for you, I'd recommand a GUI file manager with a
drag-and-drop trash can.
> God, what is wrong!
Let's see: not doing backups, and not testing code before deploying it
live.
| |
| Bo Yang 2006-11-30, 1:33 am |
| Lew Pitcher :
> Bo Yang wrote:
>
> The command
> find ./ -maxdepth 1 ! -name yb
> will match everything in the cwd that isn't named yb.
>
> ../. is in the cwd, and isn't named yb
>
> ../. is the cwd
My god, I forgot ./ .
Is there any way to let find not consider the ./ and ../ dirs?
>
> Thus, your command matched the cwd, and since you instructed it to
> delete (-fr) all matches, you deleted the cwd
>
> :-(
>
> Next time, test your command using echo, as in
> find ./ -maxdepth 1 ! -name yb -exec echo rm -fr {} \;
> and see what it will do
>
> HTH
| |
| Bo Yang 2006-11-30, 1:33 am |
| Stephane CHAZELAS :
> 2006-11-29, 22:37(+08), Bo Yang:
>
> Had you used the portable way instead of -maxdepth it wouldn't
> have happened:
>
> find . ! -name . -prune ! -name yb -exec rm -rf {} +
>
> Note that cp -r often doesn't do what you want. I would use pax, tar
> or cpio instead.
>
> pax -rw /usr/portage .
>
> (cd /usr && tar cf - portage) | tar xpf -
>
Thank you !
| |
| Bo Yang 2006-11-30, 1:33 am |
| Bruce Barnett :
> Bo Yang <struggle@mail.nankai.edu.cn> writes:
>
>
> It's too late, but you need to learn how to break a problem down into steps:
>
> 1) Backup everything.
> 2) Get list of files you want to delete.
> 3) Make sure the list contains only the files you want to delete.
> 4) Delete the files.
>
I will remeber this for ever!
| |
| Bo Yang 2006-11-30, 1:33 am |
| Kaz Kylheku 写é“:
> Bo Yang wrote:
>
> Dumbass, next time run a harmless command first to verify. Would it
> have killed you to run this instead?
>
> find .-maxdepth 1 ! -name yb -exec echo rm -rf {} \;
>
> A little echo would show you what rm commands are going to be run.
>
> Here is another one idea: how about doing it interactively? List your
> directory to a file:
When I type this command, I have something else to do.
So, I type it in the shell and then go away.
When I came back before my computer, it is too late!
I will remeber this lesson for ever!
>
> ls > delete-list
>
> Now open up the delete list in vi and remove all of the items from it
> that you want to keep. Join the remaining lines into one with:
>
> :%j
>
> Now insert "rm -rf" at the beginning of that line:
>
> Irm -rf [ESC]
>
> Then execute the command by piping it to your shell with !!sh {enter}
>
> But maybe for you, I'd recommand a GUI file manager with a
> drag-and-drop trash can.
>
>
> Let's see: not doing backups, and not testing code before deploying it
> live.
>
| |
| Kaz Kylheku 2006-11-30, 1:33 am |
|
Bo Yang wrote:
> Lew Pitcher :
>
> My god, I forgot ./ .
> Is there any way to let find not consider the ./ and ../ dirs?
find does not consider the .. directory. In general, properly written
programs which recursively search trees cannot be confused on this
matter! They must recognize the directory entries named "." and ".."
and skip them. A search program which doesn't understand .. will search
the whole filesystem, assuming it's smart enough to properly implement
a spanning tree algorithm and thereby avoid looping endlessly around
the cyclic links.
And, second question, how do you get find not to consider the ./
directory? Well, you passed it as an explicit argument, so why should
it ignore that directory? The meaning of the find program is to find
every one of the argument paths, and for each of the arguments paths
which is a directory, recursively find the descendants.
The trailing slash, by the way, means nothing. The notation ./ means
the same thing as .
If I compare the output from "find ." and "find ./" on my
implementation of find, the only difference between the two outputs is
that it prints a trailing slash on the .:
$ diff <(find ./) <(find .)
1c1
< ./
---
> .
Find /will/ even search the .. directory, if you explicitly specify it
as the root of a search.
find .. -print
Yes this works, but it will only search the parent directory and its
children; it won't go to the ../.. grandparent directory.
If you don't understand the search procedure that a program uses, why
would you use that tool to blindly delete everything that it finds?
You even specified the -f flag to rm, to force it to delete
write-protected files without asking.
| |
| Kaz Kylheku 2006-11-30, 1:33 am |
| Bo Yang wrote:
> When I type this command, I have something else to do.
> So, I type it in the shell and then go away.
> When I came back before my computer, it is too late!
Not necessarily. Instead of going on Usenet, you should have pulled the
power on the computer. Then use another computer to search for an
deleted file recovery tool for your filesystem. I may be possible to
bring back files; the data is not actually overwritten on the disk's
surface. But it likely will be as new content is created on the
filesystem.
You can move the hard drive to a different computer, and then run the
tool on it, if such a tool exists.
Something tells me that you must be a Linux user, so google "ext2fs
undelete".
/First/ try to get your data back, /then/ learn about find, etc.
If it's a large number of files, you're looking at spending a lot of
time. But chances are that most of it is garbage, containing only a few
"nuggets".
|
|
|
|
|