Find the answer to your Linux question:
Results 1 to 7 of 7
I am trying to delete contents of any folder that happens to reside in a certain path. For example: My server has a whole list of users and they all ...
  1. #1
    Just Joined! mpmackenna's Avatar
    Join Date
    Nov 2008
    Location
    Bettendorf, IA USA
    Posts
    10

    Help with xargs

    I am trying to delete contents of any folder that happens to reside in a certain path.
    For example:
    My server has a whole list of users and they all have a cache folder. I want to delete the contents of all of their cache folders. I imagine I could do it with xargs but I am having trouble doing so. I tried to do it with a single command as such.
    Code:
    rm -rf /mnt/data/home/*/cache/*
    But I get the error "Arguments list too long".
    I have a text file that lists all the home folder names so I figured I could use xargs to read the list and then store each name as a variable as it reads the list and execute the rm command. Here is how it looks in my head, but it doesn't work.
    Code:
    function read-and-remove ()
    { while read var1; do
          rm -rf /mnt/data/home/$var1/cache/*
        done
    }
    I know this is incorrect. A little guidance would be much appreciated! Mike

  2. #2
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Would this work for you:
    Code:
    #!/bin/sh
    users=`awk -F : '{print $1}' /etc/passwd`
    for i in $users; do
            echo "Deleting /mnt/data/home/$i/cache/*"
            rm -rf /mnt/data/home/$i/cache/*
    done
    This will get the usernames of all of the users in /etc/passwd (where login information is kept) and delete the folder you specified for them.

  3. #3
    Just Joined! mpmackenna's Avatar
    Join Date
    Nov 2008
    Location
    Bettendorf, IA USA
    Posts
    10

    Very close... just a little too good

    Quote Originally Posted by Thrillhouse View Post
    Would this work for you:
    Code:
    #!/bin/sh
    users=`awk -F : '{print $1}' /etc/passwd`
    for i in $users; do
            echo "Deleting /mnt/data/home/$i/cache/*"
            rm -rf /mnt/data/home/$i/cache/*
    done
    This will get the usernames of all of the users in /etc/passwd (where login information is kept) and delete the folder you specified for them.
    Wow that is great! That is actually a little better than what I had in mind. Unfortunately I have a few users with whom I don't want to delete cache for so I would prefer to get the names from an existing text file rather than from the awk command. How would I edit your script so that variable i is set to each individual line in a text file?
    I imagine something like....
    Code:
    file=users.txt
    for i in $file; do
       echo "Deleting /mnt/data/home/$i/cache/*"
       rm -rf /mnt/data/home/$i/cache/*
    done
    Is that correct? I am guessing I need something more to read the lines from my text file? Thanks so much!

  4. #4
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    If your users are in a file called users_txt, then:
    Code:
    #!/bin/sh
    users=`. ./users_txt`
    for i in $users; do
            echo "Deleting /mnt/data/home/$i/cache/*"
            rm -rf /mnt/data/home/$i/cache/*
    done
    This assumes users_txt is in the same directory as the script. If it's in another, replace ./users_txt with the full path.

  5. #5
    Just Joined! mpmackenna's Avatar
    Join Date
    Nov 2008
    Location
    Bettendorf, IA USA
    Posts
    10

    Fantastic

    Quote Originally Posted by Thrillhouse View Post
    If your users are in a file called users_txt, then:
    Code:
    #!/bin/sh
    users=`. ./users_txt`
    for i in $users; do
            echo "Deleting /mnt/data/home/$i/cache/*"
            rm -rf /mnt/data/home/$i/cache/*
    done
    This assumes users_txt is in the same directory as the script. If it's in another, replace ./users_txt with the full path.
    This is perfect. Thanks so much. I would like to pick your brain a little bit more though.
    For the line:
    Code:
    for i in $users; do
    Does the letter i hold some special meaning or is it just another variable? Anytime you use the expression
    Code:
    for x in $y
    where y is a variable that is = to a text file does it just assume you want to read each line one at a time automatically? Is that just a feature of bash? If they were comma delimited values would the expression be different?
    Thanks so much for all of the help! If it is easier to just point me to a link that gives an explanation on this type of function I am good with that as well. I Googled a bit to try and find a concise explanation of this statement, but I guess my searches are not worded well because the results I got weren't so great.

    Thanks again! Mike

  6. #6
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    No, there's no significance to the use of 'i'; it's just a common loop control variable name. The delimitation is controlled by the $IFS variable or internal field separator. The default value for IFS is any whitespace (newline in your case but it could be space or tab). So you could do:
    Code:
    for i in RED WHITE BLUE; do
        echo "My favorite color is $i"
    done
    But you can change $IFS if you want the delimitation to be different. Like if you wanted to parse out the contents of a csv vile, you would change it to comma and it would treat everything separated by the comma as an individual token, not the whole line.

    More info here.

  7. #7
    Just Joined! mpmackenna's Avatar
    Join Date
    Nov 2008
    Location
    Bettendorf, IA USA
    Posts
    10

    Sweet

    Quote Originally Posted by Thrillhouse View Post
    No, there's no significance to the use of 'i'; it's just a common loop control variable name. The delimitation is controlled by the $IFS variable or internal field separator. The default value for IFS is any whitespace (newline in your case but it could be space or tab). So you could do:
    Code:
    for i in RED WHITE BLUE; do
        echo "My favorite color is $i"
    done
    But you can change $IFS if you want the delimitation to be different. Like if you wanted to parse out the contents of a csv vile, you would change it to comma and it would treat everything separated by the comma as an individual token, not the whole line.

    More info here.
    Great information, thanks again. I really appreciate you taking the time to help me out with all of that. Mike

    PS... Love your avatar too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...