Solved

Linux: delete all files NOT being accessed

Posted on 2011-09-08
7
275 Views
Last Modified: 2013-12-16
I want to delete all files here that are NOT currently being accessed or downloaded:
/home/userxyz/public_html/downloads/

If a file is in that directory and is NOT being downloaded or accessed, I want it to be deleted.
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36504381
as "root", type
find  /home/userxyz/public_html/downloads/   -type f   -atime +7
  to see how many files are not accessed within past 7 days.

If you are sure those files can be delete, then

find  /home/userxyz/public_html/downloads/   -type f   -atime +7  -exec /bin/rm -f {}\;
   to remove them

you can change  to "-atime +3" for not accessed for past 3 days.
0
 
LVL 16

Author Comment

by:hankknight
ID: 36504784
Is there a way to do this for files that are not CURRENTLY being accessed?

If we must set a time then files not accessed for 10 minutes.
0
 
LVL 38

Accepted Solution

by:
wesly_chen earned 125 total points
ID: 36504813
> not accessed for 10 minutes.
-amin +10

find  /home/userxyz/public_html/downloads/   -type f   -amin +10  -exec /bin/rm -f {}\;
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 125 total points
ID: 36505035
>> Is there a way to do this for files that are not CURRENTLY being accessed?

You'd have to be using lsof if you want to know for sure. But then you would have to compare a directory listing of ./downloads with lsof output and determine what can be deleted. This is a more difficult approach than using find with -amin +10 (access for 10 minutes).
0
 
LVL 40

Expert Comment

by:noci
ID: 36507150
be shut that the disks are mounted with the atime option though. (noatime is used a lot to speed up diskaccess by preventing updates of the atime stamp ....)
0
 
LVL 40

Assisted Solution

by:noci
noci earned 125 total points
ID: 36507152
be sure in stead of be shut... Sorry.
0
 
LVL 8

Assisted Solution

by:eager
eager earned 125 total points
ID: 36516510
Open files are not deleted until the last user closes the file, although the directory entry is removed immediately.  This may mean that you can delete all files in the directory and any that are currently being accessed will continue to be read. There may be interactions with a web server or other program which causes the file to be re-opened.

You can test this easily by creating a large file which takes a while to download or copy.  While this is being accessed, delete the file.  The transfer should complete without error.  
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
awk sed 8 103
How to use question mark (?) in filename with html 25 97
SMB share across internet 15 80
Linux kernel panic ext3-fs error 14 24
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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