Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

migrated from stackoverflow.com Nov 18 '09 at 12:51

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

5 Answers

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"
    fi
done

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

share|improve this answer
add comment

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

for user in 'user1' 'user2'
do
rm -rf /home/$user/data
done
share|improve this answer
add comment

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
share|improve this answer
    
yes, he wants to delete from only 2 out of the 3 users –  ennuikiller Nov 18 '09 at 12:46
    
Are you sure? I'd like the OP to answer. –  Pascal Thivent Nov 18 '09 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 '09 at 12:59
    
And I'd do ls -l /home/*/data first to see exactly what I was going to delete. –  David Thornley Nov 18 '09 at 15:21
add comment

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.

share|improve this answer
    
Except that parentheses and pipes don't work in this context unless you're using zsh. For bash and ksh, use: {user1,user2,user3,...} –  Dennis Williamson Nov 18 '09 at 14:41
add comment
# 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!

share|improve this answer
    
You could stick a -mindepth in there, too, in case Brent Spiner is a user of your system. –  Dennis Williamson Nov 18 '09 at 14:12
add comment

Your Answer

 
discard

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