LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-02-2009, 04:27 PM   #1
thllgo
Member
 
Registered: Sep 2003
Location: Laurel MD
Posts: 296

Rep: Reputation: 32
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.
 
Old 06-02-2009, 04:31 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to use xargs to execute commands on the arguments passed as standard input:
Code:
find . -type d | xargs fuser
 
1 members found this post helpful.
Old 06-02-2009, 04:34 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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 {} +
 
1 members found this post helpful.
Old 06-02-2009, 04:39 PM   #4
thllgo
Member
 
Registered: Sep 2003
Location: Laurel MD
Posts: 296

Original Poster
Rep: Reputation: 32
that worked, thanks.
 
Old 12-26-2011, 03:00 PM   #5
avalonit
Member
 
Registered: Jun 2008
Posts: 81

Rep: Reputation: 19
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.

Last edited by avalonit; 12-26-2011 at 03:25 PM.
 
Old 12-27-2011, 08:59 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
	fuser "$dir"
done <( find . -type d -print0 )
http://mywiki.wooledge.org/BashFAQ/001

Or if you're using bash v4+, you can try this:

Code:
shopt -s globstar
for dir in . **/ ; do	# Use . to include the top level too.
	fuser "$dir"
done
** is the new globstar globbing pattern, which matches recursively. You have to enable it first. Adding a / after it makes it match only directories. You can tack on additional globs onto it to search for files inside a directory tree (e.g. **/*.txt to grab all text files). You can also enable shopt -s dotglob to match hidden files.

find may sometimes still be more efficient however.
 
Old 12-28-2011, 06:03 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by thllgo View Post
that worked, thanks.
Hi,

Please mark this thread as solved. This is VERY useful for others.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find grep foampile Linux - Newbie 5 04-24-2009 06:14 PM
Grep V/s Find justmovedtolinux Programming 3 10-02-2008 03:29 AM
using find /grep gamor Linux - Newbie 14 04-13-2005 02:36 PM
lsof and fuser do not work with /dev/dsp alexrait1 Slackware 4 10-09-2004 04:07 PM
help me find grep-2.5.1 shanenin Linux - Software 1 01-23-2004 09:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration