Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
But instead of applying ls -l only on files modified in the last 24 hours, it lists all files in the current directory. How can I make list only the files found by find?
But instead of applying ls -l only on files modified in the last 24 hours, it lists all files in the current directory. How can I make list only the files found by find?
Try the man page for the find command; look at the '-ls' option, which may do what you want. There are also options to have find use printf to return whatever you're after.
In the manual I did find out that there's a direct option called "-ls", which indeed does display only the files found by find. The problem is that it imposes a certain format, and I'd have liked to use ls options.
Besides avoiding directories, the -exec will only operate on the records found, by the find command.
Are you saying that you do not believe this to be the case? For instance are you saying that you're seeing files last modified beyond up to 24 hours ago?
Note also that I do not believe you are supposed to be using a MINUS 1.
Your original code:
Code:
find . -mtime -1 -exec ls -l {} \;
Quote:
-mtime n
File's data was last modified n*24 hours ago.
In the manual I did find out that there's a direct option called "-ls", which indeed does display only the files found by find. The problem is that it imposes a certain format, and I'd have liked to use ls options.
No, because again, the man page for find has options for using printf, to return whatever you'd like:
Code:
-printfformat
%M File's permissions (in symbolic form, as for ls). This directive is supported in findutils 4.2.5 and later.
%u File's user name, or numeric user ID if the user has no name.
%g File's group name, or numeric group ID if the group has no name.
%s File's size in bytes.
%Ak File's last access time in the format specified by k, which is either `@' or a directive for the C `strftime' function.
%f File's name with any leading directories removed (only the last element).
...which is pretty much what an "ls -l" would give you:
It also finds the current directory "./" to have changed, and "./" as an argument to ls -l will list you the whole directory. So you need to exclude the current directory from the search results, like this:
Thank you all for your answers. Really helpful, and thank you aragon for explaining exactly what is going on.
Do you agree with rtmistler statement that I shouldn't be using minus 1? I don't really see why, because it obviously prints different information (-1 means displaying files that were changed in the last 24 hours).
@rtmistler Yes, if I simply run find . -mtime -1, then it will only display files that were changed in the last 24 hours. If I add -exec -ls -l {} \, then it will display all files - this is already explained in the posts above. And yes, now it makes sense.
Do you agree with rtmistler statement that I shouldn't be using minus 1?
Yes. You should avoid parsing output from ls. TB0ne mentioned using the printf function built into find itself. With it you can get just the data you want about the files:
Yes, I know the difference, but what intrigued me was rtmistler saying that. I didn't know why he'd think I shouldn't be using "-mtime -1"
I was incorrect with that assumption. The documentation seemed to imply that it already does a minus, and that was what I cited. However in testing, you do need to use the minus, and perhaps the documentation is clear and just not my interpretation of it. As Turbocapitalist cites, try each out and you shall see.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.