Shell Script to delete the protected files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to delete the protected files.
# 1  
Old 08-31-2015
Shell Script to delete the protected files.

Hello,

we have more than 100000 files in a directory which are write-protected regular file, these files are quite old and would like to delete them completely, Kindly let me know the command or peice of code to automate the process.

The filenames are like below

Code:
MPNT_ Bulk_2015.07.01-14.50.01.Bobline
MPNT_Bulk_2015.07.01-14.55.01.Bobline

i get the files every 5 mins hence the count of files in Dir is huge, Please help me to delete them.

Last edited by Don Cragun; 08-31-2015 at 05:19 AM.. Reason: Add CODE tags.
# 2  
Old 08-31-2015
Please use code tags as required by forum rules!

With that sparse a specification (i.e. NO conditions), a simple crontab entry
Code:
0 * * * * rm /path/to/directory/MPNT*.Bobline

to delete the files on the hour might suffice. Please be careful and test the command beforehand (e.g. echo rm /path...) so no serious harm is done on e.g. typographical errors.
# 3  
Old 08-31-2015
Shell Script to delete the protected files.

Thanks RudiC!!

I get a prompt before i delete the files, i need to enter y to delete the files which i dont want to do all the time, Can this be automated to make sure all the files are deleted with out manual intervention?

Code:
 
rm: remove write-protected regular file `MPNT_Bulk_2015.07.01-14.40.00.Bobline'? y
rm: remove write-protected regular file `MPNT_Bulk_2015.07.01-14.45.00.Bobline'? y

# 4  
Old 08-31-2015
Looks like rm is aliased to rm -i . man rm:
Quote:
-i prompt before every removal
To get rid of the -i , you can
- unalias rm
- run /bin/rm to deploy the unaliased command.
# 5  
Old 08-31-2015
In addition to what RudiC suggested, with write protected files you may need the rm -f option. And, with 100000 files you could run into ARG_MAX limitations. Note also that it is a bad idea to have spaces in the middle of some of your filenames, but with the code RudiC suggested, or with:
Code:
find /path/to/directory -name 'MPNT*.bobline' -exec rm -f {} +

you should be able to handle them with no problem. If you use a while read path loop, be very sure that you use double quotes when you expand "$path" or the spaces in your filenames will cause unfound pathname fragment errors or will remove files other than the ones you are trying to remove.
# 6  
Old 08-31-2015
Shell Script to delete the protected files.

Hi,

I tried the both options below which didnt help me much Smilie

The Solution that Don has suggsted

Code:
 
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]


The Solution that Rudi has suggested

[
Code:
puser@Polo44 Bobline]$ unalias rm
bash: unalias: rm: not found
[puser@Polo44 Bobline]$ which unalias
/usr/bin/which: no unalias in (/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/ACEngsc/bin:/opt/CCOsudo/bin:/opt/dataload/bin:/opt/*/sbin:/opt/ACEngsc/bin:/opt/CCOsudo/bin:/opt/dataload/bin:/opt/*/sbin)
[puser@Polo44 Bobline]$ which rm
/bin/rm
[puser@Polo44 Bobline]$ unalias /bin/rm
bash: unalias: /bin/rm: not found

My server configuration is like below

Code:
 
Linux Polo44 2.6.18-371.4.1.el5 #1 SMP Wed Jan 8 18:42:07 EST 2014 x86_64 x86_64 x86_64 GNU/Linux


Last edited by Hadoop_Master; 08-31-2015 at 06:02 AM..
# 7  
Old 08-31-2015
Quote:
Originally Posted by Hadoop_Master
Hi,

I tried the both options below which didnt help me much Smilie

The Solution that Don has suggsted

Code:
 
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]


The Solution that Rudi has suggested

[
Code:
puser@Polo44 Bobline]$ unalias rm
bash: unalias: rm: not found
[puser@Polo44 Bobline]$ which unalias
/usr/bin/which: no unalias in (/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/ACEngsc/bin:/opt/CCOsudo/bin:/opt/dataload/bin:/opt/*/sbin:/opt/ACEngsc/bin:/opt/CCOsudo/bin:/opt/dataload/bin:/opt/*/sbin)
[puser@Polo44 Bobline]$ which rm
/bin/rm
[puser@Polo44 Bobline]$ unalias /bin/rm
bash: unalias: /bin/rm: not found

My server configuration is like below

Code:
 
Linux Polo44 2.6.18-371.4.1.el5 #1 SMP Wed Jan 8 18:42:07 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

If you had used the code I suggested, you would not have gotten that error from find.

Please try again with the exact command I suggested:
Code:
find /path/to/directory -name 'MPNT*.bobline' -exec rm -f {} +

just replacing /path/to/directory with an absolute pathname of the directory that contains the thousands of files you're trying to remove.

Any version of bash should include unalias as a built-in, but you don't need it. As I said before, if you don't run into ARG_MAX limits, the command:
Code:
rm -f /path/to/directory/MPNT*.bobline

should do what you want (again changing /path/to/directory to an absolute pathname of the directory that contains the thousands of files you're trying to remove).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to delete the ip address from files

Hello, I am new to shell scripting, need help, my requirement is to delete the ip address from serveral files, please suggest (2 Replies)
Discussion started by: manoj.solaris
2 Replies

2. Shell Programming and Scripting

Shell Script to delete files from 5 different servers

Hello, I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations. Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell... (2 Replies)
Discussion started by: Teja G
2 Replies

3. Shell Programming and Scripting

Shell script to check a file and delete old files

Hello, I needed help with a shell script where in it checks if a file exists under a directory and also checks the age of the file and delete it if it is older than 3 weeks. thanks (10 Replies)
Discussion started by: hasn318
10 Replies

4. UNIX for Advanced & Expert Users

Help with a shell script? List files, delete them and log them

Hello, i'm trying to solve this script. List, one at a time, all files larger than 100K in the /home/username directory tree. Give the user the option to delete or compress the file, then proceed to show the next one. Write to a logfile the names of all deleted files and the deletion times. I... (7 Replies)
Discussion started by: jose2802
7 Replies

5. Shell Programming and Scripting

[Solved] Get files & delete them by shell script

I want to use my script to get any file then delete it once it transfers to my side , I manage to create below script to generate "list" file which contains all file names in "10.10.1.1" then I made "a.out" file which contains the commands that I want to run it on "10.10.1.1" to get & delete the... (2 Replies)
Discussion started by: arm
2 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

writting a shell script to delete damage files

hi, I have 100 files say File1.mp3, File2.mp3 .......file100.mp3 i found that File1.mp3 to File50.mp3 are damaged. I want to delete the damaged files from the directory using sed with regex how can i do this. thanks (5 Replies)
Discussion started by: pchangba
5 Replies

8. Shell Programming and Scripting

shell script for delete old files

i want to delete all the files in my directory except the latest one. i need to do this from shell script. say i have a.txt - latest file b.txt, c.txt.. it should delete all the files except a.txt? (4 Replies)
Discussion started by: krishnarao
4 Replies

9. Shell Programming and Scripting

How to delete files in UNIX using shell script

Hi, I have the following task to perform using shell script. The user will provide a directory name along with a date. The script will delete all the files in the specified directory that was created earlier to that date. Also it should display the number of files that has been deleted. ... (7 Replies)
Discussion started by: theguy16
7 Replies

10. UNIX for Advanced & Expert Users

how to delete empty files in a shell script

I'm trying to figure out a way to delete empty files in a directory. I have a cron that runs and creates a flat file every 15 mins. However, most times at night the flat file will be empty. I'd like to run a script to delete empty files that end with *.dat Any suggestions? Rich (1 Reply)
Discussion started by: rpnuge
1 Replies
Login or Register to Ask a Question

Featured Tech Videos