LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-12-2017, 08:29 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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?
 
Old 07-12-2017, 09:01 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,183

Rep: Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062
Quote:
Originally Posted by vincix View Post
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.
 
Old 07-12-2017, 09:11 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
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.
Old 07-12-2017, 09:39 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
 
Old 07-12-2017, 09:47 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
Quote:
Originally Posted by vincix View Post
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.
Old 07-12-2017, 09:54 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
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.
 
Old 07-12-2017, 10:01 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,183

Rep: Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062Reputation: 8062
Quote:
Originally Posted by vincix View Post
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.
Old 07-13-2017, 12:42 AM   #8
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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.
Old 07-13-2017, 01:22 AM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,297
Blog Entries: 24

Rep: Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255Reputation: 4255
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.
Old 07-13-2017, 01:31 AM   #10
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
 
Old 07-13-2017, 01:57 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
Quote:
Originally Posted by vincix View Post
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

Code:
man find
 
Old 07-13-2017, 02:00 AM   #12
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
 
Old 07-13-2017, 03:10 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
Quote:
Originally Posted by vincix View Post
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.

Code:
man find
 
Old 07-13-2017, 03:20 AM   #14
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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"
 
Old 07-13-2017, 06:40 AM   #15
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
Quote:
Originally Posted by vincix View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to use 'grep' for files found from 'find' .. susja Linux - Newbie 32 06-23-2016 11:28 PM
Find command - alert on files not found SmurfGGM Linux - General 3 06-13-2014 08:22 AM
find found files that don't exist? mocax Linux - Newbie 3 12-08-2010 06:13 AM
How to find a file and just list the found files? thomas2004ch Linux - Newbie 4 08-07-2009 04:54 AM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-04-2008 11:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration