Ubuntu – Shell to print modification date of all directories name match pattern

14.04bashcommand line

I want to write a shell program which will go through all folders whose names match a pattern like this:

sudo sh  shell.sh   pub 

When run, the script will look for all folders whose name contains pub and print its modification date.
I wish to have code which will print results like in this image:
enter image description here
I have this code but it is not giving me the result I expect.

echo  'the folder '$1' was modified at ';
 find  -type d -name 'kam*'  -exec stat -c '%y %n' '{}' \;

I must go through every folder and output text echo 'the file '$1' was modified at '; and print the date like in the image.

I would like a result like this:

netcom@hotspot:~$ bash script.sh testRegex Pub
the folder testRegex was modified on may 15 01:19
 the folder Public was modified on may 19 01:19 
  the folder Pubos was modified on may 19 01:19 

Best Answer

  • You can use find itself to print the whole thing:

    for pattern
    do
        find . -type d -name "*$pattern*" -printf 'The folder %P was modified on %t\n'
    done
    

    for pattern; do ... done loops over all the arguments, with the pattern variable set to each argument in turn.

    With find, %P and %t give the path to the file and modification time in -printf.

  • Related Question