2
find /user/stc/tmp -mmin -200 -ls | grep '.txt' | awk '{print $8, $9, $10, $11}' >> tempresult.txt

First of all I am a newbie, I am trying to find out ONLY .txt files modified in last week(in home/user/temp) and save the result to a file. so some how i figured it out, but I need help with to save only file name without path. I can print it with

awk '{print \$NF}'

but when i try that in combination with above command It's not working as expected.

Any help/suggestion/Improvement is highly appreciated. Thank you in advance.

Opal
  • 81,889
  • 28
  • 189
  • 210
Reyaz eon
  • 23
  • 2

2 Answers2

4

That pipeline is very over-complicated for the task.

find supports all the filtering and formatting support you need to do what you want without the grep/awk pipeline at all. (Not to mention that piping grep to awk is almost never necessary in the first place.)

Try:

find /usr/stc/tmp -mmin -200 -a -name '*.txt' -printf '%P\n'
  • -a to combine find specifiers (though that is the default operation too)
  • -name '*.txt' to select only files that match the desired name format
  • -printf to select the format of the output
  • '%P\n' to output the name of the found file with the command line argument under which it was found removed (followed by a newline).

If you actually meant just the file name (no path components at all) then you want the %f format string instead of %P (thanks Xorg).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • 1
    He did say only the filename, not the path - that would be "%p" rather than "%P". Also, it would help to have a "-type f" (just in case). – Thomas Dickey Feb 17 '15 at 15:47
  • 1
    "-mmin 200" is a lot shorter than a week (I would just use "-mtime -7"). – Thomas Dickey Feb 17 '15 at 15:48
  • 1
    using the "%p" will not ignore any leading subdirectory the file is in..."%f" will do..refer to find manpages – repzero Feb 17 '15 at 22:44
  • @Xrog Thanks guys for the help..But I need to take only few fields, **File name without path, Date time and year**. This will out put all the details. Any suggestions ? – Reyaz eon Feb 18 '15 at 07:00
  • You want the `%t` or `%T` format strings for that. Look at the find manual for more details about the `%T` specifies to see what you can get from it. – Etan Reisner Feb 18 '15 at 12:38
  • @Reyaz eon...I was at work...however i edited my answer for your request – repzero Feb 18 '15 at 23:09
1
find /user/stc/tmp -mmin -200 -printf "%f\n" > log_file

or

  find /user/stc/tmp -mtime -7 -printf "%f\n" > log_file

..

-mtime -7 # in the last 7 days

the %f will ignore any subdirectory the file is in.

EDIT for user request

 find /user/stc/tmp -mtime -7 -printf "%Td-%Tm-%TY %Tr %TY %f\n" > log_file


%Td-%Tm-%TY        #modification time: day-month-year
%Tr                #modification date time
%TY                #modification date year

note also linux keeps track of modification dates instead of creation dates...

repzero
  • 8,254
  • 2
  • 18
  • 40