0

I have a line in my bash script that goes like this:

cp -v /temp1/* /temp2/* >> $LOGFILE

$LOGFILE is basically that - a log file. The result I currently get in the $LOGFILE is this:

‘/temp1/file1’ -> ‘/temp2/file1’
‘/temp1/file2’ -> ‘/temp2/file2’
‘/temp1/file3’ -> ‘/temp2/file3’
‘/temp1/file4’ -> ‘/temp2/file4’

First of all, I'd like to get rid of the prefix "‘" and trailing "‘". Secondly, I'd either like to show the current time (in hh:mm format) or elapsed time so $LOGFILE will show:

10:24 /temp1/file1 -> /temp2/file1
10:27 /temp1/file2 -> /temp2/file2

or

00:03 /temp1/file1 -> /temp2/file1
00:27 /temp1/file2 -> /temp2/file2

Could someone please help me fix one or both these issues?

KoreanGuy
  • 37
  • 1
  • 6

2 Answers2

1

I think that ‘ and ’ instead of ' or " is due to an encoding problem (see for example here). What do you use to open $LOGFILE? Try with a cat $LOGFILE

You can use something like:

for fn in /temp1/*
do
    #hour and minutes
    d=$(date +%H:%M)
    # cp output
    c=$(cp -v $fn /temp2/)
    # echo
    echo $d $c >> $LOGFILE
done
Community
  • 1
  • 1
asclepix
  • 7,971
  • 3
  • 31
  • 41
  • As for the `‘`, at the end of my script is the following line `/usr/bin/sendemail -f $EMAILFROM -t $EMAILTO -o message-file=$STATUSLOG -u $EMAILSUBJ -s $EMAILSMTP -a $EMAILATT`. It is then opened in Outlook. In the actual logfile, it displays correctly as a `'`. – KoreanGuy Jul 01 '15 at 13:25
  • So, change the encoding in Outlook or replace the character in the script adding something like (as suggested in the other answer) `c=$(echo $c | sed "s|[\\`\']||g" -)` – asclepix Jul 01 '15 at 13:47
0

I believe you have to use a loop for this, and copy the files one by one, then print the date manually before each copy:

(for i in test1/*; do \
   printf "%s: " $(date +"%M:%S"); \
   cp -v $i ${i/test1/test2} | sed "s|[\`\']||g"; \
 done) >> log.file

The funny characters you are seeing are caused by your terminal's encoding. They are the equivalents of ` and ', so the sed statement I provided should work (can't test locally as I don't have whatever encoding you are using).

----- EDIT -----

If you want to find all files under the directory (not just those directly in test1), you could use find with -exec as follows:

find test1 -type f -exec printf "%s: " $(date +"%M:%S") \; -exec echo {} \;

This will find all files, and run the two commands for each. {} expands to the filename which was found. The \; tells find that the command is done. Do a man find for more info.

John
  • 3,400
  • 3
  • 31
  • 47
  • I couldn't figure out how to modify your script's cp for subdirectories (such as `/dir1/test1`. I will use your sed on the logfile to fix the encoding! – KoreanGuy Jul 01 '15 at 13:34
  • if you want to do all files, change the loop to read: `for i in $(find test1 -type f); do`. This will iterate over all files, not just the ones directly under test1. – John Jul 03 '15 at 14:47