The following script I have saved in an .sh file on the server that clears a few directories of old files and directories.
#!/bin/bash
find /PATH_TO_DIRECTORY_1 -mtime +5 -type f -delete
find /PATH_TO_DIRECTORY_2 -mtime +5 -type f -delete
find /PATH_TO_DIRECTORY_3 -mtime +5 -type d -exec rm -rv {} +
This is the error message when I run the script:
'ind: unknown predicate `-delete
In addition to
$'\r': command not found
I don't think the latter disturbs the code but the first one surely does.
Mind that I edit my code on Windows 10 and my server is an Ubuntu 64x run through Amazon Web Services (EC2).
I encountered the same issue.
The problem was that my file was in dos format.
Using the command dos2unix on my file, solve the problem.
Related
Using the command line in Ubuntu 16.04.2 LTS. I'm getting towards the end of Zed Shaw's LPTHW, and on the video to ex46.py he exercises the following bash command to find and remove all .pyc byte code files:
find . -name "*.pyc" -exec rm {}
On the video this successfully removes all of Zed Shaw's .pyc files. However, upon typing in the exact same command I get the following error:
find: missing argument to `-exec'
I understand that there are many ways to delete .pyc files, however, since I'm following along with Zed Shaw I'd like to know how to do it using find and -exec. What am I doing wrong?
you need to terminate the -exec command with \;
find . -name "*.pyc" -exec rm {} \;
have a look at find -exec in the man page.
as mentioned in the comments by Gordon Davisson it may be more efficient to terminate the command with + as rm is then invoked fewer times:
find . -name "*.pyc" -exec rm {} +
You could leverage using -delete over -exec rm as the former does not spawn a new process for each of the file instance to delete. Also you could chip in the -type f option to apply the operation for files only.
find . -type f -name "*.pyc" -delete
I am having a problem with a bash command returning the following error:
/usr/bin/find: missing argument to `-exec'
The actual command I am running is:
/usr/bin/find /backup-directory/ -maxdepth 1 -type f -mtime +14 -printf "%f\n" -exec /usr/local/bin/aws s3 mv /backup-directory/{} s3://my-s3-bin/{}\;
The goal was to have this command called from the crontab nightly to search a directory and move any file older than 14 days to Amazon S3 using the aws cli.
The find command works correctly up until just before the -exec, with the following output:
/usr/bin/find /backup-directory/ -maxdepth 1 -type f -mtime +14 -printf "%f\n"
20161030002947.Pg
20161029002644.Pg
20161027002705.Pg
20161028002402.Pg
20161031002440.Pg
And just the aws cli move command with explicit file name to move works as intended: The following command will move 20161030002947.Pg for instance, from the local backup directory to the s3 bin.
/usr/local/bin/aws s3 mv /backup-directory/20161030002947.Pg s3://my-s3-bin/20161030002947.Pg
I don't know why it is breaking when I put them together with the -exec and {} parameters.
The reason everything is called from the full path is to make sure there are no unforeseen issues when the command is called from crontab, and the OS on this particular server is Debian 8.
I suggest to replace
{}\;
by
{} \;
find //C://Source_dir -type f! -name sample_file*$(date +%Y%m%d)* -exec mv {} //D://target_dir// \;
The above command is working in UNIX, but when I use this command in ODI OS COMMAND it is throwing error.
A backup program I used recently made duplicates of whole bunch of files throughout my computer because of some setting that I've since changed.
When the backup program made a copy, it renamed the old one original1.thefilename.extension. I'm trying to automatically delete all of these unnecessary files with a simple shell command.
find -type f -name 'original1*' -exec rm {} \;
However, when I try to run this I get
find: missing argument to `-exec'
I've looked all over the web for a solution. I've found suggestions that I should try exec rm +, -exec rm {} +, -exec rm {} \;, -exec rm + etc. but none of them work. I am using Windows 8.1
I would really appreciate any help!
In Windows command shell, you don't need to escape the semicolon.
find -type f -name 'original1*' -exec rm {} ;
Your version of the command should work in a bash shell (like cygwin).
It's interesting that you get the gnu find to execute, because on my Windows 8.1 machine, I get Microsoft's find.
I am working on a shell script to clear out files from a folder if they are older than 60 days. I am using find command to do that. I have one more requirement in that as if the file exist then the script can remove such files. If no files are there which are older than 60 days, i don't want my command to give any error messages.
find AA* -mtime +60 -exec rm {} \;
I am using above given command. It gives the error message "No such file or directory" if there is no files are older than 60 days as well as if they are not starts with "AA". I don't want any such messages reported for my command.
Please help.
TIA.
Perhaps you are missing the target, i. e. AA* files in current directory?
How about changing it to
find . -name 'AA*' -mtime +60 -exec rm {} \;
or if you need to check only files or directories starting from 'AA' you could use -regex:
find . -regex './AA.*' -mtime +60 -exec rm {} \;
I don't want any such messages reported for my command.
Redirect command's standard error to /dev/null
... 2>/dev/null