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 ...
- 03-17-2009 #1
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.
But I get the error "Arguments list too long".Code:rm -rf /mnt/data/home/*/cache/*
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.
I know this is incorrect. A little guidance would be much appreciated! MikeCode:function read-and-remove () { while read var1; do rm -rf /mnt/data/home/$var1/cache/* done }
- 03-17-2009 #2
Would this work for you:
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.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
- 03-17-2009 #3
Very close... just a little too good
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....
Is that correct? I am guessing I need something more to read the lines from my text file? Thanks so much!Code:file=users.txt for i in $file; do echo "Deleting /mnt/data/home/$i/cache/*" rm -rf /mnt/data/home/$i/cache/* done
- 03-18-2009 #4
If your users are in a file called users_txt, then:
This assumes users_txt is in the same directory as the script. If it's in another, replace ./users_txt with the full path.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
- 03-18-2009 #5
Fantastic
This is perfect. Thanks so much. I would like to pick your brain a little bit more though.
For the line:
Does the letter i hold some special meaning or is it just another variable? Anytime you use the expressionCode:for i in $users; do
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?Code:for x in $y
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
- 03-18-2009 #6
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:
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.Code:for i in RED WHITE BLUE; do echo "My favorite color is $i" done
More info here.
- 03-18-2009 #7


Reply With Quote
