How to copy file after finding it - linux

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/

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

Rename "*.sample.js" files to "*.js" in linux

Currently, I'm working on a project where I need to git ignore my local config files. This means that if the user clones the repository, he would see *.sample.js files inside the config dir.
If the user executes make config the following script is executed.
config: ###Misc Create config files
#cd src/backend/config
#cp cache.sample.js cache.js
#cp database.sample.js database.js
#cp steam.sample.js steam.js
#cp teamspeak.sample.js teamspeak.js
#cp website.sample.js website.js
#echo
This script basically removes the "sample" part from the file name. The above code needs to be modified when ever I add a new config file, which is not a practical thing.
I would like to convert this into a simple regex command like:
find -iname \*.sample.js -type f -exec rename -n 's/(.*sample\.js)/$1/' {} \;
This command doesn't work, at least on windows MINGW64 bash. I need a vanilla solution.
Thanks to #Jetchisel for a working solution:
find -iname '*.sample.js' -type f -exec bash -c 'for f; do cp "$f" "${f/.sample}"; done' sh {} +
Bonus: why that last + sign?
That will process as many files as possible while avoiding arg_max
as opposed to \; which will process one file per -exec call

Find all files and unzip specific file to local folder

find -name archive.zip -exec unzip {} file.txt \;
This command finds all files named archive.zip and unzips file.txt to the folder that I execute the command from, is there a way to unzip the file to the same folder where the .zip file was found? I would like file.txt to be unzipped to folder1.
folder1\archive.zip
folder2\archive.zip
I realize $dirname is available in a script but I'm looking for a one line command if possible.
#iheartcpp - I successfully ran three alternatives using the same base command...
find . -iname "*.zip"
... which is used to provide the list of / to be passed as an argument to the next command.
Alternative 1: find with -exec + Shell Script (unzips.sh)
File unzips.sh:
#!/bin/sh
# This will unzip the zip files in the same directory as the zip are
for f in "$#" ; do
unzip -o -d `dirname $f` $f
done
Use this alternative like this:
find . -iname '*.zip' -exec ./unzips.sh {} \;
Alternative 2: find with | xargs _ Shell Script (unzips)
Same unzips.sh file.
Use this alternative like this:
find . -iname '*.zip' | xargs ./unzips.sh
Alternative 3: all commands in the same line (no .sh files)
Use this alternative like this:
find . -iname '*.zip' | xargs sh -c 'for f in $#; do unzip -o -d `dirname $f` $f; done;'
Of course, there are other alternatives but hope that the above ones can help.

Howto replace a file in several sub-folders

I've a series of directories containing a set of files. There is a new copy of this file which I would like to replace all instances with. How can do this with find command?
Latest file is in /var/www/html is called update_user.php
There are 125 directories with several other files including a copy of update_user.php. I want to replace these with the one in update_user.php excluding itself.
This should do the job:
find /path/to/old/files -type f -name update_user.php -exec cp /path/to/new/update_user.php {} \;
You should check if the new file is not inside /path/to/old and if so than first copy it outside and use that copy but.. it'll not harm if you don't - one cp will fail with are the same file error.
You can use
cp -v to see what it does
cp -u to update only when source file is newer
echo cp to perform dry run
I would suggest to check first if all dest. files are the same with:
find /path/to/old/files -type f -name update_user.php -exec md5sum {} \;|awk '{print $1}'|sort|uniq

Issues with large directory and cp command

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/ \;

Resources