Solved

Linux: delete all files NOT being accessed

Posted on 2011-09-08
7
276 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Zimbra on Amazon Linux help 7 161
LogmeIn using Linux Ubuntu 16.04 6 189
Redhat upgrade 1 69
regular expression help for sed command 5 64
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

710 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