0

I am running a shell script which runs multiple jars and as a result, writes into different files located in different directories.

I want to get list of files created in a directory as an output of running the script in the last run.

I have my file name as date_time_ABC.txt. I thought of getting the file names by date but on a particular date I may run it many times, so I will get all the files including the ones created in previous runs also.

Is there a way I can get the list of the files which are generated after running the shell script. Please suggest.

0

Just an idea. Keep 2 directories - one with files touched by script (work directory) and second with synced files (mirror directory). Put on the first line of script syncing command:

rsync -xrlptgoEvv /home/user/work/ /home/user/mirror/

and at the end of script command to detect difference between work and mirror directories with redirection of output to a plain text file:

diff -rq /home/user/work/ /home/user/mirror/ > /home/user/list-of-files.txt

You can additionally filter output of diff command with sed, awk etc. to have necessary format of your data. Also consider --delete option with rsync to remove from mirror directory files which have been deleted from work directory since last synchronization.

UPDATE. The better way possibly is to create list of files on top of script:

ls -la /home/user/work/ > /home/user/list-of-files.txt

and on bottom of script append new list of files to old list, sort it and filter unique lines which will represent new files only:

ls -la /home/user/work/ >> /home/user/list-of-files.txt
cat /home/user/list-of-files.txt | sort | uniq -u > /home/user/list-of-new-files.txt
  • @ArchitGoyal You're welcome! Please, see updated version of answer. – Bob Jul 23 '18 at 18:38
1

if your time does not contain second, you could use ls with -u option:

-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first

otherwise, you could list current files with ls -u -1>../Before and run it again after your code ls -u -1 > ../After then use comm -3 ../After ../Before and do your job

  • I need to automate the process and generate the files in the script and then copy the files generated to other machine and use it. When the files are generated, there may be multiple files generated on that day. How can I segregate them by test run and get the file list – LearningToCode Jul 23 '18 at 17:56
  • first please test above and let me know the result. thanks. – Hossein Vatani Jul 23 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.