Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
There is a website called "Command Line Fu" that has lots of other ideas also. –  djangofan Jan 30 '12 at 1:45
add comment

migrated from stackoverflow.com Jan 30 '12 at 1:01

This question came from our site for professional and enthusiast programmers.

4 Answers

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).

share|improve this answer
    
umask 022 tar zxvf file.tar.gz --> this asks me for confirmation for each file separately if I want to extract it –  Kaspar Lemmo Palgi-Unt Jan 29 '12 at 14:35
    
Are you over-writing existing files when doing so? –  sarnold Jan 30 '12 at 0:30
add comment

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
share|improve this answer
    
But the second command gives me on many files this output: chmod: cannot access the': No such file or directory chmod: cannot access Logo.txt': No such file or directory chmod: cannot access `./blog/wp-content/themes/OneRoom/LOGO': No such file or di rectory –  Kaspar Lemmo Palgi-Unt Jan 29 '12 at 15:22
1  
777 is dangerous and only applicable to a very small minority of systems. –  sarnold Jan 30 '12 at 0:31
2  
To 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 Jan 30 '12 at 0:32
add comment

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 {}\;
share|improve this answer
    
find: missing argument to `-exec' –  Kaspar Lemmo Palgi-Unt Jan 25 '12 at 16:43
    
any idea why this command says to me "find: missing argument to `-exec'"? Thanks. –  Kaspar Lemmo Palgi-Unt Jan 29 '12 at 14:37
add comment

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

share|improve this answer
1  
Welcome to Superuser, please be advised 1 line answers are rarely accepted, can you elaborate more on your answer? –  50-3 Sep 30 '13 at 1:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.