Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single command to create a file and set its permission

I am using the following 2 commands to create a 0B file and set its extn to 644

touch filename.ext chmod 777 filename.txt 

My question is that whether there is any single command in unix (korn shell) that will do the two things together that is to say create a 0B fil with desired permission?

like image 689
user3075776 Avatar asked Jan 24 '14 20:01

user3075776


People also ask

What is the command to set permissions?

To change file and directory permissions, use the command chmod (change mode). The owner of a file can change the permissions for user ( u ), group ( g ), or others ( o ) by adding ( + ) or subtracting ( - ) the read, write, and execute permissions.

What does chmod command 777 mean?

Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users and may pose a huge security risk.


2 Answers

install -m 777 /dev/null filename.txt 
like image 103
proski Avatar answered Sep 17 '22 21:09

proski


For bash, simply use chmod with file redirection and history expansion:

chmod 777 filename.txt>>!#:2 

For ksh and zsh you have to drop the history expansion (as shown above, there may be other ways) and use:

chmod 644 filename>>filename 

For scripts of any shell you don't have (and really don't need) history expansion so use the above there as well.

like image 45
Paul Evans Avatar answered Sep 17 '22 21:09

Paul Evans