Removing files in UNIX

My company purchased a beta program that creates backup files in the format of backup.<date>. However they are put in different locations on the drive. Id like to be able to create a script to remove all files that start with backup. without looking through each folder.
Cyber ITEngineerAsked:
Who is Participating?
 
coandaConnect With a Mentor Commented:
CyberIT - the -r and -f commands are only necessary if you wanted to delete a directory and it's contents.
0
 
coandaCommented:
find / -type f -iname 'backup*' -exec rm {} \;
0
 
Cyber ITEngineerAuthor Commented:
Now will that way search the whole drive and delete all files with the backup name?

Can I prompt it for permission the first time and then delete it automatically afterwards?

Can I limit it to where it should search?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ozoCommented:
Yes.

Yes, with a little more coding.

Yes.
0
 
coandaCommented:
to change which path is searched just replace / with whatever directory you want it to check. if you have sudo installed you can just add the sudo command to the front of the find command to run it as root, it will ask you to authenticate when you run it.
0
 
Cyber ITEngineerAuthor Commented:
OK, I have files located in each month of the year and I wanted to delete all files with the backup in each month.  I dont want to browse and delete.

find / -type f -iname 'backup*' -exec rm -i -f -r {ls.. \January\backup}
find / -type f -iname 'backup*' -exec rm -i -f -r {ls.. \February\backup}
...

Am I off track here or is there an easier way??

Thanks!
0
 
ozoConnect With a Mentor Commented:
find /January/backup  /February/backup -type f -iname 'backup*' -exec rm {} \;
0
 
Cyber ITEngineerAuthor Commented:
oh, no ... I dont want to delete the directory... i just want to delete the files within the directories with that name scheme.
0
 
ozoConnect With a Mentor Commented:
-type f
restricts you to finding only regular files, not directories or links or sockets,
see
man find
0
 
vikaskhoriaCommented:
You can use a for loop to do for each month.So use something like this:

for month in January Febuary March April May June July August September October November December
do
find / -type f -iname 'backup*' -exec rm -i -f {ls.. \\$month\\backup}
done

Open in new window

0
All Courses

From novice to tech pro — start learning today.