Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
07-12-2017, 08:29 AM
|
#1
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Rep:
|
ls -l applied only on files found with find
Hi,
I'm running this command:
Code:
find . -mtime -1 -exec ls -l {} \;
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?
|
|
|
07-12-2017, 09:01 AM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,183
|
Quote:
Originally Posted by vincix
Hi,
I'm running this command:
Code:
find . -mtime -1 -exec ls -l {} \;
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.
Last edited by TB0ne; 07-12-2017 at 09:03 AM.
|
|
|
07-12-2017, 09:11 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
My guess is that it is also finding directories that have been modified within the time limit, including the current directory "."
So try adding "-type f" in the formula near the beginning. Again, see "man find" frequently
|
|
2 members found this post helpful.
|
07-12-2017, 09:39 AM
|
#4
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
Actually in the respective directory there are only files.
Code:
echo $pwd
/opt/zimbra/store/0/2/msg/0
find . -mtime -1
.
./1941-59511.msg
./1942-59643.msg
./1943-59647.msg
./1944-59648.msg
./1945-59649.msg
./1946-59650.msg
./1947-59739.msg
./1948-59740.msg
./1949-59741.msg
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.
|
|
|
07-12-2017, 09:47 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
Quote:
Originally Posted by vincix
Actually in the respective directory there are only files.
Code:
echo $pwd
/opt/zimbra/store/0/2/msg/0
find . -mtime -1
.
|
That first item, the period there, is a directory. Try limiting the search to regular files only as suggested above.
Code:
find . -type f -mtime -1 -print;
|
|
1 members found this post helpful.
|
07-12-2017, 09:54 AM
|
#6
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
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.
|
|
|
|
07-12-2017, 10:01 AM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,183
|
Quote:
Originally Posted by vincix
Actually in the respective directory there are only files.
Code:
echo $pwd
/opt/zimbra/store/0/2/msg/0
find . -mtime -1
.
./1941-59511.msg
./1942-59643.msg
./1943-59647.msg
./1944-59648.msg
./1945-59649.msg
./1946-59650.msg
./1947-59739.msg
./1948-59740.msg
./1949-59741.msg
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:
-printf format
%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:
Code:
drwxr-xr-x 2 username usergorup 4096 Jul 12 09:38 DIRNAME
"man find"
|
|
1 members found this post helpful.
|
07-13-2017, 12:42 AM
|
#8
|
Member
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567
|
Hi,
The thing is when you do
Code:
find . -mtime -1 -exec ls -l {} \;
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:
Code:
find . ! -path . -mtime -1 -exec ls -l {} \;
|
|
1 members found this post helpful.
|
07-13-2017, 01:22 AM
|
#9
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,297
|
As first mentioned by Turbocapitalist and repeated by others, just try the -type f.
Change this...
Code:
find . -mtime -1 -exec ls -l {} \;
...to this...
Code:
find . -type f -mtime -1 -exec ls -l {} \;
That will show all files modified within the last 24 hours, as requested.
The reason this works is already stated above... give it a try!
|
|
1 members found this post helpful.
|
07-13-2017, 01:31 AM
|
#10
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
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.
Last edited by vincix; 07-13-2017 at 03:36 AM.
|
|
|
07-13-2017, 01:57 AM
|
#11
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
Quote:
Originally Posted by vincix
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:
Code:
. . . -printf "%u\t%g\t%AY%Am%Ad\t%AH:%AM:%AS\t%-10s\t%f\n"
See
|
|
|
07-13-2017, 02:00 AM
|
#12
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
So rtmistler was talking only in the context of parsing output from ls? I was actually asking if "find . -mtime -1" by itself is a legitimate command.
|
|
|
07-13-2017, 03:10 AM
|
#13
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
Quote:
Originally Posted by vincix
So rtmistler was talking only in the context of parsing output from ls? I was actually asking if "find . -mtime -1" by itself is a legitimate command.
|
rtmistler was referring to -mtime. I got the posts mixed but still emphasize avoiding ls
With -mtime, compare the following:
Code:
find . -type f -mtime -1 -print;
find . -type f -mtime 1 -print;
find . -type f -mtime +1 -print;
Also take a look at -daystart in the manual to see if that is of any use.
|
|
|
07-13-2017, 03:20 AM
|
#14
|
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240
Original Poster
Rep:
|
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"
|
|
|
07-13-2017, 06:40 AM
|
#15
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
Quote:
Originally Posted by vincix
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.
|
|
|
All times are GMT -5. The time now is 03:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|