Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to recursively step through folders and delete files

Tags:

linux

bash

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?

like image 660
dnatoli Avatar asked Sep 01 '10 01:09

dnatoli


People also ask

How do I delete a folder with all files inside it recursively?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I delete a recursive file in bash?

How do I delete folder recursively under Linux operating systems using a bash command line options? You need to use the rm command to remove files or directories (also known as folders) recursively. The rmdir command removes only empty directories. So you need to use rm command to delete folder recursively under Linux.

How do I loop over a folder?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.

How do I loop a directory in bash?

The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).


2 Answers

Change directory to the root directory you want (or change . to the directory) and execute:

find . -name "._*" -print0 | xargs -0 rm -rf

xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.

like image 159
Brandon Horsley Avatar answered Oct 20 '22 15:10

Brandon Horsley


find . -name '._*' -exec rm -Rf {} \;
like image 36
Vadim Shender Avatar answered Oct 20 '22 16:10

Vadim Shender