Searching and Deleting Files in Termianl Mac or Linux



Ever Wondered how can you delete files from you system using shorter time than searching for it in every folder or using a GUI search app that will take even longer time!. Using Terminal might be useless in most of the daily tasks, but some tasks are butter done using terminal and this is one of them.

Explanation

(FORMULA)

[command] [where-to-look] [match-pattern] [EXTRA(-print=print-file-location, -delete=deltefile)]

(EXAMPLE)

find /home/user/ -name *.doc -delete -print



Action

1- Searching for the files you want to delete:
First we need to locate the files that we want to delete and validate that it is exactly what we want to delete, by default the system will print the matched files and if it didn't you can print it using (-print).

Command: find /home/user/ -name *.doc

2- Deleting the Founded Files:
Now that you have founded the files, all you need to do is to Delete after double checking that founded results. all you need to do is to add this attribute (-print) to delete the files.

Command: find /home/user/ -name *.doc -delete

Done! the files are now deleted, but the command didn't print the deleted files on screen, bu default it doesn't and if you want to show it you will need to add the attribute (-print), the command should look like this:

Command: find /home/user/ -name *.doc -delete -print

Comments