PDA

View Full Version : BASH - putting results of "locate" to an array



sonicb00m
February 8th, 2012, 03:22 PM
I would like to use the locate command to find a bunch of files and then iterate through the results to remove them.

Initially I used the find command but it's too slow



find / -name "fglrx" -exec rm -rf {} \;


So my idea was to place the results of locate into an array and then iterate the array and remove the found file.

Can anyone show me how to do this or even give me a more optimal solution?
Thanks

sudodus
February 8th, 2012, 03:31 PM
This is a dangerous command! At least you should run it with echo inserted at first to make sure it does not delete too much.

find / -name "fglrx" -exec echo rm -rf {} \;

sisco311
February 8th, 2012, 03:32 PM
There is no need for an array. You can pipe the output of locate to xargs:



# create a local database
updatedb -l 0 -o ~/my-db -U /path/to/dir
# delete files matched by PATTERN
locate --null -d ~/my-db PATTERN | xargs --null <the remove command here :)>

sonicb00m
February 9th, 2012, 12:17 PM
Thanks a lot!