LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting last X files, by modified date to build Rsync file list (https://www.linuxquestions.org/questions/programming-9/getting-last-x-files-by-modified-date-to-build-rsync-file-list-941795/)

nadigo 04-26-2012 02:23 AM

getting last X files, by modified date to build Rsync file list
 
hi,

I'm trying to (r)sync the last modified x files from a win share mounted to my linux (slim version of Ubuntu)

what I basically do is using "ls -lt" (-ltrRe) to build the list, than modify the ls output using regex to taking off dirs and symbolic links, remove all un-needed info, than cut the first x lines, fix the output in order to build a file list of the needed files Last X files to sync.

the problem is that I can't get the real path of the files using the regex as "ls -lt" list the directory on a separate line and i can't seem to get it right.


Code:


SOURCE=/media/win;
NumberOfFiles=5;

$LS -ltrRe $SOURCE/ > TMP_FILE

$CAT TMP_FILE | $GREP  -v '^[d-l]' | $AWK  '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10="";print $0}' | $GREP -v '^\s*$' | $SED -e "s/^ *//" | $SED -e "s/ *$//" |  $AWK 'NR<='${NumberOfFiles}'' | $AWK -v srch="^" -v repl="$SOURCE/" '{ sub(srch,repl,$0); print $0 }' > FileList


The above works for a single folder, once there are sub-dirs inside /media/win the code

Code:

$AWK -v srch="^" -v repl="$SOURCE/" '{ sub(srch,repl,$0); print $0 }'
is only adding the base path $SOURCE/ which is wrong.

any help ?


thanks,
Nadav

Ps i use bin files as variables such as $AWK as I plan to port the script, but they all link to the /bin/XYZ of ir

pan64 04-26-2012 04:26 AM

please do not make such commands:
cat|grep|awk|grep|sed|sed|awk|awk
Usually it can be replaced with one awk or perl script, that would be much better (readable).
But I suggest you to use find
Code:

find <rootdir> -type f -exec ls --time-style <format> {} \; | cut or sed what you do not need | sort | head -<XX lines>


All times are GMT -5. The time now is 01:28 AM.