4

Can I set permissions when I create a file/directory using a single command or do I have to create the file/directory first and then use chmod to set its permissions?

For instance to do something like this

// for directories
mkdir 755 test 

// for files
touch 644 test/my_file.php
ltdev
  • 3,461
  • 17
  • 60
  • 117

3 Answers3

7

For files, try using install command:

$ install -m 644 /test/path/ myfile.php

For folders, mkdir with -m param:

$ mkdir -m 755 test

You might have to execute that as sudo.

henriale
  • 982
  • 8
  • 20
3

Man pages are your friend. This is possible with GNU mkdir but not with GNU touch.

mkdir -m 755 test
jangler
  • 919
  • 6
  • 7
0

you can use following command to create directory and give permissions at the same time

mkdir -m755 test
Omer Gafar
  • 99
  • 1
  • 6