Using -exec with and without -name


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Using -exec with and without -name
# 1  
Old 01-18-2012
Error Using -exec with and without -name

Hi,

I need to delete the last N days file using find.
I am trying to use

find . -mtime -10 -print

which lists down required files.
but when i use

find . -mtime -10 -exec ls -lrt {} \;

it gives me all files in the directory including the required files but the required files are having ./ preceded with them.

Now when I tried
find . -name '*.txt' -mtime -10 -exec ls -lrt {} \;

It gave me proper output i.e. only required .txt files.
But i want to check for all the files in that directory and not just .txt files.
Can someone please help me out and let me know why -exec is responding differently and how can i achieve what i need to get?

Thanks
Vikas
# 2  
Old 01-18-2012
Of course it prepends ./

That's the folder you told it to look inside, .

These are completely valid paths.

If you wanted absolute paths, give find /path/to/folder/ instead of .
# 3  
Old 01-18-2012
Thanks but I am not looking for absolute path. I want to know why
find . -name '*.txt' -mtime -10 -exec ls -lrt {} \; and
find . -mtime -10 -exec ls -lrt {} \; are giving different results? Is specifying -name is mandatory or is there anyway i can get similar results from find . -mtime -10 -exec ls -lrt {} \;
# 4  
Old 01-18-2012
Quote:
Originally Posted by v_m1986
Thanks but I am not looking for absolute path. I want to know why
find . -name '*.txt' -mtime -10 -exec ls -lrt {} \; and
find . -mtime -10 -exec ls -lrt {} \; are giving different results?
The first will only list files. The second lists directories, as well, making the results very different, because 'ls folder' will list a folder's contents, not its name. This means your second version not only has different files listed different ways -- it also prints some duplicates!

Do you even need ls -lrt? Why not leave off the -exec entirely let find print by itself? It prints files by default. That way you don't have to worry about what ls is doing to the files.

Last edited by Corona688; 01-18-2012 at 12:15 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-18-2012
If you are only interested in files, not directories (or other types of inode) you need to specify "-type f".

Code:
find . -type f -mtime -10 -print

To stop "ls" expanding any directories it finds, use the "-d" switch to "ls".
Code:
find . -mtime -10 -exec ls -lad {} \;

Code:
find . -type f -mtime -10 -exec ls -lad {} \;

This User Gave Thanks to methyl For This Post:
# 6  
Old 01-19-2012
Thanks Corona688 for useful information which explains why i am getting different outputs.

Thanks Methyl, including type -f resolved my problem and now I could see desired results.
# 7  
Old 01-20-2012
Quote:
Originally Posted by Corona688
The first will only list files. The second lists directories, as well, making the results very different, because 'ls folder' will list a folder's contents, not its name. This means your second version not only has different files listed different ways -- it also prints some duplicates!

Do you even need ls -lrt? Why not leave off the -exec entirely let find print by itself? It prints files by default. That way you don't have to worry about what ls is doing to the files.
The first would also list any directory that happened to have been created with .txt on the end.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

2. UNIX for Dummies Questions & Answers

exec

Hi, i don't understand this part of one script exec >> $Log_File 2>&1 (1 Reply)
Discussion started by: messi777
1 Replies

3. Ubuntu

Find and exec

Hello, I am a linux newbe. I want to install a program. I can download it only with wget command from internet. As far as i know this wget command does not transfer the exacutable flags. Because of that i wanted to find all configure files and change their mod to 744. I found this... (1 Reply)
Discussion started by: disconnectus
1 Replies

4. UNIX for Advanced & Expert Users

-exec cp

Hi, on AIX 6.L I want to copy the result of grep -v to test directory then : `hostname`@oracle$ls -l | grep -v RINT -exec cp {} test grep: can't open -exec grep: can't open cp grep: can't open {} test:°`. Can you help me ? Thank you. (3 Replies)
Discussion started by: big123456
3 Replies

5. Shell Programming and Scripting

exec in tcl

Hello; I have a console application who shown a output in every iteration, now i create a small GUI to call the application with button etc..., but what must i do to follow showing the output in the screen? Advanced thanks, Pablo (0 Replies)
Discussion started by: pablodecastillo
0 Replies

6. Shell Programming and Scripting

exec tclsh

As a part of learning shell scripting I was just going through some already created scripts there I found- exec tclsh "$0" $(1+"$@") Here I was able to find what exec ,tclsh does & o/p of same but I could not find usage & output of $(1+"$@"). Can anybody pls expalain me usage details of it? (5 Replies)
Discussion started by: rediffmail
5 Replies

7. Shell Programming and Scripting

Help with use of `` vs exec

Hi all, I had an issue regarding use of `` or exec in perl . `` are considered to be unsafe. Why? In my case an user would be giving some parameters as input and I will form an command of it and execute it using ``. It is important to capture output as i have to parse the output. As well as I need... (0 Replies)
Discussion started by: bharadiaam
0 Replies

8. UNIX for Advanced & Expert Users

exec

I have read that exec "replaces the current process with a new one". So I did $ exec ls and after this executed, my shell disappeared. I am assuming that my shell had PID xyz, and when I did exec ls, this ls got pid xyz, and when it terminated, there was no more shell process running, and... (5 Replies)
Discussion started by: JamesByars
5 Replies

9. Shell Programming and Scripting

exec

In exec function say when i would like to remove the files exec rm{}\; Why is this "\" needed immediately after {} and what if i dont give it? TIA, Nisha (1 Reply)
Discussion started by: Nisha
1 Replies
Login or Register to Ask a Question