Extract file and folders with specified permissions

11

1

I pack a folder with lot of sub-folders and files in Windows with 7zip, upload to VPS and then run the command:

tar -xvzf file.tar.gz

then all unpacked files and folders have permissions of 777. How do I get it so that folders will have permissions of 755 and files 644?

Kaspar L. Palgi

Posted 2012-01-23T00:37:34.520

Reputation: 231

There is a website called "Command Line Fu" that has lots of other ideas also. – djangofan – 2012-01-30T01:45:36.933

Answers

13

If you're running tar(1) as a regular user, it will apply your umask by default. If you're running tar(1) as root, then you can give --no-same-permissions command line option to ask tar(1) to respect the umask.

So: either run this as a regular user:

umask 022
tar zxvf file.tar.gz

or run this as root:

umask 022
tar zxvf file.tar.gz --no-same-permissions

You might want to stick umask 022 into your ~/.bashrc, ~/.bash_profile, or ~/.profile. (See bash(1) manpage for full details on the start up files. It's complicated.)

Details on umask can be found in your shell's manpage, the umask(2) system-call manpage, or the umask(1posix) POSIX-provided utility manpage (if you have the manpages-posix installed).

sarnold

Posted 2012-01-23T00:37:34.520

Reputation: 2 988

umask 022 tar zxvf file.tar.gz --> this asks me for confirmation for each file separately if I want to extract it – None – 2012-01-29T14:35:44.640

Are you over-writing existing files when doing so? – sarnold – 2012-01-30T00:30:26.337

6

Run the following commands in the root of the directory to set the desired permissions for your directories and files:

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Be aware of the space between the closing curly bracket and the back slash

alxbrd

Posted 2012-01-23T00:37:34.520

Reputation: 163

Add a space between the closing curly bracket and the \; – poussma – 2014-12-16T08:36:59.323

find: missing argument to `-exec' – None – 2012-01-25T16:43:33.970

any idea why this command says to me "find: missing argument to `-exec'"? Thanks. – None – 2012-01-29T14:37:30.443

2

If you use capital X in chmod you can use it to set the execute permissions only on directories. i.e.

chmod -R ugo+X .

Stephen Quan

Posted 2012-01-23T00:37:34.520

Reputation: 226

1

I found this solution that worked for me. For folders and subfolders:

chmod -R 777 */

And for all files (also in folders and subfolders):

find . -type f -name "*" | xargs chmod 644

All comments welcome if this is not a good way of doing it. I just started learning Linux.

Following comments, a more robust solution that handles special characters nicely would be:

find . -type f -print0 | xargs -0 chmod 644  # For files
find . -type d -print0 | xargs -0 chmod 755  # For directories

Kaspar L. Palgi

Posted 2012-01-23T00:37:34.520

Reputation: 231

But the second command gives me on many files this output: chmod: cannot access the': No such file or directory chmod: cannot accessLogo.txt': No such file or directory chmod: cannot access `./blog/wp-content/themes/OneRoom/LOGO': No such file or di rectory – None – 2012-01-29T15:22:32.437

2777 is dangerous and only applicable to a very small minority of systems. – sarnold – 2012-01-30T00:31:00.693

2To improve upon your second command, find . -type f -print0 | xargs -0 chmod 644. The -name "*" isn't necessary, and xargs(1) will parse filenames on spaces or newlines unless you use the -0 -- which parses filenames on ASCII NUL character, which cannot appear in filenames. The -print0 for find(1) asks it to format its output in a mode suitable for xargs -0 input. – sarnold – 2012-01-30T00:32:18.667

0

find . -type f -exec chmod 644 {} \;

hamed

Posted 2012-01-23T00:37:34.520

Reputation: 1

2Welcome to Superuser, please be advised 1 line answers are rarely accepted, can you elaborate more on your answer? – 50-3 – 2013-09-30T01:11:00.907