Skip to content

Instantly share code, notes, and snippets.

@cverbiest
Last active August 29, 2015 14:23
Show Gist options
  • Save cverbiest/51a31afa415561945c4d to your computer and use it in GitHub Desktop.
Save cverbiest/51a31afa415561945c4d to your computer and use it in GitHub Desktop.
Create a file/directory with same owner and permissions
#!/bin/bash
# Create a file / directory with same rights as an exiting one
typeset local
Usage() {
echo Usage $0 -r reference_file|reference_dir new_file|new_dir ...
exit 1
}
while getopts ":r:" option
do
case "${option}" in
r)
reference=${OPTARG}
;;
*)
Usage
;;
esac
done
shift $((OPTIND-1))
if [ -d $reference ]
then
mkdir $*
elif [ -f $reference ]
then
touch $*
else
Usage
exit
fi
chown --reference=$reference $*
chmod --reference=$reference $*
@cverbiest
Copy link
Author

I find this useful with sudo,

    sudo mklike -r existing_dir newdir

instead of

    sudo mkdir newdir
    sudo chown ... newdir
    sudo chmod ... newdir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment