ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The grep -v part excludes all entries that contain /home/, all that is left is given to ls -l. The xargs command is needed to "feed" the ls -l command.
I used ls -l instead of rm -rf for safety (find is a powerful command). Replace ls -l with rm -rf if the output generated is what you want.
Hope this helps.
Last edited by druuna; 08-04-2010 at 04:58 AM.
Reason: Excluding possible false hits (added ^ to grep command)
druuna, when using a grep approach (the OP didn't specify whether he wanted to exclude files in subdirectories of /home, too, but let's assume he wants that), you should at least use an anchor to only match paths starting whti /home like this:
egrep -v '^/home/'
I could not understand this completely.
Can you be a bit elaborate.
Yes, of course. The action -prune just tells find to not descend into directories. Let's look at every single step:
1. Look for a directory whose whole name is /home
Code:
$ find / -wholename /home
/home
The name is relative to the specified search path, in this case / (root). This tell us that there is (obviously) only one item whose name is /home.
2. Add -prune
Code:
$ find / -wholename /home -prune
/home
Nothing different here, because the action -prune alone is evaluated as true and the result is to print the directory name. Actually the rule here is: if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.
Note that the expression here contains at least one action other than -prune (-print), so that the rule cited above does not apply. For this reason, when find encounters /home the left-hand-side of the expression is true and the output is null now. Instead for all the other directories, the left-hand-side of the expression is false and the right-hand-side is evaluated (in this case every file whose name is lighttpd is printed out).
Note that if you omit -print (without adding any other action) the rule above is applied and -prune still has the effect to not descend into the directory, but the directory name is printed out:
In my previous example the action -exec prevents this behaviour.
Finally you can omit multiple directories from the search by expanding the left-hand-side of the expression with other alternatives (embedded in escaped parentheses). For example:
find / lighttpd /home -prune -o -name lighttpd
/
find: `lighttpd': No such file or directory
/home
Just seen your addition. Based on the explanation in my previous post, this command tells to search into /, into /home and an item called lighttpd in the current working directory:
Code:
$ find / lighttpd /home
<omitted results for obvious reasons>
find: `lighttpd': No such file or directory
<still omitted results>
The error just tells you that a file or a directory named lighttpd does not exist in the current working dir. Also note that the files in /home will be printed twice, beacuse find first searches recursively into /, then again into /home. Specifying a multiple search path might have unpleasant side effects!
Add -prune
Code:
$ find / lighttpd /home -prune
/
find: `lighttpd': No such file or directory
/home
No other output here, since the / (root) directory is not descended. Just the two directory names listed in the (multiple) search path are printed out, since the line does not contain any action other than -prune!
As usual colucix has provided the good oil along with a great explanation. I thought I would just add an extra choice as I read the original query
to mean not from my home directory, but this says nothing about what if in others, so the simple addition of $HOME works nicely
I am having certain directories also with the same name how do I put it here on command line.
Also I forgot to ask the meaning of {} at the end of the command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.