Solved

Removing files in UNIX

Posted on 2009-05-11
10
334 Views
Last Modified: 2013-12-23
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.
0
Comment
Question by:-CyberIT-
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 3

Expert Comment

by:coanda
Comment Utility
find / -type f -iname 'backup*' -exec rm {} \;
0
 

Author Comment

by:-CyberIT-
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Yes.

Yes, with a little more coding.

Yes.
0
 
LVL 3

Expert Comment

by:coanda
Comment Utility
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
 

Author Comment

by:-CyberIT-
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 45 total points
Comment Utility
find /January/backup  /February/backup -type f -iname 'backup*' -exec rm {} \;
0
 
LVL 3

Accepted Solution

by:
coanda earned 30 total points
Comment Utility
CyberIT - the -r and -f commands are only necessary if you wanted to delete a directory and it's contents.
0
 

Author Comment

by:-CyberIT-
Comment Utility
oh, no ... I dont want to delete the directory... i just want to delete the files within the directories with that name scheme.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 45 total points
Comment Utility
-type f
restricts you to finding only regular files, not directories or links or sockets,
see
man find
0
 
LVL 5

Expert Comment

by:vikaskhoria
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now