Solved

Linux: delete all files NOT being accessed

Posted on 2011-09-08
7
272 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
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 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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

911 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

22 Experts available now in Live!

Get 1:1 Help Now