Solved

Linux: delete all files NOT being accessed

Posted on 2011-09-08
7
271 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
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen 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 39

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 39

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now