doman412
doman412

Reputation: 190

finding most recent file version from list of file path names with jumbled file names

I recently lost a bunch of files from eclipse in an accidental copy/replace dilema. I was able to recover most of them but I found in the eclipse metadata folder a history of files, some of which are the ones I need. The path for the history is:

($WORKSPACE/.metadata/.plugins/org.eclipse.core.resources/.history). 

Inside there are a bunch of folders like 3e,2f,1a,ff, etc.. each with a couple files named like "2054f7f9a0d30012175be7013ca49f5b". I was able to do a recursive grep with a keyword i know would be in the file and return a list of file names (grep -R -l 'KEYWORD') and now I can't figure out how to sort them by most recently modified.

any help would be great, thanks!

Upvotes: 2

Views: 92

Answers (1)

clt60
clt60

Reputation: 63902

you can try:

find $WORK.../.history -type f -printf '%T@\t%p\n' | sort -nr | cut -f2- | xargs grep 'your_pattern'

Decomposed:

  • the find finds all plain files and prints their modification time and path
  • the sort sort sort them numerically - and reverse, so highest number comes first (the latest modified)
  • the cut removes the time from each line
  • the xargs run its argument for each file what get to it input,
  • in this case will run the grep command, so
  • the 1st file what the grep find - was the lastest modified

The above not works when the filenames containing spaces, but hopefully this is not your case... The -printf works only with GNU find.

For the repetative work, you can split the command to two parts:

find $WORK.../.history -type f -printf '%T@\t%p\n' | sort -nr | cut -f2- > /somewhere/FILENAMES_SORTED_BY_MODIF_TIME

so in 1st step you save to somewhere the list of filenames sorted by their modification times, and after you can repeatedly use the grep command on their content with:

< /somewhere/FILENAMES_SORTED_BY_MODIF_TIME xargs grep 'your_pattern'

the above command is usually written as

xargs grep 'your_pattern' < /somewhere/FILENAMES_SORTED_BY_MODIF_TIME 

but for the bash is OK write the redirection to the start and in this case is simpler changing the pattern for the grep if the pattern is in the last place...

If you want check the list of filenames with modification times, you can break the above commands as:

find $WORK.../.history -type f -printf "%T@\t%Tc\t%p\n" | sort -nr >/somewehre/FILENAMES_WITH_DATE

check the list (they now contains readable date too) and use the next

< /somewehre/FILENAMES_WITH_DATE cut -f3- | xargs grep 'your_pattern'

note, now need to use -f3- and not -f2- as in the 1st example.

Upvotes: 1

Related Questions