LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2016, 07:08 AM   #1
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Rep: Reputation: Disabled
How do I delete a file that is x minutes old


Hello, I am using a hosting site and I am told that I need to create a cron job to automatically delete the file I want to, so what is the Linux command to force remove all .TS files in my directory that are older than 10 minutes? Thank you
 
Old 08-12-2016, 07:18 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i would touch a file and datestamp it to the time you want. then use find to find files that are not (!) newer than the touched file.
 
Old 08-12-2016, 07:25 AM   #3
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
OK, can you please provide me the commands then I would use, thx

1 - command to touch 20 .ts files all at once
2 - command to delete all .ts files older that 15 minutes

So I can't just use a rm command to do all what I want at once?
 
Old 08-12-2016, 07:26 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by schneidz View Post
i would touch a file and datestamp it to the time you want. then use find to find files that are not (!) newer than the touched file.
Not sure you'd even have to touch a file and do the compare.

Just run find and use -amin +10 to only list files accessed 10 or more minutes ago.

Code:
man find
is your friend
 
Old 08-12-2016, 07:30 AM   #5
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
basically what the hosting site told me to do is in my control panel is setup the cron job to run every 15 min and it has a line to put the Linux command that I want it to run. do I have to setup multiple tasks then instead of one command?
 
Old 08-12-2016, 07:58 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You can do the deletion be using the -delete parameter.

Last edited by TenTenths; 08-12-2016 at 08:00 AM.
 
Old 08-12-2016, 08:04 AM   #7
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
ok, so is this the command I will use;

rm *.tc -amin +10 -delete
 
Old 08-12-2016, 08:10 AM   #8
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Just to point out that your plan may result in files being as old as 25 minutes old before being deleted. For example, if you run the cron at point A, you will delete any files older than 10 minutes old. That may leave files that are just under 10 minutes old. By the time you get to your next cron run, those files will be just under 25 minutes old. As long as you're fine with that, all is ok.
 
Old 08-12-2016, 08:11 AM   #9
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
No, that's the rm (remove) command, you need the find command to find files.

Take a look at man find and work out what the line below will do, that'll get you closer to what you want to achieve.

Code:
find /path/to/the/folder -name "*.tc" -type f -amin +10 -delete
 
Old 08-12-2016, 08:16 AM   #10
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
Yeah, that's fine, I just don't want the files to build up for days

So the command is correct? since it is on an FTP server, what is the syntax to specify the directory name that the files are in?

EG: I'm going to assume my home directory is /public so where do I add another directory?

rm /directory *.tc -amin +10 -delete
 
Old 08-12-2016, 08:18 AM   #11
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
Oh, ok so this is what I use;

find /directory -name "*.tc" -type f -amin +10 -delete
 
Old 08-12-2016, 08:20 AM   #12
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You're still not getting it.

The rm command does not support the -amin parameter.

You need the find command and the -delete parameter.

Go read post #9 again and then then go read the man page for find.

As for multiple directories, if you've only a couple of them then do separate cron entries for each directory.
 
Old 08-12-2016, 08:25 AM   #13
matrixebiz
Member
 
Registered: Jun 2011
Posts: 48

Original Poster
Rep: Reputation: Disabled
Sorry, should of delete my other post, I meant;

find /directory -name "*.tc" -type f -amin +10 -delete

What the heck is a man page
 
Old 08-12-2016, 08:30 AM   #14
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by matrixebiz View Post
What the heck is a man page
I don't know if you're being serious or sarcastic, so I'll go with serious.

man pages are the manual pages for commands.

You can access the man page by typing: man <command> at the linux command prompt.

If you don't have command line access to a system then using Google and searching for man <command> will give you a list of resource links. My preference is for links to linux.die.net So in this case http://linux.die.net/man/1/find is a good starting point.
 
Old 08-12-2016, 08:32 AM   #15
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by matrixebiz View Post
Sorry, should of delete my other post, I meant;

find /directory -name "*.tc" -type f -amin +10 -delete

What the heck is a man page
If you're kidding, it's the male equivalent of a woman page.

If you're not, the man command shows you the manual page(s) for software and commands available on your computer. So, for example, man find will tell you more about the find command.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple shell script that will delete every 5 minutes all files with extension log Alexis2156 Programming 3 11-06-2014 02:10 PM
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
Find out if a file was modified in the last 2 minutes.... cricos Programming 5 04-06-2005 02:57 PM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 02:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration