LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Help needed for find command (https://www.linuxquestions.org/questions/linux-general-1/help-needed-for-find-command-671734/)

ZAMO 09-23-2008 02:36 AM

Help needed for find command
 
Hi all,


Am using the following find command to list the files and got my desired output.

Code:

find /zamo/direct/test/ -name myfile*  -mtime -25 |xargs ls -lht|grep -v total
The output must be as following, since I need to do awk,sed &cut from this

Code:

-rw-rw-r--  1 zamo zamo  21M Jul 14 03:02  myfile_2008.07.14

It was working fine. I have my question here . If my search did not find a output for the 25 days using
Code:

find /zamo/direct/test/ -name myfile*  -mtime -25
It is returning no output. But the
Code:

find /zamo/direct/test/ -name myfile*  -mtime -25 |xargs ls -lht
is making an undesired output of listing all the files in the current directory from which the script is executed.

Is there any command or choice to avoid this kind of errors. I need the output if the "find" results an output of file with 25 days old. But "xargs ls -lht " is also must , as i need to do long listing.

Any alternate for this? any Suggestion.


Thanks :)

Berhanie 09-23-2008 02:49 AM

If I understand the question, to prevent myfile* from being expanded by the shell, you need to quote it:
Code:

find /zamo/direct/test/ -name "myfile*"  -mtime -25 |xargs ls -lht

ZAMO 09-23-2008 02:55 AM

Sorry Berhanie,

Its not working . Am getting the same output. I hope you understand my issue and if you have time, have a sample try on your box.

Thanks :)

Berhanie 09-23-2008 03:03 AM

I think I understand the question: ls -lht lists files in the current directory. So, here's something:
Code:

find /zamo/direct/test/ -name 'myfile*'  -mtime -25 -exec ls -lht '{}' \; | grep -v
You should look up the exact syntax for -exec, but I think that's correct.

ZAMO 09-23-2008 03:45 AM

Berhanie,

Yes the "exec" works. But the "find" has to search for files within a thousands of files , as it will send out a lot of errors even though i get the desired output.
Code:

find: ls terminated by signal 13
Am using the same "find " about 40 times within the script. IS there anyway to use "xargs " to get my desired outoput and without the errors.


thanks :)

Berhanie 09-23-2008 04:04 AM

I just glanced through the man page of xargs. It looke like there's an "xargs -r", which will come in handy if find doesn't produce output. So, it looks like
Code:

find /zamo/direct/test/ -name 'myfile*'  -mtime -25 |xargs -r ls -lht|grep -v total
is our candidate. By the way, you know about redirecting standard error, right?

jschiwal 09-23-2008 04:06 AM

There are two things you want to do. First, the find command will also return directories as well as find, so "ls" will return all files in the directory. Add "-type f". Also, if a file might contain white space, you want to use the NULL character as an argument delimiter when using xargs. Simply add -print0 to the find command and the "-0" argument to xargs. If you have thousands of files matching the criteria, then use one of the limiting option to xargs.
Code:

find /zamo/direct/test/ -type f -name "myfile*" -mtime -25 -print0 | xargs -0 ls -lht
Since all of the arguments are files, you don't have an -v line to get rid of.
If you want to include directories in the find results, then include the -d option for "ls" to prevent recursively listing the directory contents.

ZAMO 09-23-2008 04:15 AM

Thank you both for the suggestions,

Currently am moving with
Code:

find /zamo/direct/test/ -name 'myfile*'  -mtime -25 |xargs -r ls -lht|grep -v total
as it works.

Will share with you , if get something hard or useful.

Thanks for you time and help :)


All times are GMT -5. The time now is 08:03 PM.