Windows Shell Script to scan folder & copy file - windows

I would like to loop throught folder & its subfolders & start scnanning and copying a file to specific location within each folder
Regards,

Sorry, can't comment with less than 50 reputation, have to answer instead.
Are you looking for a certain file type to copy or you want to copy everything from one folder to another? Or, if you find a file of interest, you want to create a new folder and copy it to there?
If you could specify these things, it will be a little easier to provide an answer.
Thanks, Tim.

As per my understanding you have to copy a specific file or file type into a specific dir.
You can try find command with copying specific file into your directory .
find /source_dir/ -type f -name "filename_or_type" -exec cp -t /destination_dir/ {} \;

Related

How to Copy Files Without Overwriting Any Files in Bash Script

I am trying to write a script that finds all my files that are .jpg, and copies them do a new directory. It currently looks like this:
find ~/Pictures -iname \*.jpg -exec cp {} ...newDirectory \;
The problem is that some of my older files have the same name as newer files, when the IMG_#### reset back to 0001 and started counting again.
Is there a way to find the .jpgs and copy without overwriting the files? Ideally giving them a new name in the process.
EDIT
I ended up learning about rsync, which in its own way does exactly what I was looking for. Thanks for the help!
Use -n parameter for cp, that means: do not overwrite an existing file.
To prevent identical names, you could just name all of them unique.
Example:
$ touch screenshot.jpg
$ cp screenshot.jpg screenshot-$(date "+%s").jpg
So basically, mass rename the new files you want to copy to the same name+date.
That will make them different from what's already there, sice the older ones are unnamed or (if you repeat this later) will have different dates.

Script to find files in subdirectories

I need a script that will find and get me all files in all subdirectories (and leave them in the folder structure as they are now). I know how to find and print that files:
find . -name "something.extension"
The point is, in those directories are lots files that was used before, but I don't want to get those, so the script should only find me files that matches some kind of path pattern which is:
xxx/trunk/xxx/src/main/resources
xxx is different everytime, and after resources there are still some folders that directories are different based on xxx.
Every top xxx folder contains folder named 'tags' (the same level as trunk) that stores previous releases of module (and every release has files that name I am looking for, but I don't want outdated files).
So I want to find all that files in subdirectories of that path pattern that I specified and copy to new location but leave folder structure as it is right now.
I am using Windows and cygwin.
Update
I combined answer commands that 'that other guy' posted below, and it works. Just to be clear I have something like this:
find */trunk/*/src/main/resources -name "something.extension" -exec mkdir -p /absolute/target/path/{} \; -exec cp {} /absolute/target/path/{} \;
Thanks.
Instead of searching under the entire current directory (.), just search under the directories you care about:
find */trunk/*/src/main/resources -name "something.extension"

Mac OS X command or script to zip all subfolders with a particular name

I have a folder containing many files and subfolders multiple levels deep. I'm looking for a command or script that will zip any subfolder called "fonts", resulting in a fonts.zip file at the same level as the fonts folder.
The fonts folders should remain after creation of their zip files (no delete).
If there is a fonts folder inside another fonts folder (unlikely case), only the top-level fonts folder should result in a fonts.zip file (ideal, but not mandatory).
If there is already a fonts.zip file at the same level as the fonts folder, it should be replaced.
I'll admit, I'm a Mac newbie. Hopefully there can be a simple terminal command to accomplish this. But I'm open to other ideas how to accomplish this.
Thanks,
-Matt
Using Kevin Grant's suggestion as a starting point, I was able to put together a terminal command that works the way I needed:
find . -type d -iname '*fonts' -execdir ditto -c -k -X --rsrc {} fonts.zip \;
I referred to the man pages for the find and ditto commands and their switches. I chose to use ditto over zip because it is supposed to be more compatible with HFS and resource forks under Mac OS X. Next I'll find out if StuffIt has a command line tool. If so, I'll use it instead of ditto.
This was also helpful.

Bash script to find specific files in a hierarchy of files

I have a folder in which there are many many folder and in each of these I have lots and lots of files. I have no idea which folder each files might be located in. I will periodically receive a list of files I need to copy to a predefined destination.
The script will run on a Unix machine.
So, my little script should:
read received list
find all files in the list
copy each file to a predefined destination via SCP
step 1 and 3, I think I'll manage on my own, but how will I do step 2?
I was thinking about using "find" to locate each file and when found, write the location in a string array. When all files are found I loop through the string array, running the "SCP" command for each file-location.
I think this should work, but I've never written a bash script before so could anyone help me a little to get started? I just need a basic "find" command which finds a filename and returns the file location if the file is found.
find $dir -name $name -exec scp {} $destination \;

Batch script to move files into a zip

Is anybody able to point me in the right direction for writing a batch script for a UNIX shell to move files into a zip one at at time and then delete the original.
I cant use the standard zip function because i don't have enough space to fit the zip being created.
So any suggestions please
Try this:
zip -r -m source.zip *
Not a great solution but simple, i ended up finding a python script that recursively zips a folder and just added a line to delete the file after it is added to the zip
You can achieve this using find as
find . -type f -print0 | xargs -0 -n1 zip -m archive
This will move every file into the zip preserving the directory structure. You are then left with empty directories that you can easily remove. Moreover using find gives you a lot of freedom on what files you want to compress.
I use :
zip --move destination.zip src_file1 src_file2
Here the detail of "--move" option from the man pages
--move
Move the specified files into the zip archive; actually, this
deletes the target directories/files after making the specified zip
archive. If a directory becomes empty after removal of the files, the
directory is also removed. No deletions are done until zip has
created the archive without error. This is useful for conserving disk
space, but is potentially dangerous so it is recommended to use it in
combination with -T to test the archive before removing all input
files.

Resources