[SOLVED] Syntax question for a command within a bash script
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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.
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.
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.
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'
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.