1819876 Members
2550 Online
109607 Solutions
New Discussion юеВ

xdev

 
Waugh
Frequent Advisor

xdev

Hi,

How xdev and xargs helpful in scripting. how its working. What is the use of it. Pls explain

Reagrds
Rkumar
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: xdev

Hi:

I assume that by 'xdev' you mean the '-xdev' option of 'find'. This option prevents 'find' from crossing mountpoints. This is extremely useful when you truly want to only search the root filesystem, as in:

# find / -xdev -type f -mtime -30

...which would find files modifed during the last 30-days.

In the case of 'xargs' is seen in pipelines to construct lists of things on which execution is performed. Since 'xargs' can collect multiple arguments into a list of arguments it can be used to greatly reduce the number of processes that you need to spawn. A classic example of this is again with 'find'. Consider:

# find / -xdev -type f -mtime -30 -exec ls -l {} \;

...This spawns an 'ls' command for *every* file found (and therefore becomes costly).

# find / -xdev -type f -mtime -30 | xargs ls -l {}

...spawns only one or very few processes depending on the argument list size making this very efficient.

Now, in truth, 'find' has a newer syntax with its '-exec' that accomplishes the same thing as an 'xargs' pipe:

# find / -xdev -type f -mtime -30 -exec ls -l {} +

The "+' instead of the escaped ";" triggers 'find's inbuilt buffering.

Regards!

...JRF...
Ganesan R
Honored Contributor

Re: xdev

Hi,

xdev:
xdev is to restrict search not to cross mount points. Default search will do it on mount points as well.

For example if you want to search only on / and not to seach /var, /etc, etc then you need to specify xdev

#find / -xdev -name *.txt -print

xargs:
when you use -exec to pipe the output of find command to perform something else,
-exec will start a new process for each line of the output. Sometime -exec may not be able to handle this and command seems to be hung. xargs handle this perfectly. It will start only limited process

#find / -user abc |xargs rm -rf {}
Best wishes,

Ganesh.