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

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 .

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 {} \;

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.

1 Like

If you are only interested in files, not directories (or other types of inode) you need to specify "-type f".

find . -type f -mtime -10 -print

To stop "ls" expanding any directories it finds, use the "-d" switch to "ls".

find . -mtime -10 -exec ls -lad {} \;
find . -type f -mtime -10 -exec ls -lad {} \;
1 Like

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.

The first would also list any directory that happened to have been created with .txt on the end.

Naturally. I suspect there weren't any of those around, though.