rm command - Command line - Terminal window

Hello Apple users,


I have a doubt about rm command, as follows:


How can I delete all the same format files (e.g., *.txt) in folders and subfolders recursively in a pendrive?


Example: From the root directory, the OS should search deeply and recursively through folders and subfolders all .txt and delete them.


Is it correct the following syntax?


$ cd /Volumes/DATA

$ rm -r -f *.txt

$


Thank you in advance.

MacBook Pro 15”, macOS 10.15

Posted on Feb 6, 2020 5:10 AM

Reply

Similar questions

5 replies

Feb 6, 2020 12:22 PM in response to VikingOSX

Thank you, VikingOSX! It is solved.


However, I couldn't understand the syntax {} \ in the end of above command. I tried to get more information about it on the man -t find | open -f -a Preview, but, unfortunately, I couldn't find out.


How could I learn more about those commands for OSX? In general, learning through the man <command> is a little bit hard. Would you please suggest any tutorial or book?


Thank you in advance.

Feb 6, 2020 1:45 PM in response to ReinaldoGD

The UNIX find command is recursive, and starting with the provided path, it will traverse all folders and files in the specified directory tree. When the find command matches a *.txt file, the full path to that file is passed into the {} so that the rm -f command has an explicit file to remove. This occurs until all returned matches are removed. The \; syntax terminates the -exec statement because one has to escape '\' a semi-colon from the shell itself.


I have been doing UNIX command line for 39 years and have lost touch with any current books on the subject. They are few and far between for macOS. You might have better luck over at unix.stackexchange.com, or stackoverflow.com. Enter the URL of either, and then a space followed by your short usage question in a browser:


unix.stackexchange.com find remove all text files from directory tree


You will get several focused hits on how to do this with find. Many of these hits may be using Linux, and not macOS, and you just have to be aware that some commands may seem to do the trick online, but when implemented in macOS, they may not work, or produce different results because of operating system, or command differences differences.


What you should learn is the Bash 3.5.2 syntax, because Apple has frozen on that version, or the more advanced Zsh shell, either of which are the shell from which you will use UNIX commands. Most of the Bash references found online are from Linux and Bash 4.n or 5.0 — which will not be suitable for macOS.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

rm command - Command line - Terminal window

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.