find: missing argument to `-exec' when running from script file [duplicate] - linux

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
When I run the following command from my script file it gives me:
find: missing argument to `-exec'
But the same command from the command line works normally:
find /home/poseidoncharters/poseidon_backup/*.sql -mtime +1 -exec rm -f {} \;
I run the script like ./myscript.sh

Well as other said on their comment command seems correct, and it was. the problem i was having was newline character problem, as i wrote this script in windows machine and then uploaded to unix server, so as soon as i ran dos2unix remove_backup.sh it started to work.

Related

How to replace path stored in variable using sed [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 12 months ago.
I've a script
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
I need to replace path using sed command. I've stored paths in variables
replace="/run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/"
replacewith="/home/nnice/Windows/D/FILES/Softwares/HTTP/Downloads/"
I am trying following command but it doesn't work
sed -i "s/$replace/$replacewith/g" script.sh
I've also used different separators instead of / but script remains unchanged.
[nnice#myhost scripts]$ sed "s|$replace|$replacewith|g" script.sh
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
can you please help me with that to replace them using sed command?
Thank you
Your command fails because you're using the same separator for the sed command and your file paths. File paths need to use / but sed separators can be anything, so try this:
sed -i "s#$replace#$replacewith#g" script.sh

How to expand variable in bash tar command [duplicate]

This question already has answers here:
bash script execute command with double quotes, single quotes and spaces
(2 answers)
Variable containing multiple args with quotes in Bash
(4 answers)
Closed 1 year ago.
I have a tar command to create .tar.gz file. It looks like below:
exclude="--exclude='.cache' --exclude='.composer' --exclude='.local' --exclude='.config'"
tar "$exclude" -czf "$backupsDir/$timestamp-home.tar.gz" /home
The problem is the $exclude variable in not evaluated and excluded directories are still in the archive. Can somebody tell me what is wrong with it? Thanks a lot.

How to flush contents in shell pipes? [duplicate]

This question already has answers here:
How to make output of any shell command unbuffered?
(5 answers)
Force flushing of output to a file while bash script is still running
(13 answers)
Closed 3 years ago.
When typing such a command:
find . -type f -iname "*part*"
I get, slowly, file names appearing, as soon as they're found -- one at a time.
Adding a sed expression behind it...
find . -type f -iname "*part*" | sed "s#^\./##"
… delays the display up to the end of the find command, then processing everything at once.
How to avoid that? How to flush to sed?
PS- Alternatively, how to remove the ./ prefix from every line, without delaying the display?

Move files with exception in bash [duplicate]

This question already has answers here:
Move all files except one
(14 answers)
Closed 3 years ago.
It works fine when I wrote this code on bash shell
mv -v `pwd`/!(.git) `pwd`/NewDir
But if I create shell script file like below,(name of this file is "s.sh")
#!/bin/bash
mv -v `pwd`/!(.git) `pwd`/NewDir
it returns error
./s.sh: line 2: syntax error near unexpected token `('
./s.sh: line 2: `mv -v `pwd`/(!.git) `pwd`/NewDir'
How can I fix it?
!(.git) is an extended glob, you need to enable extglob to make it work in a non-interactive shell. And I think instead of calling pwd twice, you can use PWD variable in this case.
shopt -s extglob
mv -v "$PWD"/!(.git) "$PWD/NewDir"

Change the output of find command [duplicate]

This question already has answers here:
How to strip leading "./" in unix "find"?
(8 answers)
Closed 8 years ago.
Hello guys I'm using the find command to find the .apk files in a directory. But the output of the find command is **./**foo.apk.
I don't want to have this ./.
cd output/dist
output_apk=`find ./ -name "*.apk" -print0`
echo "$output_apk"
The output is ./foo.apk.
I have try the sed command with no luck.
find output/dist -name "*.apk" |
sed 's%^output/dist/%%'
This also avoids the useless cd and removes the erroneous -print0. If you are not piping into a program which requires null-terminated input, this option is wrong.

Resources