106

I am trying to change the ownership and permissions of some files (and directories) in the current directory. I tried this:

chown username:groupname .

...expecting that it would affect all the files in the current directory, but instead only affected the directory that I am in (which is the opposite of what I want to do). I want to change it on all the files without affecting the current directory that I am in.

How can I chown and chmod all files in current directory?

Andrew
  • 15,494

5 Answers5

160

You want to use chown username:groupname *, and let the shell expand the * to the contents of the current directory. This will change permissions for all files/folders in the current directory, but not the contents of the folders.

You could also do chown -R username:groupname ., which would change the permissions on the current directory, and then recurse down inside of it and all subfolders to change the permissions.

chown -R username:groupname * will change the permissions on all the files and folders recursively, while leaving the current directory itself alone. This style and the first style are what I find myself using most often.

Darth Android
  • 38,658
16

I think you want this:

chown username:groupname *

If you also want to recursively change subdirectories, you'll need the -R (-r is deprecated) switch:

chown -R username:groupname *

djf
  • 406
1

chown is great if you are a superuser. I had an issue where someone else had run make in my directory, and now owned some files that I could not modify. Here is my workaround which handles files and directories, although it leaves the directories lying around with suffix .mkmeowner if it can't delete them.

  • The following script changes ownership of files and directories passed to it to be owned by the current user, attempting to work around permission issues by making a new copy of every directory or file not owned by the current user, deleting (or trying to delete) the original file, and renaming appropriately.
  • The intent is for it to be an abbreviation for "make me owner". I don't use underscores because they are a pain to type.

Examples:

% mkmeowner .

% mkmeowner dirpath1 dirpath2
  • It requires the following script mkmeownerone to be in your path.

mkmeowner:

#!/bin/bash
[ "x$1" == "x-h" ] || [ "x$1" == "x--help" ] && cat << END && exit 0
Usage: $0 dirorfile [direorfile2 ...]:
    change ownership of directories or files to current user.
    Current user must have permissions to read those and write to owner directory.
END
mkmeownerone=`which mkmeownerone`
for d in $*; do
    find "$d" -not -user `whoami` -exec $mkmeownerone {} \;
done

mkmeownerone:

#!/bin/bash
# change ownership of one file or directory
f="$1"
expr match "${f}" '.*\.mkmeowner$' > /dev/null && exit 1 # already tried to do this one
if mv -f "$f" "${f}.mkmeowner"; then
    cp -pr "${f}.mkmeowner" "$f" && rm -rf "${f}.mkmeowner"
    exit 0
fi
exit 1
0

I needed to change the owner of the current directory too. None of the above chown . and chown * worked. I had to pass in the full directory path:

$ chown -R nobody: "$PWD"

In the above nobody is the user I need. Group is omitted because it's optional when it equals the user.

Alex
  • 221
0

Another way to change username:groupname of all files in the current directory is to work with a subshell:

chown username:groupname $(find . -name "[a-zA-Z0-9]*")

The bash built-in command find is used with a regular-expression [a-zA-Z0-9]* in a subshell $() which can form the basis of more complex administrative tasks.

Destroy666
  • 12,350