How to delete files in UNIX using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete files in UNIX using shell script
# 1  
Old 02-02-2006
Question 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.

Please help experts! Thanks in advance!
# 2  
Old 02-02-2006
This deletes files in a directory tree that are less than 5 days old, and asks if it is okay to delete each file:

Code:
find /path/to/directory -mtime -5  -ok  rm -f {} \;

You need to do a search on the FAQ here on the ofrums for date arithmetic, so your script can calculcate the number of days to put in the above command, or a coomand like it.
# 3  
Old 02-03-2006
Thanks for the reply.

Still there is one issue left. How can we determine the number of files it has deleted after we run the script ?
# 4  
Old 02-03-2006
Piping the previous command into wc

find /path/to/directory -mtime +5 -ok rm -f {} \; | wc -l

will give you a line count.

If you're confident in what you're deleting, and you VERIFY that the directory provided isn't a system directory, you can streamline the command without a prompt. Also, add one more exprssion to make certain you're deleting only FILES:

find /path/to/directory -mtime +5 -type f -print -exec rm -f {} \; 1>/tmp/out
wc -l /tmp/out

The print command displays the file being deleted. I'd recommend saving the output for review. You can then do a line count on the output file.
# 5  
Old 02-03-2006
else have an illustration as follows,

Code:
delcnt=0
for files in `find /path/to/directory -mtime -5 -print`
do
   echo "Deleting file $file"
   /bin/rm $file
   delcnt=$(($delcnt + 1))
done

echo "deleted $delcnt files"

# 6  
Old 03-24-2006
Question How to delete files in UNIX using shell script

Hi,

find /path/to/directory -mtime -5 -ok rm -f {} \

is working fine. But there is one more concern. This command deletes all the files in the specified directory along with files in sub-directories also.

The requirement is like, it should delete files that are 5 Days old only in the directory specified and not the subdirectories within it.

Any idea on how to do that?

Thanks in advance
# 7  
Old 04-09-2008
Adding -maxdepth 1 would do the trick, this way the find command only searches 1 level deep in the directory structure.

The complete command looks like this :

find /path/to/directory -mtime -5 -maxdepth 1 -ok rm -f {} \

Edit: I noticed this is an old thread, but I wanted to add a solution for completeness and to help people who would stumble upon this thread in the future.

Last edited by ruleant; 04-09-2008 at 05:50 AM.. Reason: reaction to later post
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. Shell Programming and Scripting

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 MPNT_... (6 Replies)
Discussion started by: Hadoop_Master
6 Replies

5. Shell Programming and Scripting

Unix shell script to delete files on windows server

Hi experts, can anyone suggest me on the below: how to write a shell script to search and delete files on windows server. -script runs on unix box -it should search for specific files on windows server and delete them periodically. (2 Replies)
Discussion started by: chpradeepch
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

delete old files thru unix script

I have to delete files older than 60 days from a windows directory. So I tried to include this script FTP_LOG=${DATA_TOP}/data_tmp/logfile FTP_CMDS=${DATA_TOP}/data_tmp/cmdfile echo "open ftp1" > ${FTP_CMDS} echo "user anonymous local" >> ${FTP_CMDS} echo "cd ${SRC_DIR}" >> ${FTP_CMDS} echo... (3 Replies)
Discussion started by: snair001
3 Replies

8. Shell Programming and Scripting

Delete files older than 2 days using shell script in Unix

I'm new to shell script.... can any one help... What is the shell script to delete the files older than 2 days ? (3 Replies)
Discussion started by: satishpabba
3 Replies

9. 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

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