LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2012, 07:05 AM   #1
Utsaki
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
How to obtain more than one information from the same result


Hello to everybody!

Well, first of all I have to say I'm starting programming in Bash, so I haven't got a deep knowledge about it... so perhaps this question may seem to be too obvious for more than one of you.

Anyway, I want to solve this problem: I'm trying to get multiple data (specifically, the file name, user and date of last access) from the same result. So far, what I've done is someting like this:

Code:
find . -mtime -1 | stat -c %x $(awk '{print $1}')
What gives me the date and time of modification of files that have been modified in the last 24 hours.

However, I also want to attach the file name and the associated user in the same line. I have already tried several things, but I cannot find how to run 3 stat commands on the same line to provide the whole information. Is it possible? Or do I need to store the result of the find command in temporary variable previously?

Thank you very much in advance.
Regards.
 
Old 11-16-2012, 07:27 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello Utsaki, welcome to LQ,

you could for example use the ls command with option -l with the pipe
Code:
find . -type f -mtime -1 | ls -l | awk '{ print $3 " " $NF }'
Here is a great tutorial: http://tldp.org/LDP/abs/html/

Markus
 
Old 11-16-2012, 07:29 AM   #3
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
I'm not sure you can get who accessed the file unless you are running the audit daemon and are auditing for file access. You can get file name and access time with:
Code:
find . -type f -mtime -a -exec /bin/ls -lu {} \;
Is that more what you are after?
 
Old 11-16-2012, 08:10 AM   #4
Utsaki
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
Hello Utsaki, welcome to LQ,

you could for example use the ls command with option -l with the pipe
Code:
find . -type f -mtime -1 | ls -l | awk '{ print $3 " " $NF }'
Here is a great tutorial: http://tldp.org/LDP/abs/html/

Markus
Thank you for your help, Markush.

In fact, one of the options I'm trying is using the ls command. But I noticed I didn't get the same result using these two lines:

Code:
find . -mtime -1 -ls
Code:
find . -mtime -1 | ls -l
The first one really gives me the long listing format of the last modified files; the second one gives me the long listing format of the whole directory.

Anyway, I still have the same problem. For example, if I want to show the name file, the username who owns the file and the date of the last modification, everything is ok, I just write, for instance, the following line:

Code:
find . -mtime -1 -ls | awk '{printf $11; printf $5; printf $9;printf $8; printf $10; printf "\n"}'
But if I wanted to obtain the date of the last access instead, I couldn't write the same code, using the -atime extension. That is, if I write the following line:

Code:
find . -atime -1 -ls | awk '{printf $11; printf $5; printf $9;printf $8; printf $10; printf "\n"}'
I really get the files I was looking for, but the 8, 9 and 10 columns have the date of the last modification, not the date of the last access. How could I insert the stat command in the line above to get the right date? I'm trying things like this (without success, obviously):

Code:
find . -atime -1 -ls | awk '{printf $11; printf $5; printf $(stat -c %x $(awk '{print $11}'); printf "\n"}'
Thanks in advance again.
Regards.
 
Old 11-16-2012, 08:46 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
mh, I think I made a mistake. But you did not understand what I did as well.

I've used the find command together with the ls command. ls with option -l or better -lh gives you all information you need, namely the filename and the time and others. What you try is to use find with an option -ls which is something completely different.

My mistake was that I did not recognize, that ls does not use the output of the find command. One needs the -exec option for find.
Code:
find . -type f -mtime -1 -exec  ls -lh {} \;
this will find the files with the appropriate mtime and ls will show them.

Markus
 
Old 11-16-2012, 09:14 AM   #6
Utsaki
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes, I know what you mean and I also expected that the ls command used the output from the previous command. Anyway, with the code you provided to me in your last message, I still have the same problem with the information of the date an hour. Using the line:

Code:
find . -type f -atime -1 -exec  ls -lh {} \;
I don't get the same date and hour than when I use the line:

Code:
stat -c %x namefile
That's the reason why I was trying to combine the stat commands with the output from find.Do you think it could be possible to do? I think so, but I don't know how...

Thank you one more time for your patience.
Regards.
 
Old 11-16-2012, 09:37 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How about something like
Code:
find . -mtime -1 | stat --printf 'Modification: %x\nFile name: %n\nUser: %U\n' $(awk '{print $1}')
Replace all the \n strings except the last with a space to get output on one line.
 
Old 11-16-2012, 09:44 AM   #8
Utsaki
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes! That's it!

Thank you very much, catkin
Well, thank you to everyone for your help and support.
 
Old 11-16-2012, 10:00 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Glad it worked for you

Threads can be marked SOLVED via the Thread Tools menu.
 
Old 11-16-2012, 10:11 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You can get a file's info directly from find:
Code:
find . -mtime -1 -printf 'name: %p user: %u modification: %a\n'
 
2 members found this post helpful.
  


Reply



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
Obtain information about an installed library littleplane Programming 1 07-15-2012 10:07 AM
Where to obtain information for applications jstephens84 Programming 2 12-03-2008 07:59 AM
How to obtain debug information from a section!!!!! webquinty Linux - Newbie 2 10-30-2008 04:23 AM
Wifi not working on FC8. Unable to obtain IP information. neolithic_psyche Fedora 2 04-11-2008 01:48 PM
how to obtain cd session information nIMBVS Linux - Newbie 1 10-26-2003 05:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 AM.

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