LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to obtain more than one information from the same result (https://www.linuxquestions.org/questions/programming-9/how-to-obtain-more-than-one-information-from-the-same-result-4175437422/)

Utsaki 11-16-2012 07:05 AM

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.

markush 11-16-2012 07:27 AM

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

bradvan 11-16-2012 07:29 AM

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?

Utsaki 11-16-2012 08:10 AM

Quote:

Originally Posted by markush (Post 4830743)
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.

markush 11-16-2012 08:46 AM

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

Utsaki 11-16-2012 09:14 AM

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.

catkin 11-16-2012 09:37 AM

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.

Utsaki 11-16-2012 09:44 AM

Yes! That's it!

Thank you very much, catkin :)
Well, thank you to everyone for your help and support.

catkin 11-16-2012 10:00 AM

Glad it worked for you :)

Threads can be marked SOLVED via the Thread Tools menu.

ntubski 11-16-2012 10:11 AM

You can get a file's info directly from find:
Code:

find . -mtime -1 -printf 'name: %p user: %u modification: %a\n'


All times are GMT -5. The time now is 10:42 AM.