13

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?

1
  • There is a website called "Command Line Fu" that has lots of other ideas also.
    – djangofan
    Jan 30, 2012 at 1:45

5 Answers 5

16

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

2
  • 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, 2012 at 14:35
  • Are you over-writing existing files when doing so?
    – sarnold
    Jan 30, 2012 at 0:30
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

4
  • find: missing argument to `-exec'
    – Kaspar Lemmo Palgi-Unt
    Jan 25, 2012 at 16:43
  • any idea why this command says to me "find: missing argument to `-exec'"? Thanks.
    – Kaspar Lemmo Palgi-Unt
    Jan 29, 2012 at 14:37
  • 1
    Add a space between the closing curly bracket and the \;
    – poussma
    Dec 16, 2014 at 8:36
  • xargs is faster
    – milahu
    Aug 14, 2023 at 14:16
5

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 .
1
  • short: chmod -R +X .
    – milahu
    Aug 14, 2023 at 14:20
2

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
3
  • 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, 2012 at 15:22
  • 3
    777 is dangerous and only applicable to a very small minority of systems.
    – sarnold
    Jan 30, 2012 at 0:31
  • 3
    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, 2012 at 0:32
0

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .