3

What is the best way to create a new, empty file that has the same ownership and perms as an existing file for a Solaris /bin/sh shell script?

This is for rotating a log file away for compression and dump storage.

At the moment I am using:

mv log log_ts && cp -p log_ts log && cp /dev/null log

Is there a better way to do it?

Update: Unfortunately this is on a conservatively built live Sol10 server and no gnu fileutils are available.

Rob Wells
  • 36,220
  • 13
  • 81
  • 146

2 Answers2

2

I do this in GNU, I guess it would work on Solaris too:

touch new_file && chmod --reference=old_file new_file
Untitled
  • 781
  • 1
  • 6
  • 24
  • 2
    I guess you'd want to `chown --reference=old_file new_file` as well in order to meet the OP's requirement? – Celada Nov 19 '12 at 14:18
  • Thanks Sahand and Celada for the suggestions but unfortunately this server cannot have any GNU fileutils installed. (I've updated my question with this fact) – Rob Wells Nov 19 '12 at 14:43
1

You will need do something like this:

PERM=`ls -l $EXISTING | cut -d" " -f1 | cut -c2- `
PERMU=`echo $PERM |cut -c1-3 | sed s/-//g`
PERMG=`echo $PERM |cut -c4-6 | sed s/-//g`
PERMO=`echo $PERM |cut -c7-9 | sed s/-//g`
chmod u="$PERMU" $TARGET
chmod g="$PERMG" $TARGET
chmod o="$PERMO" $TARGET

Naturally this is for Solaris.

PaulB
  • 357
  • 2
  • 2