missing argument to -exec - bash

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
{} \;

Related

find command doesn't find directory in shell script

I have a shell script that is finding and copying files, which works just fine. However when it tries to find and copy a directory it is not working
#!/bin/sh
PROJECT_NAME=$1
HELM_BUILD_FOLDER="helm-build"
HELM_PROJECT_BUILD_FOLDER="$HELM_BUILD_FOLDER/$PROJECT_NAME
#find and copy all FILES from .helm/my-project to helm-build/my-project/project-values THIS IS SUCCESSFUL
find .helm/$PROJECT_NAME* -maxdepth 0 -type f,d -exec cp -av "{}" $HELM_PROJECT_BUILD_FOLDER/project-values/ ";"
#find and copy all diectories from .helm/my-project to helm-build/my-project THIS IS FAILING even though directories exist
find .helm/$PROJECT_NAME/* -maxdepth 0 -type f -exec cp -av "{}" $HELM_PROJECT_BUILD_FOLDER/ ";"
I have tried several different means of fixing this. I've tried using execdir instead of exec, used cp -R. I tried to simply output the results of the directory find and nothing got output... it's almost as if it's not being ran.
Another weird thing is happening when i try to execute in the terminal. This works, despite $PROJECT_NAME not being defined outside of the script:
find .helm/$PROJECT_NAME/* -maxdepth 0 -type d -exec cp -R -av "{}" helm-build/my-project/ ";"

Delete files that are 5 days old using bash script

I currently use a command that searches a directory and deletes 5 day old files.
find /path/to/files* -mtime +5 -exec rm {} \;
I run it from the command line and it works fine. But when I put it in a .sh file it says findĀ /path/to/files*: No such file or directory.
There is only two lines in the shell script:
#! /usr/bin/env bash
find /path/to/files* -mtime +5 -exec rm {} \;
How can I rewrite the script so that it works?
`
The error happens if there are currently no files matching the wildcard, presumably because none have been created since you deleted them previously.
The argument to find should be the directory containing the files, not the filenames themselves, since find will automatically search the directory. If you want to restrict the filenames, use the -name option to specify the wildcard.
And if you don't want to go into subdirectories, use the -maxdepth option.
find /path/to -maxdepth 1 -type f -name 'files*' -mtime +5 -delete
This works:
#! /usr/bin/env bash
find /home/ubuntu/directory -type f -name 'filename*' -mtime +4 -delete
Here is an example:
find /home/ubuntu/processed -type f -name 'output*' -mtime +4 -delete

BASH; using find and -exec to remove .pyc files

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

Get "unknown predicate `-delete" Error in script

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.

Cant find .gz file in AIX

I basically have written a shell script in AIX that will delete some old log file and will compress some .
This is my script
#!/bin/sh
###
### Static variables
###
nmon_dir="/var/log/applog/nmon"
cd $nmon_dir
find $nmon_dir -xdev -type f -mtime +360 -name "*.nmon*" -exec rm {} \;
find $nmon_dir -xdev -type f -mtime +300 -name "*.nmon" -exec gzip {} \;
I could delete the files as i wanted but that I am not sure whether it compressed those file . Because i couldn't find .gz file both in root or /var/log/applog/nmon path .
Need Help!
Seems to me like your KSH might be taking the {} you pass to find as a compound command definition instead of as the filename placeholder. Try escaping it, I use it as \{\} and it never gives me problems.

Resources