copy specific files and count them - not as easy as it seems!
Hi all:
Here's my dilemma: to identify files of a specific type, copy them to a new location while preserving the original file attributes (date, time, full path, etc), and at the same time capture the count of the number of files identified as a variable for later reporting.
Here's where I am so far:
I know that to capture the output of wc -l as a variable, I need to enclose the command in backticks:
the problem comes when I try to tee the output from the find command into a wc -land into a cpio -dumpv. I get to do one or the other, but not both.
Sure, I can do a separate find on the output files and run a wc on that, but that's duplicated effort. There's got to be a way to do this!
OS is Mac OSX, though that shouldn't matter, I'm doing this in bash.
I now defer to people much smarter than I am, and look forward to your assistance!
Last edited by Franklin52; 04-20-2011 at 03:48 AM..
Reason: Please use code tags
Thanks for the assist! It got me a bit further along.
I ended up with the following:
from a command line, this will copy out xls files, and store the count from wc -l in the variable, xlscnt.
The problem now is, this works from a command line prompt, but it doesn't work in a bash script. What I get in the bash script (named myscript.sh) is the following:
Line 20 of myscript.sh is as follows:
I want to capture the variable xlscnt for inclusion in a report later.
One step closer.........
Last edited by Franklin52; 04-20-2011 at 03:50 AM..
Reason: Please use code tags
Thanks, bash-o-logist!
I tried your solution, but was getting an error on line 3 of the shopt command.
My overall script asks the user for a source path, and a destination path, then reads file from the source and writes to the destination. I tried to use cp before, but since several of the source paths contain blank spaces, I was getting errors because cp couldn't interpret the space.
Ultimately, I'll be expanding my script to allow for capturing other types of files too, with a count for each file type. That's mainly why I was trying to accomplish this using the method I posted as line 20 previously.
If it works on the bash command line, and not in a bash script, is the script really being bash interpreted, not sh? It must be chmod to executable as well as having the right path to bash on the #!path first line, see man execvp.
I like "script_chain | read var_name" over "var+name=$( script chain )" over "var_name=`script_chain`", it just flows left to right, no unnecessary nesting.
Is it possible to only copy selected files+its directories when you are copying recursively?
find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print
From the above line, I want to only copy *txt and *ini files from /OriginalFolder/*
... (4 Replies)
Hello All,
Since i'm relatively new in shell script need your guidance.
I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder.
so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Hi guys...please any one help me ....
how to copy files from source to target location
if 5 files copied successfully out of 10 files then implement success=10
and if remaining 5 files not copied successfully then count error=5
how to implement this condition with in loop
i need code linux... (0 Replies)
Hi
I need to copy a huge directory with thousands of files onto another directory but without *.WMV files (and without *.wmv - perhaps we need to use *.).
Pls advise how can I do that.
Thanks (17 Replies)
I hope this isn't as silly as it sounds from the title of the thread.
I have software that outputs files where the name starts with a real number followed by underscore as a prefix to an input file name. These will list in the directory with the file with the smallest real number prefix as the... (5 Replies)
Hi Experts,
I want to know the count of specific word in a file. I have almost 600+ files.
So I want to loop thru each file and get the count of the specific word.
Please help me on achieving this...
Many thanks (2 Replies)
Hey all,
I'm looking for a command that will search a directory (and all subdirectories) and give me a file count for the number of files that contain specific characters within its filename. e.g. I want to find the number of files that contain "-a.jpg" in their name.
All the searching I've... (6 Replies)
Hi
I am a shell-script newbie and am looking to synchronize certain files in two directory structures.
Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over.
I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)