Issues with large directory and cp command - linux

I am trying to copy all jpgs from 1 directory to another but only new files and ones that have been updated.
I am using the following command:
\cp -uf /home/ftpuser1/public_html/ftparea/*.jpg /home/ftpuser2/public_html/ftparea/
And I am getting the error:
-bash: /bin/cp: Argument list too long
I am assuming that there are 2 many files in this directory for the cp command to work
I have also tried:
find /home/ftpuser1/public_html/ftparea/ -name "*jpg" -exec cp -uf {} /home/ftpuser2/public_html/ftparea/
and got the following:
find: missing argument to `-exec'
Any ideas?

You need to make sure to include the final “\;” to finish the command that -exec should execute.

Using find you shouldn't have the brackets in quotes. Try this
find /home/ftpuser1/public_html/ftparea/ -name "*jpg" -exec cp -uf {} /home/ftpuser2/public_html/ftparea/ \;

Related

find and copy command not working in bash

This line works in the terminal, but not in a bash script:
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
find . -name '*.wav' -exec cp {} $tempfolder \;
I'm trying to copy all the WAVE files from all the sub-directories to a temporary folder
So, I solved it. Turns out, that the
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
was not actually changing the directory for the
find .
to work. The script was trying to find the files in its own directory. Once i wrote find "$absolutePath" -name '*.wav' -exec cp {} "$tempfolder" \; it worked

How to copy file after finding it

I am trying to create a script which will execute the following actions
1- execute mvn clean
2- execute mvn package
3- find the .jar file and move it to a destination folder passed as an arg on the command line
#!/bin/bash
destination=“$1”
#Clean
mvn clean
#Package
mvn package
#Transfer the generated jar file to the destination folder
find . -name “*.jar” | xargs cp -t $destination
This is running the maven scripts fine but when I go to the destination folder the file is not copied.
I know that the .jar file is there and is found when I print out the result of find . -name “*.jar”:
./target/QuoteTool-0.0.1-SNAPSHOT.jar
But the step of copying the file is not happening correctly.
Any help much appreciated.
Note: I am using a Mac
You don't need to use xargs but just use the -exec option within find command itself.
Also you have Unicode Windows style double-quotes (see Unicode Utilities), “*.jar” should have been used as "*.jar"
find . -type f -name "*.jar" -exec cp -t "$destination" "{}" +
In the above example, cp with + over exec will avoid forking a cp for each of the jar file found, but rather copies all the files found to destination in one shot.
I recommend to rewrite the line in the following way
find . -name "*.jar" -exec cp {} $destination \;
it gives you the result you would like to have
I would use the -e option with xargs to look something like
find . -name “*.jar” | xargs -e cp $destination/

Newbie-ish error: cp: omitting directory

Pulling my hair as I'm stuck with a basic error without understanding why:
find . -type f -exec cp del {} \;
We're in a "test" directory, in which I created one "del" subdirectory. The "test" directory contains a variety of files of various types.
The result is a series of lines (same number as the number of files present in the directory from where the command is ran) with:
cp: omitting directory `del'
Possibly useful details follow.
Debian Wheezy, standard shell interface.
As a prelude to more complex exclusion and exec patterns I wanted to start with this fundamental test... and had this.
I think I excluded the "del" directory with "type -f", so it's not as if I was asking Linux to move a directory within itself.
There are no other directories or subdirectories.
Permissions: everything belongs to the current user.
I made variations for the "cp del" part, putting it in simple or double quotes, or using ./del, no difference.
I also tried with -R
find . -type f -name '*script1*' -exec cp -R ./del {} \;
That gave:
cp: cannot overwrite non-directory `./script1' with directory `./del'
Same with -r
If what you're trying to do is to copy some files found by find command to the del directory, then you can do it like this:
find . -type f | xargs cp -T del/
Or like this:
find . -type f -exec cp {} del \;

Copying modified files and their file structure

I'm still new to linux scripting, so this might be a bit trivial.
As part of my script, I am trying to copy all the modified files (relative to the original backup I have made) to another folder while keeping the file structure.
I have found this method:
find /SourceFolder/ -newer /BackupFOlder/ -exec cp --parents \{\} /Destination
However the above command does not work, giving me the error:
find: missing argument to 'exec'
Why doesn't this work?
I also found this:
Copy files preserving folder structure
But I want to use cp command only.
Would really appreciate some help.
The -exec option requires a ; argument to tell it where the command ends, because you could have additional find options after it.
find /SourceFolder/ -newer /BackupFOlder/ -exec cp --parents {} /Destination \;
However, a better solution would be to use rsync:
rsync -a /SourceFolder /BackupFolder
Try
tar cf - . | (cd <some other dir>; tar xfv -)

Running command recursively in linux

I'm trying to come up with a command that would run mp3gain FOLDER/SUBFOLDER/*.mp3 in each subfolder, but I'm having trouble understanding why this command doesn't work:
find . -type d -exec mp3gain \"{}\"/*.mp3 \;
When run, I get error Can't open "./FOLDER/SUBFOLDER"/*.mp3 for reading for each folder and subfolder.
If I run command manually with mp3gain "./FOLDER/SUBFOLDER"/*.mp3 it works. What's going wrong?
If you have a fixed data structure like
folder1/subfolder1/
folder1/subfolder2/
folder2/subfolder1/
[...]
and using zsh or bash version >=4.0 you could try
mp3gain **/*.mp3
But to make sure check the output of
ls **/*.mp3
before you are getting serious with mp3gain.
When you run mp3gain "./FOLDER/SUBFOLDER"/*.mp3 from your shell, the *.mp3 is getting expanded by the shell before being passed to mp3gain. When find runs it, there is no shell involved, and the *.mp3 is getting passed literally to mp3gain. The latter has no idea how to deal with wildcards (because normally it doesn't have to).
Hmmm. Just tried this to test how the directory is parsed by replacing mp3gain with echo and it works:
find . -type d -exec echo {}\/\*.mp3 \;
Try running your version of the command but with echo to see the file output for yourself:
find . -type d -exec echo \"{}\"/*.mp3 \;
Seems the quotes get in the way in your original command.
this works...
find /music -name *mp3 -exec mp3gain -r -k {} \;

Resources