LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-03-2012, 12:26 PM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Syntax question for a command within a bash script


Hello --

I have been tasked with creating a script that will check a directory for files that are older than a certain age, and upon finding them, have the files in question removed from the directory.

My plan is to use the find command with the mtime option, and pipe the results to a file. The syntax that I had in mind is the following:

Code:
find -mtime +7 > /tmp/olderthansevendays.txt
The next step that I had in mind was to use the rm command to reference the text file, and remove those files listed from the directory.

My question is what would be the correct syntax to accomplish this?

Thanks.
 
Old 10-03-2012, 12:38 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You can have that much easier. Just use find's -delete option:
Code:
 find -mtime +7 -delete
For more info have a look at
Code:
man find
 
1 members found this post helpful.
Old 10-04-2012, 02:28 PM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

Thanks for your reply. I will include the find command with the delete option in the script. As a follow-up, I need to have the script do the following:

1. Change to a specific directory. The directory in question contains a series of subfolders all of which contain another folder called pdf
2. Change to each subfolder within the first directory, and then to the pdf folder within each subfolder.
3. Run the find command with the delete option on files within the pdf folder.
4. Repeat this process for all subfolders that are located within the directory mentioned in item 1.

What would the syntax be here?

Thanks.
 
Old 10-04-2012, 03:14 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
If I understand you right you have one main folder (I will call it mainf), which contains several folders, which in turn contain a folder called pdf, in which you want to delete all files older than 7 days.
I would handle that with a for loop, something like this:
Code:
for searchdir in /path/to/mainf/*/pdf
do
    find searchdir -mtime +7 -delete
done
This should work, but I have not tested it, so that is up to you.
 
1 members found this post helpful.
Old 10-09-2012, 12:53 PM   #5
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

I came up with a script, and the syntax that I am using for the find command is as follows:

Code:
find searchdir -mtime +10 -delete
The problem that I am running into, when I execute the script, is the following:

Quote:
find: invalid predicate `-delete'
find: invalid predicate `-delete'
find: invalid predicate `-delete'
find: invalid predicate `-delete'
find: invalid predicate `-delete'
The version of find as well as the operating system system in question are as follows:

Quote:
GNU find version 4.1.20
Red Hat Enterprise Linux ES release 4 (Nahant Update 7)
If the delete option is not available, how can remove the directories on the fly?

Thanks.
 
Old 10-09-2012, 01:10 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
I don't now if the version of find in RHEL 4 (you should really upgrade that if you don't have an extended support contract) supports the -delete option, but you should be able to use the -exec option:
Code:
find searchdir -mtime +10 -exec rm '{}' ;\
Keep in mind that this will remove files only, if you want to additionally delete folders add the -r option to the rm command.
 
Old 10-09-2012, 02:29 PM   #7
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

I modified the command syntax to read as follows:

Code:
find searchdir -mtime +10 -exec rm '{}' ;\
When I run the script now, the following error messages appear on-screen:

Quote:
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
What am I missing here?
 
Old 10-09-2012, 02:40 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Sorry, I have a typo in that line, it should be
Code:
find searchdir -mtime +10 -exec rm '{}' \;
 
1 members found this post helpful.
Old 10-09-2012, 02:46 PM   #9
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
This thing is coming up with every excuse not to work...

Here is the script in full:

Code:
#!/bin/bash

# The purpose of this script is to go through the various
# photon clinics, and check the patient directory/pdf folders
# for any pdf files that are older than seven, 7, days.
# If there is a file that meets this criterium, it is removed
# from the folder.

# The series of for loops shown below will go through every clinic,
# patient folder, and pdf subfolder, and utilize the find command.

for searchdir in /CMS/xiodata/test/*/pdf
do
find searchdir -mtime +10 -exec rm '{}' \;
done
When the script is run, the latest error that appears on-screen is the following:

Quote:
find: searchdir: No such file or directory
find: searchdir: No such file or directory
find: searchdir: No such file or directory
find: searchdir: No such file or directory
find: searchdir: No such file or directory
 
Old 10-09-2012, 04:32 PM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You missed the dollar sign, the line should be
Code:
find $searchdir -mtime +10 -exec rm '{}' \;
 
1 members found this post helpful.
Old 10-09-2012, 07:26 PM   #11
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hello --

Once I inserted the dollar sign into the line in question, the script ran without issue.

Thanks to everyone for the help.
 
Old 10-09-2012, 07:38 PM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Nice to hear that it works now. Please mark this thread as solved using the thread tools at the top of the thread.
 
  


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
[SOLVED] run ps|grep command by script/command line ... ERROR: Unsupported option (BSD syntax) masuch Programming 4 05-23-2012 04:13 AM
[SOLVED] Question about bash script syntax musonio Linux - Software 9 09-10-2009 07:28 PM
[SOLVED] Question about sed syntax in bash script musonio Linux - Software 2 08-21-2009 07:17 AM
Bash script syntax error snowman81 Programming 5 11-16-2007 02:35 AM
bash syntax when envoking tar command in python script tangle Programming 4 02-04-2006 03:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:19 AM.

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