Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new empty file from linux command line with same permissions and ownership?

I need to create a new empty file with the same permissions and ownership (group and user) as the source, kind of like how cp -p FILE1 FILE1.bak works but without actually copying the contents.

I know I can empty out the contents later on, but that seems wasteful.

I cant use a script - the solution must run from the command line directly.

like image 917
siliconpi Avatar asked Nov 15 '11 11:11

siliconpi


People also ask

How do I copy file permissions and ownership to another file in Linux?

To copy file permissions from one file to another file, use chmod command with the --reference switch in the following syntax, where reference_file is the file from which permissions will be copied rather than specifying mode (i.e octal or numerical mode permissions) for file.

How do I copy with the same permissions in Linux?

Preserve File Permissions Using cp You can use the -p option of cp to preserve the mode, ownership, and timestamps of the file. However, you will need to add the -r option to this command when dealing with directories. It will copy all sub-directories and individual files, keeping their original permissions intact.


2 Answers

touch newfile
chmod `stat -c %a originalfile` newfile
chown `stat -c %U originalfile`:`stat -c %G originalfile` newfile
like image 58
Māris Kiseļovs Avatar answered Sep 19 '22 11:09

Māris Kiseļovs


Use

touch newfile
chmod --reference=oldfile newfile
chown --reference=oldfile newfile
like image 24
Tudor Avatar answered Sep 20 '22 11:09

Tudor