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 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.