Home > Net >  How to add day date in shell script
How to add day date in shell script

Time:04-15

i have the following script and i would like to add the date of the day it is rotated in a new column at the end.

find /sasdata -type f -exec sh -c \
   'for f; do stat --format="%n,%x,%y,%z,$(du -k "$f" | cut -f1)" "$f"; done' \
   _ {}   > /sasdata/output_sasdata_file_info.txt

Columns are separated by commas.

The output I have so far is:

/sasdata/arquivo.gz,2019-02-04 15:16:55.886454268 -0200,2018-10-02 15:49:49.936062260 -0300,2021-02-01 16:40:26.542568391 -0300,19392

I wanted the output to be as follows:

/sasdata/arquivo.gz,2019-02-04 15:16:55.886454268 -0200,2018-10-02 15:49:49.936062260 -0300,2021-02-01 16:40:26.542568391 -0300,19392,2022-04-10

I appreciate anyone who can help me.

CodePudding user response:

Try this :

#!/usr/bin/env bash
  
today=$(date  %F)

find /sasdata -type f -exec sh -c \
   'for f; do
        stat --format="%n,%x,%y,%z,$(du -k "$f" | cut -f1)"',$today' "$f"
    done' \
   _ {}   > /sasdata/output_sasdata_file_info.txt

CodePudding user response:

Catch output of your stat statement using $(...) command substitution and pass it to echo -n command, to output it without the newline at the end. Then follow it with the date command.

Example how it works:

$ ls -d /; date  %F # outputs:
/
2022-04-14

$ echo -n $( ls -d / ),; date  %F # outputs
/,2022-04-14

So your command that you pass to the shell should be something like:

sh -c 'for f; do echo -n $( stat --format="%n,%x,%y,%z,$(du -k "$f" | cut -f1)" "$f" ),; date  %F; done'
  • Related