Command to delete half of files in directory.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to delete half of files in directory.
# 1  
Old 03-21-2014
Command to delete half of files in directory.

Hello Friends,

I have directory called /tmp. which stores the log files.
Whenever it becomes full, i want to delete half of files from all log files.
even after deleting the files, if space is more than 90% then it should delete rest of half files.
While deleting files, older files should be deleted first.
Thanks in advance Smilie
# 2  
Old 03-21-2014
And where is the issue?

We see no attempts from you to write the script...
# 3  
Old 03-21-2014
I am unable to find the logic to divide the files into half part. and delete the older files first. As i am not sure how much files are there in tmp directory, I am bit confused.
# 4  
Old 03-21-2014

Why don't you try using find command to delete files of particular days/weeks old .. like all files older then 7 ...

then use unix "du / df " commands to checks if its usage = 90% .. if so delete all files else no nothing Smilie

If you provide a sample directory/number of files .. we would help you better Smilie
# 5  
Old 03-21-2014
Hello Nakul,

Here is the hint for you.

1- you can check the number of files by ls command with using wc command or by awk command.

2- you can then look for the man page for find command which have all the severa; options to search files older than specific number of days.

3- You can use exec command to delete those files within find command itself.

Could you please try the steps and let us know where you have stuck.

R. Singh
# 6  
Old 03-21-2014
Thanks for reply.
Yes i can find out count of files by using
ls -lrt |wc -l

suppose it is 120
How can i delete the first 60 files then.?
File name is like

Will that format is helpfull to develop the logic.?

Experts Please help
# 7  
Old 03-21-2014
find /your_directory -mtime +7 -exec rm -f {} \;

should help you to delete files.. you can simply use "cron" to execute the above command on particular day.

Plzzz How does the number of files matter in deletion SmilieSmilie??

i suggest going by age of file would help you more ..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

3. Shell Programming and Scripting

Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt" #/bin/sh echo "Enter directory" read gh for file in `ls $gh` do # to get the size of file a=$( ls -l file | awk ' {print $7} '); echo $a if then echo "removing file " rm file fi done (6 Replies)
Discussion started by: adirajup
6 Replies

4. UNIX for Dummies Questions & Answers

Cannot delete files and directory

hello i am trying to delete some files and also some directories. However, despite having the required permissions (i m the owner), Permission is being denied. I also tried to delete using find and inode number, but again Permission was denied. :wall: I am new to unix so please dumb down... (8 Replies)
Discussion started by: curiosity
8 Replies

5. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

6. Homework & Coursework Questions

Single command to delete a directory

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a command that will delete the john directory, and all of its contents. The problem I can't firgure out is how to create a absoute path to... (5 Replies)
Discussion started by: jorogon0099
5 Replies

7. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies

8. Shell Programming and Scripting

Delete Some Old files from Particular Directory

Hi Team, I am new to scripting. I want to create a script, which needs to keep only 5 days directories and want to remove the old directory from a particular directory. Can Somebody help me with starting this script. All my directories will be created in the name <YYYYMMDD>. Thanks... (2 Replies)
Discussion started by: siva80_cit
2 Replies

9. Linux

command in linux to delete a directory

hi all, can anybody tell me command(in linux) to delete a directory which have other directories & files in it. its urgent thanks vikas (2 Replies)
Discussion started by: guptavikas1
2 Replies
Login or Register to Ask a Question