find: missing argument to -exec not copying - linux

Want copy all files from catalog /usr/sbin to other folder with range beetwen 500kb and a 4M.
First line of code, that I write it's normally do.
find /usr/sbin/ type -f -size +500k -size -4M
But when I add -exec code action above to do it's print err:
find /usr/sbin/ type -f -size +500k -size -4M -exec cp /usr/sbin '/dest-catalog'
find: missing argument to `-exec`
Try 'find --help' for more information
Can't find any answer despite study man, find --help, search answer.

Ok, thanks RavinderSingh13 & 123. Found solution write this code:
find /dest_catalog/ -type f -size +500k -size -4M -exec cp -nv {} ./ \;

Related

problem in copying find results in another directory

I'm trying to execute this command to copy the latest file that exist in the courant directory to another one .
find . -mtime -1 -exec cp -r {} /media/96DB-120D/bck \;
but after copying the recent files , I find the other content of the folder that does not respond to the condition -mtime -1 .
If any one had an idea about how to fix it to just copy the result of find command and thanks.
The find command probably includes the directory and then cp copies all the files in the directory. Add -type f to only have find report actual files.
Try the -p option of cp command which will preserve the timestamp of the copied file:
find . -mtime -1 -exec cp -pr {} /media/96DB-120D/bck \;
I think this is the best solution :
find . -mtime -1 -type f -exec cp --parents {} /media/960DB-120D/db \;

How to delete files matching linux command output

I have a list of files which I got using find / -type f -size +10M -exec ls -l {} \;
I got this command from here
How can I remove all these files ?
I tried
sudo rm `find / -type f -size +10M -exec ls -l {} \;`
but it doesn't work.
Also, what does {} \ do ? And what's the use of -exec in this command, will the pipe operator not work ?
I think it should be possible to have find run rm on each file found, but I couldn't get it to work.
So here is my solution using a for loop:
for $f in `find / -type f -size +10M`;do rm $f;done
Thanks guys, I finally got it to work with #some-programmer-dude suggestion:
find / -type f -size +10M -exec rm {} \;

shell script that allows to empty a file when it exceeds a certain size

I'm looking for a Linux script that allows to empty the contents of a file when it exceeds a certain size for example 50 kB.
I tried this script :
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec echo >"{}" \;
but it does not work.
On the other hand it works well for deleting files:
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec rm "{}" \;
Your redirection (>) takes place before starting find. You probably now have a file of name {}.
I propose to use truncate instead of a redirection for overwriting the file:
find /home/walid/Documents -type f -size +50k -exec truncate --size 0 "{}" \;
A little tweak on your first script should work fine:
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec sh -c 'echo -n > {}' \;
Give a try to this:
find /home/walid/Documents -type f -size +50k -exec cp /dev/null {} \;
That should work in any *nix like operating system, but also you could give a try to truncate -s 0 filename
find /home/walid/Documents -type f -size +50k -exec truncate -s 0 {} \;

Remove files in subdirectories older than 1 day with Linux command

I am honestly nowhere near to be a decent bash scripter, but I made a little research and found a command that seems to be useful
find /path/to/files* -mtime +1 -exec rm {} \;
The question is if this line will remove directories? Because I want to only remove files that are images (actually in a *.jpeg format)
No, rm without the -r flag does not remove directories.
It looks like you want to add some more filters:
-type f to match only files
-name '*.jpeg' to match only files ending with .jpeg
Lastly, instead of -exec rm {} \;, you could use the much simpler -delete.
Putting it together, this looks more appropriate for you:
find /path/to/files* -mtime +1 -type f -name '*.jpeg' -delete
Then narrow your search results to *.jpeg files:
find /path/to/files* -mtime +1 -type f -name "*.jpeg" -exec rm {} \;
It's always better to remove the exec parameter to do a dry run before delete:
find /path/to/files* -mtime +1 -type f -name "*.jpeg"
Each line will be passed to rm command, and nothing more.

Using find command on in a Bash Script to find integers

I need to find and archive files with a certain file name e.g. ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
however I can not seem to find a way to limit the find function to find only 3 integers as there are files that are named ABC_CBA.jpg that I do not want included
Try this find:
find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
EDIT: Or using regex:
find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;

Resources