Operating System - HP-UX
1830213 Members
1375 Online
109999 Solutions
New Discussion

Various definitions of UX commands

 
SOLVED
Go to solution
panchpan
Regular Advisor

Various definitions of UX commands

Hello.
In this thread, I shall keep asking few basic ux command's questions.

Whats the difference in:
find . -mtime +60 -exec rm -f {} \;

and

find . -mtime +60 | xargs rm 2>/dev/null

Thank you!
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Various definitions of UX commands

Shalom,

Functionally I see no difference.

They are both supposed to erase files over 60 days since last modification.

The real difference is how the output is handled.

Have you tested them? Do they work differently?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Various definitions of UX commands

The correct command is:
$ find . -mtime +60 -exec rm -f {} +

This has the advantage of xargs in doing more one file at a time (using ";") and it doesn't need xargs.

Your find -exec ... \; above invokes rm once per file.

Another differences is that your xargs command was missing -f and had an extra redirect of stderr.

You may want to fix them to add "-type f" so you only find files.
Jannik
Honored Contributor

Re: Various definitions of UX commands

The main diff is the output as mentioned but I see another.
The -exec spawns a separate process for each file or directory, where xargs groups and collects, resulting in fewer processes.
If you want to use the -exec you should use the + delimiter insted of the ; this will work identical with the xargs.

find . -mtime +60 -exec rm -f {} \+

You will find some information about this in the man pages for find.
jaton
panchpan
Regular Advisor

Re: Various definitions of UX commands

What is the use of all {}\+ in below command

find . -mtime +60 -exec rm -f {} \+
panchpan
Regular Advisor

Re: Various definitions of UX commands

My next question is about command - /usr/bin/rm $(/usr/bin/ls -t /home/root/core* | /usr/bin/tail +2)

What is it suppose to do?
panchpan
Regular Advisor

Re: Various definitions of UX commands

Here are my consolidated questions:

1)
Is it correct?

find . -follow -type f \( -name "core*" -a -mtime +0 \) -exec rm {} \;

What will follow do here?

2) Is it correct?
find /global/mfg/home/*/archive -follow -type f \( -name "*" -a -mtime +30 \) -exec rm {} \;

3) What is the differnce for ending command with {} \; and {}+ Also, what is the significance of {}?

4) /usr/bin/rm $(/usr/bin/ls -t /home/root/core* | /usr/bin/tail +2)

What is it suppose to do?
Dennis Handly
Acclaimed Contributor
Solution

Re: Various definitions of UX commands

>What is the use of all {}\+ in below command

The proper syntax is just: -exec rm -f {} +
(There is no need for "\", contrary to the broken documentation.)

The "{} +" tells find to batch up as many args as will fit.

>command: /usr/bin/rm $(/usr/bin/ls -t /home/root/core* | /usr/bin/tail +2)

This says remove the files returned by $(...). This lists all of the files core*, and skips the latest.

>1)find . -follow -type f \( -name "core*" -a -mtime +0 \) -exec rm {} \;
>What will -follow do here?

It will follow symlinks.
Note: No need for () and -a, since the default is AND.

>2) Is it correct?
find /global/mfg/home/*/archive -follow -type f \( -name "*" -a -mtime +30 \) -exec rm {} \;

Probably not. No need for -name "*".

>3) What is the difference for ending command with {} \; and {}+ Also, what is the significance of {}?

Using "{} +" will batch up files to execute the same command with as many as will fit.
The "{}" is an indication where -exec should insert the filenames it finds. For the "+" case it is required by Posix but HP-UX allows you to leave it out, unless you are doing UNIX 2003 branding, where "{}" is required.
panchpan
Regular Advisor

Re: Various definitions of UX commands

Thank you - its little clear now. Still i have doubt with -follow

find . -follow -type f -mtime +0 -exec ll {} \;

IF i understand correctly, above will only list files which are symbolic links , or will it print all files. See here:

a300sl57:/prod_wrk/pp> ll h
lrwxrwxrwx 1 mfgeb qad 1 2007-07-04 05:57 h -> g
a300sl57:/prod_wrk/pp> find . -follow -type f -mtime +0 -exec ls -l {} \;
find: ./h: No such file or directory
-rw-rw-r-- 1 mfgeb qad 0 2007-01-26 15:44 ./a.txt
-rw-rw-r-- 1 mfgeb qad 0 2007-01-26 15:45 ./b.txt
a300sl57:/prod_wrk/pp>
panchpan
Regular Advisor

Re: Various definitions of UX commands

I am not able to make out clear use of follow delimiter. Shouldnt it display only symbolic links:

a300sl57:/prod_wrk/pp> find . -follow -type f -mtime +0 -exec ls -l {} \;
-rw-rw-r-- 1 mfgeb qad 0 2007-01-26 15:44 ./g
lrwxrwxrwx 1 mfgeb qad 1 2007-07-04 05:57 ./h -> g
-rw-rw-r-- 1 mfgeb qad 0 2007-01-26 15:44 ./a.txt
-rw-rw-r-- 1 mfgeb qad 0 2007-01-26 15:45 ./b.txt
a300sl57:/prod_wrk/pp>
James R. Ferguson
Acclaimed Contributor

Re: Various definitions of UX commands

Hi:

> find . -follow -type f -mtime +0 -exec ll {} \;

> IF i understand correctly, above will only list files which are symbolic links , or will it print all files. See here:

By adding '-follow' you will list files that are referenced by symbolic links in the directory in which you are searching. If omitted, symbolic links in the directory that you are searching will NOT be resolved.

Consider:

# mkdir /tmp/dummy && cd /tmp/dummy
# touch f1 f2 f3
# touch /var/tmp/removeme

# find . -type f -exec ll {} \;
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f1
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f2
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f3

# find . -follow -type f -exec ll {} \;
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f1
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f2
-rw-r----- 1 root sys 0 Jul 4 08:57 ./f3
lrwxr-x--- 1 root sys 17 Jul 4 08:57 ./linktovar -> /var/tm
p/removeme

The '-type' switch can take various arguments : "f" for files; "d" for directories; and "l" for symbolic links.

By choosing "f" you limit yourself only to FILES but by adding '-follow' you FOLLOW any symbolic links and report FILES that they represent.

Regards!

...JRF...