# dealing with multiple files recursively



## hirohitosan (Jun 3, 2010)

Hi there. Hope here is the right place to ask that.
By mistake I formated an HDD and I used an recovery software for recovering my data.
At the end I got a HUUUGE numbers of directories (2800) named like "recup_dir.2518" containinh a HUUGE numbers of files with no significations (like f620529941.txt). What I want is to:
1. delete all .txt files (diving in all directories) from all recup_dir.1000 to recup_dir.2800 directories
2. move all files with same extension in a new directory, like "move all avi files in avi directory" same by diving in all directories

Sorry if it's a simple question but for me it worth a lot

thanks


----------



## SirDice (Jun 3, 2010)

Use the find(1) command.

Something like:

```
find /some/dir -type f -name '*.txt' -exec rm -rf {} \;
```
Or the other one

```
find /some/dir -type f -name '*.avi' -exec mv {} /save/avi/here/ \;
```


----------



## jb_fvwm2 (Jun 3, 2010)

You may get an error with "argument list too long" or
something with those commands, in which case you may
search the forum or the freebsd-questions list for
a pipe to xargs, which can handle the larger list than
maybe find can 

```
find... | xargs...
```


----------



## jalla (Jun 4, 2010)

No, find with -exec forks the command argument for each file found.
Piping through xargs is much faster cause it invokes the cmd only once, but the args list is limited to what can be handled by that command (rm or mv), not xargs.

Edit
This got me thinking "why isn't xargs smart enough to chop up the arglist if necessary?"
Reading the manpage, it turns out it is  (-L or -n options seems to do the trick)


----------



## hirohitosan (Jun 4, 2010)

thanks guys. It works fine. 
Now there are a lot of empty directories. It is possible with command line to delete empty directories?
thanks


----------



## Carpetsmoker (Jun 4, 2010)

You can use the find(1) command mentioned above in comabination with rmdir(1). rmdir will only delete empty directories.

As an exercise I'll leave the exact command up to you


----------

