find and copy command not working in bash - linux

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

Related

Linux move files from dir to dir by name mask

I am trying to move all files with names starts with SML from directory to another.
Tried with
find /var/.../Images/ -name SML\* mv /var/.../Images/Small but doesnt work
try find /var/.../Images/ -name SML\* -exec mv {} /var/.../Images/Small/ \;
I guess you want something like this:
dir=/path/to/your/Images
mkdir -p "$dir/Small"
find "$dir" -name "SML*" -not -wholename "$dir/Small/*" -exec mv {} "$dir/Small/" \;
Since the directory you move the files to is a subdirectory of the one you seach in, you need to exclude the files already moved there. So I added -not -wholename "$dir/Small/*"
To execute a command for each found file, you need -exec .... The alternative would be to pipe your find results to a while read loop.
When using -exec, the found name can be referenced by {}.
See man find for a lot more information.

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/

How to find files recursively by file type and copy them to a directory?

I would like to find all the pdf files in a folder. It contains pdf files inside and more directories that contain more as well. The folder is located on a remote server I have ssh access to. I am using the mac terminal but I believe the server I am connecting to is Centos.
I need to find all the pdfs and copy them all to one directory on the remote server. I've tried about 10 variations with no luck. Both mine and the remote systems do not seem to recognise -exec as a command though exec is fine so thats a problem.
Im not sure what the problem is here but the command does not fail it just sits there and stalls forever so I do not have any useful errors to post.
cp $(find -name "*.pdf" -type f; exec ./pdfsfolder {} \; | sed 1q)
find: ./tcs/u25: Permission denied
find: ./tcs/u68: Permission denied
-bash: /var/www/html/tcs_dev/sites/default/files/pdfsfolder: is a directory
-bash: exec: /var/www/html/tcs_dev/sites/default/files/pdfsfolder: cannot execute: Success
cp: target `./runaways_parents_guide_2013_final.pdf' is not a directory
This is the last one I tried, I think I can ignore the permission denied errors for now but im not sure about the rest.
Try this:
find . -name "*.pdf" -type f -exec cp {} ./pdfsfolder \;
Paul Dardeau answer is perfect, the only thing is, what if all the files inside those folders are not PDF files and you want to grab it all no matter the extension. Well just change it to
find . -name "*.*" -type f -exec cp {} ./pdfsfolder \;
Just to sum up!
Something like this should work.
ssh user#ip.addr 'find -type f -name "*.pdf" -exec cp {} ./pdfsfolder \;'

Find -exec and Bash scripts

I'm writing a script in bash.
I invoke it with
find *.zip -type f -exec ./myscript.sh {} \;
At the top of my script I invoke another script like this:
#!/bin/bash
. ticktick.sh
I get the following error
.: ticktick.sh: file not found
If I invoke the script like this
./myscript.sh somefile.zip
it works
If I put the ticktick.sh script in my path in another directory it breaks, so that isn't an option. Is there some special kind of context that scripts called with a find have? I'm obviously new to BASH scripting. Any help would be appreciated
I think there are 2 problems.
1.: if you want to search for all zip files in the current directory, you have to write the following command
find . -type f -name *.zip -exec ...
2.: you execute myscript.sh with ./ before it. So myscript.sh has to be in the current working directory. if your script is in /home/jd/ and you execute it from /home/ your myscript.sh will be not found.
first you have to determine the directory of your files:
install_path=$(dirname $(readlink -f $0))
So your complete find command is:
find . -type f -name *.zip -exec $install_path/myscript.sh {} \;
The myscript.sh file have to be in the same directory as ticktick.sh

Create file in Linux and replace content

I have a project in Linux. I want to create a file named index.html in all folders.
So I have used the following command:
find . -type d -exec touch {}/index.html \;
It's working! Now I'm trying to copy the existing file from a given location and it to be automatically replaced into all the folders of my project.
This should actually work exactly in the same way:
find . -type d -exec cp $sourcedir/index.html {}/index.html \;
If I understand your question correctly, what you want is to copy a given file in all the directories.
You can use a similar find command :
find . -type d -exec cp -f /tmp/index.html {} \;
where /tmp/index.html is path to the original file (replace it with your own path).
Also, you don't need to create the files if your final objective is to replace them with the original file.
tar -cvzf index.tar.gz `find . -type f -iname 'index.html'` && scp index.tar.gz USER#SERVER:/your/projec/root/on/SERVER && ssh USER#SERVER "tar -xvzf index.tar.gz"
Or if you're in the proper directory localhost, and rsync is available:
rsync -r --exclude='**' --include='**/index.html' . USER#SERVER:/your/projec/root/on/SERVER
HTH

Resources