Bobby
Bobby

Reputation: 320

How to create a file with 777 permission?

In the following script, how can I add chmod 777 so that file can be created with 777 permission?

file="New_file.txt";cd \abc\efg\;  if [ ! -f $file ] ; then touch $file;  fi;

Upvotes: 0

Views: 5475

Answers (2)

fedorqui
fedorqui

Reputation: 290415

It looks quite obvious:

file="New_file.txt"
cd "your_dir"
if [ ! -f "$file" ]; then
   touch "$file"
   chmod 777 "$file"
fi

Note you should quote variables and also in cd, so that you don't have to escape. It also prevents unexpected behaviours if the file name contain spaces, new lines...

One liner:

file="new"; cd "your_dir"; if [ ! -f "$file" ]; then touch "$file"; chmod 777 "$file"; fi

Upvotes: 1

SMA
SMA

Reputation: 37093

Try this:

file="New_file.txt"; cd /abc/efg/; if [ ! -f $file ] ; then touch $file;  chmod 777 $file; fi

Upvotes: 1

Related Questions