How to Create a Directory and Give Permission in Single Command

How to create a directory and give permission in single command

According to mkdir's man page...

mkdir -m 777 dirname

Can I set permissions when creating a file or directory?

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.

How do I change permissions for a folder and its subfolders/files?

The other answers are correct, in that chmod -R 755 will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?

I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example:

To change all the directories to 755 (drwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

Some splainin': (thanks @tobbez)

  • chmod 755 {} specifies the command that will be executed by find for each directory
  • chmod 644 {} specifies the command that will be executed by find for each file
  • {} is replaced by the path
  • ; the semicolon tells find that this is the end of the command it's supposed to execute
  • \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find

How to grant permission to users for a directory using command line in Windows?

As of Vista, cacls is deprecated. Here's the first couple of help lines:

C:\>cacls
NOTE: Cacls is now deprecated, please use Icacls.

Displays or modifies access control lists (ACLs) of files

You should use icacls instead. This is how you grant John full control over D:\test folder and all its subfolders:

C:\>icacls "D:\test" /grant John:(OI)(CI)F /T

According do MS documentation:

  • F = Full Control
  • CI = Container Inherit - This flag indicates that subordinate containers will inherit this ACE.
  • OI = Object Inherit - This flag indicates that subordinate files will inherit the ACE.
  • /T = Apply recursively to existing files and sub-folders. (OI and CI only apply to new files and sub-folders). Credit: comment by @AlexSpence.

For complete documentation, you may run "icacls" with no arguments or see the Microsoft documentation here and here

How to create a directory using Ansible

You want the file module. To create a directory, you need to specify the option state=directory :

- name: Creates directory
file:
path: /src/www
state: directory

You can see other options at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html

Chmod 777 to a folder and all contents

If you are going for a console command it would be:

chmod -R 777 /www/store. The -R (or --recursive) options make it recursive.

Or if you want to make all the files in the current directory have all permissions type:

chmod -R 777 ./

If you need more info about chmod command see: File permission

Getting permission denied while creating a directory and how to add suffix to .exe to each files in a directory

I used mkdir source_dir -> But getting error cannot create directory. Permission denied.

It seems you do not have permission to create a fodler here. You might use sudo mkdir source_dir, but is likely a better idea to make the folder in a directory where you have write access EG. $HOME.

Once error is resolved and files are created in source_dir then I will use mv .* source_dir destination_dir -> To move all the files at once but for this command I am not sure whether this will work or not

For moving use mv .* destination_dir from withing the source_dir. (IE, first cd source_dir then run the move command from above)

Then how to suffix all the files with .exe is also challenging to me and got stuck.

You will have to loop over the files and move them one by one.

for i in * ; do mv "$i" "${i}.exe" ; done 


Related Topics



Leave a reply



Submit