# cd /usr/ports && make clean



## fluca1978 (Nov 30, 2011)

Hi all,

in order to save a few disk space I ran *make clean* on the whole ports tree, to see if I missed some clean of compiled ports. However this is going a little slow, is there a smarter way to do a clean on the ports without having to specify every single port to clean?


----------



## SirDice (Nov 30, 2011)

fluca1978 said:
			
		

> is there a smarter way to do a clean on the ports without having to specify every single port to clean?


`# rm -rf /usr/ports/*/*/work`

Is usually a lot faster than:
`# make -C /usr/ports clean`


----------



## fluca1978 (Nov 30, 2011)

It is indeed!


----------



## SirDice (Nov 30, 2011)

If you're low on disk space you could set WRKDIRPREFIX to somewhere with a lot more room. The "work" directories will end up there.

I've mine set to /tmp/build/. My /tmp/ is a 10GB tmpfs(5).


----------



## kpa (Nov 30, 2011)

I've set WRKDIRPREFIX to /usr/obj and dedicated a partition for it, that way both the /usr/src builds and ports(7) use the same filesystem for temporary files.


----------



## Dru (Nov 30, 2011)

Seems like

```
make install clean
```
would be a lot simpler in the first place.

No?


----------



## wblock@ (Dec 1, 2011)

Yes, but there can be reasons to do the steps separately.  Like if you're looking for a file, or patching first.  And sometimes things fail and leave leftover work directories.  I use find(1):

```
find -X /usr/ports -name work -type d -depth 3 -prune -print -exec rm -rf {} \;
```


----------



## gkontos (Dec 1, 2011)

Dru said:
			
		

> Seems like
> 
> ```
> make install clean
> ...


Absolutely! But in some cases, especially after a major upgrade that went bad or if you follow experimental ports, you end up with leftovers. 
Now, I know that most people will disagree here. But what I usually do to clean my messy desktop ports is:

[CMD=""] # cd /usr/ports && rm -rf * && portsnap extract[/CMD]

I never do it on a server though


----------



## wblock@ (Dec 1, 2011)

gkontos said:
			
		

> Absolutely! But in some cases, especially after a major upgrade that went bad or if you follow experimental ports, you end up with leftovers.
> Now, I know that most people will disagree here. But what I usually do to clean my messy desktop ports is:
> 
> [CMD=""] # cd /usr/ports && rm -rf * && portsnap extract[/CMD]
> ...



Careful, that also gets rid of all the distfiles that have been downloaded.


----------



## jotawski (Dec 1, 2011)

Hi,


```
# touch Clean
# echo #\!/bin/sh > Clean
# echo set -x >> Clean
# echo >> Clean
# chmod +x Clean
# find /usr/ports -name work -type d | \
  sed -e 's,/usr,cd /usr,g'  \
      -e 's,/work, \&\& make clean,g' >> Clean
# ./Clean
```

replace clean, in sed ,with package if you would like to keep built packages.
very stupid but it works.


----------



## fluca1978 (Dec 1, 2011)

gkontos said:
			
		

> Absolutely! But in some cases, especially after a major upgrade that went bad or if you follow experimental ports, you end up with leftovers.



Exactly my problem. And since I'm running on a desktop, I don't have another disk with enough room to set the working directory for building ports.
Thanks everyone for suggestions.


----------



## wblock@ (Dec 1, 2011)

Another way to save disk space is to delete obsolete distfiles:
`# portmaster -t -y --clean-distfiles`


----------



## phoenix (Dec 1, 2011)

wblock@ said:
			
		

> Careful, that also gets rid of all the distfiles that have been downloaded.



And it also has the potential to backfire and destroy the entire system if you mistype command or use & accidentally instead of &&.  You should never use *rm -rf* and *** together.    Much better to write the command like `# rm -rf /usr/ports/*` or even to combine the two into `# cd /usr/ports/ && rm -rf /usr/ports/*`

Or, if you really want to make things fast, put /usr/ports onto a separate filesystem, and just newfs(1) it to clear it.


----------



## gkontos (Dec 1, 2011)

@phoenix

You bring back memories :e


----------



## fluca1978 (Dec 2, 2011)

phoenix said:
			
		

> And it also has the potential to backfire and destroy the entire system if you mistype command or use & accidentally instead of &&.  You should never use *rm -rf* and *** together.    Much better to write the command like `# rm -rf /usr/ports/*` or even to combine the two into `# cd /usr/ports/ && rm -rf /usr/ports/*`
> 
> Or, if you really want to make things fast, put /usr/ports onto a separate filesystem, and just newfs(1) it to clear it.



Of course what you are saying is true: there is a risk of destroying all the system with a rm. I think however this is always possible when using a rm so that you have to be really careful before hitting <enter>. And combining the two commands as `# cd /usr/ports/ && rm -rf /usr/ports/*` does not solves the problem of a mispelled & (moreover the first command is useless). Finally having the ports on a separate filesystem resilvered each time sounds like a bit too much to me.


----------



## SirDice (Dec 2, 2011)

phoenix said:
			
		

> And it also has the potential to backfire and destroy the entire system if you mistype command or use & accidentally instead of &&.  You should never use *rm -rf* and *** together.


New users that use (t)csh might want to do this:

[cmd=]set rmstar[/cmd]


```
dice@molly:~/test>echo $SHELL
/bin/tcsh
dice@molly:~/test>touch test1
dice@molly:~/test>touch test2
dice@molly:~/test>touch test3
dice@molly:~/test>ll
total 0
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test1
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test2
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test3
dice@molly:~/test>rm *
dice@molly:~/test>
dice@molly:~/test>ll
total 0
dice@molly:~/test>touch test1
dice@molly:~/test>touch test2
dice@molly:~/test>touch test3
dice@molly:~/test>ll
total 0
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test1
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test2
-rw-r--r--  1 dice  dice  0 Dec  2 08:32 test3
dice@molly:~/test>set rmstar
dice@molly:~/test>rm *
Do you really want to delete all files? [n/y] 
dice@molly:~/test>
```


----------



## JamesElstone (Jul 22, 2013)

What is wrong with `portsclean -C`?

James.


----------



## wblock@ (Jul 22, 2013)

JamesElstone said:
			
		

> What is wrong with `portsclean -C`?
> 
> James.



Nothing, except that it's a part of ports-mgmt/portupgrade, which many people do not have installed.


----------

