Hi I am using Fedora 10. I am using as root. I have 3 users and there is a folder named "data" in the home directory of 2 users out of 3. I want to delete those folders.

What is the command to delete a folder from home directories of all users.

Thanks Sunil Kumar Sahoo

5 Answers 5


To be fully pedantic you should probably use getent instead of looking at /etc/passwd, just in case you have users form other sources such as NIS, SQL, LDAP, etc. To get the list of homedirectories you could use this:

getent passwd | cut -d: -f6

And to delete all data directories for all users:

for home in $(getent passwd | cut -d: -f6) ; do
    if [ -d "$home/data" ]; then
        rm -rf "$home/data"

The extra if statement is there to make sure you do not accidentally delete files.


assuming the home directories are in /home, you would do this:

for user in 'user1' 'user2'
rm -rf /home/$user/data

First, check what is going to be deleted (as suggested by David, I should have mention that, I'd never run the next command blindly):

ls -al /home/*/data 

And, if nothing is wrong, perform the delete:

rm -rf /home/*/data
  • yes, he wants to delete from only 2 out of the 3 users Nov 18, 2009 at 12:46
  • Are you sure? I'd like the OP to answer. Nov 18, 2009 at 12:50
  • Sounds to me like only 2 of the 3 users have the folder. I think this should work just fine, though I'd want to be doubly sure before I ran it.
    – tvanfosson
    Nov 18, 2009 at 12:59
  • And I'd do ls -l /home/*/data first to see exactly what I was going to delete. Nov 18, 2009 at 15:21

To simply delete the folder in several user accounts in one step, you can use

# rm -rf /path/to/home/(user1|user2|user3|...)/data

Note that this only deletes the folder and not the user. For every user, You'll need a quick Awk command such as

$ grep "/home" /etc/passwd | awk -F":" '{ print $1 }'

To get a listing. The problem with the awk command is that it doesn't know the difference between a system user that doesn't have a home directory and one that does.

  • Except that parentheses and pipes don't work in this context unless you're using zsh. For bash and ksh, use: {user1,user2,user3,...} Nov 18, 2009 at 14:41
# cd /home/
# find . -maxdepth 2 -type d -iname data -exec rm -rf {} \;

The find command is useful in this context because it is automatically recursive, won't accidentally delete a file by the same name and will scale better than most solutions to this type of problem (with the exception of the old "too many arguments" issue bumped into by experienced sysadmins - see xargs for that).

A quick rundown of the meaning of this specific find command:

  • maxdepth: do not descend beyond one directory below that given on the command line (.);
  • type: only find directories
  • iname: case-insensitive match on the name against the string "data"
  • exec: command to execute against matching objects found - in this case, rm -rf
  • {}: placeholder for each matching object found
  • \;: the semi-colon used to end each command is escaped to stop shell expansion

Hope this helps!

  • You could stick a -mindepth in there, too, in case Brent Spiner is a user of your system. Nov 18, 2009 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy