Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through a directory recursively to delete files with certain extensions

Tags:

bash

I need to loop through a directory recursively and remove all files with extension .pdf and .doc. I'm managing to loop through a directory recursively but not managing to filter the files with the above mentioned file extensions.

My code so far

#/bin/sh  SEARCH_FOLDER="/tmp/*"  for f in $SEARCH_FOLDER do     if [ -d "$f" ]     then         for ff in $f/*         do                   echo "Processing $ff"         done     else         echo "Processing file $f"     fi done 

I need help to complete the code, since I'm not getting anywhere.

like image 957
Elitmiar Avatar asked Jan 09 '11 11:01

Elitmiar


People also ask

How do I loop through 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 you rm 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.

What is recursive deleting?

To delete files recursively means to delete the contents of the folder before deleting the folder itself. If the folder has other folders in it, it will do the same with those folders. Basically it means delete whatever is inside the folder I am deleting, so that I can delete the folder itself.

How do I recursively list a directory?

Linux recursive directory listing command Linux recursive directory listing using ls -R command. The -R option passed to the ls command to list subdirectories recursively.

How to delete all files with the considered name recursively?

Delete all files with the considered name or postfix recursively: find. -name '*.pyc' -type f -delete Delete all directories with the considered name recursively: find ~ -path '*/__pycache__/*' -delete‍‍‍ find ~ -type d -name '__pycache__' -empty -delete‍‍‍

How do I loop through all files in a folder?

'Loop through all files in a folder Dim fileName As Variant fileName = Dir("C:\Users\marks\Documents\") While fileName <> "" 'Insert the actions to be performed on each file 'This example will print the file name to the immediate window Debug.Print fileName 'Set the fileName to the next file fileName = Dir Wend.

How to delete files with find command in Linux?

In order to delete files with find, we just add -delete to the end of the find command. Or, if we don’t want to print the files while deleting them: 5.

Is it possible to recursively iterate through all directories in Ubuntu?

These defects could be fixed but the whole approach is needlessly complex so it isn't worth bothering. The following function would recursively iterate through all the directories in the \home\ubuntu directory ( whole directory structure under ubuntu ) and apply the necessary checks in else block.


1 Answers

As a followup to mouviciel's answer, you could also do this as a for loop, instead of using xargs. I often find xargs cumbersome, especially if I need to do something more complicated in each iteration.

for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm $f; done 

As a number of people have commented, this will fail if there are spaces in filenames. You can work around this by temporarily setting the IFS (internal field seperator) to the newline character. This also fails if there are wildcard characters \[?* in the file names. You can work around that by temporarily disabling wildcard expansion (globbing).

IFS=$'\n'; set -f for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm "$f"; done unset IFS; set +f 

If you have newlines in your filenames, then that won't work either. You're better off with an xargs based solution:

find /tmp \( -name '*.pdf' -or -name '*.doc' \) -print0 | xargs -0 rm 

(The escaped brackets are required here to have the -print0 apply to both or clauses.)

GNU and *BSD find also has a -delete action, which would look like this:

find /tmp \( -name '*.pdf' -or -name '*.doc' \) -delete 
like image 181
James Scriven Avatar answered Sep 22 '22 08:09

James Scriven