find . -type d |grep fuser (does not work, why)
if I run the command "find . -type d" it does what its supposed to, it find directories. If I try to pipe the output into fuser I get a response as if I just typed "fuser" by it self. Why?
I user fuser to find out what process is occupying a directory. This is useful if you are trying to unmount a dir. and get a busy error. The problem is fuser is not recursive. |
You have to use xargs to execute commands on the arguments passed as standard input:
Code:
find . -type d | xargs fuser |
Maybe fuser can't read a list from standard input (as appears to be the case here); instead you'll need to supply the list of arguments to fuser. This can achieved with find as follows:
Code:
find ./ -type d -exec fuser {} + |
that worked, thanks.
|
Actually this doesn't catch everything. Remove "type -d" and you catch more. But still you could be unable to umount due to FS being busy. In my case the filesystem is used as aufs overlay fs and it doesn't show up in fuser. I don't have lsof on that distro so can't tell if lsof is better.
|
Just in case it's not perfectly clear, in the command above you're piping the stdout of find into the stdin of grep. So you have grep searching for the literal string "fuser" in the text output of find; you're searching the file names, not the contents of the files themselves.
If you tried piping it directly into fuser you'll simply get an error, because as pwc101 points out, fuser doesn't read from stdin. What you really want to do is execute the fuser command once for each name found. As demonstrated, find's -exec option or xargs are common ways to do it. It's also possible to use a bash loop like this: Code:
while IFS="" read -r -d "" dir; do Or if you're using bash v4+, you can try this: Code:
shopt -s globstar find may sometimes still be more efficient however. |
Quote:
Please mark this thread as solved. This is VERY useful for others. |
All times are GMT -5. The time now is 06:46 PM. |