copy specific files and count them - not as easy as it seems!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy specific files and count them - not as easy as it seems!
# 1  
Old 04-19-2011
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:
Code:
find . -type f -iname '*.xls*' | tee wc -l | cpio -dumpv /destination_path

I know that to capture the output of wc -l as a variable, I need to enclose the command in backticks:
Code:
xlscnt=`find . -type f -iname '*.xls*'| wc -l`

the problem comes when I try to tee the output from the find command into a wc -l and 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
# 2  
Old 04-19-2011
Bash and some systems ksh can do this:
Code:
find . -type f -iname '*.xls*' | tee >(
  cpio -dumpv /destination_path
 ) | wc -l | read cnt

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 04-19-2011
getting close, but not quite.......

Thanks for the assist! It got me a bit further along.

I ended up with the following:
Code:

xlscnt=`find . -type f -iname '*.xls*' | tee >(cpio -dumpv &outpath) |wc -l`

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:
Code:
myscript.sh: command substitution: line 20: syntax error near unexpected token `('
myscript.sh: command substitution: line 20: `find . -type f -iname \'*.xls*\'|tee >(cpio -dumpv $outpath)|wc -l'

Line 20 of myscript.sh is as follows:
Code:
xlscnt=`find . -type f -iname '*.xls*'|tee >(cpio -dumpv $outpath)|wc -l`

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
# 4  
Old 04-19-2011
Code:
#!/bin/bash
# tested on bash 4
shopt -s globstar
shopt -s nullglob
numfiles=0
for files in **/*.xls
do
    cp -p "$files" /destination
    ((numfiles++))
done
echo $numfiles


Last edited by bash-o-logist; 04-20-2011 at 11:51 AM..
This User Gave Thanks to bash-o-logist For This Post:
# 5  
Old 04-20-2011
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.
# 6  
Old 04-20-2011
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.
This User Gave Thanks to DGPickett For This Post:
# 7  
Old 04-20-2011
That was the final step I needed, DGPickett! Making the script executable took care of it.

Thanks for the assist, I really appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy Specific Files Recursively

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)
Discussion started by: apacheLinux
4 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

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)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Rsync to copy specific subfolders and files to new directory

RootFolderI: RootFolderI/FolderA/Subfolder1/Subsub1/JPG1.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub1/JPG2.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub2/JPG3.jpg . . . RootFolderI/FolderB/Subfolder1/Subsub1/JPG4.jpg -> want this jpg ... (1 Reply)
Discussion started by: blocnt
1 Replies

4. Post Here to Contact Site Administrators and Moderators

How to count successfully copy files source to target location with check directory in Linux?

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)
Discussion started by: sravanreddy
0 Replies

5. Shell Programming and Scripting

How to copy a directory without specific files?

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)
Discussion started by: reddyr
17 Replies

6. Shell Programming and Scripting

How to copy specific files when you don't know the file name?

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)
Discussion started by: LMHmedchem
5 Replies

7. Shell Programming and Scripting

Need script to count specific word and iterate over number of files.

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)
Discussion started by: elamurugu
2 Replies

8. UNIX for Dummies Questions & Answers

Unix command to count the number of files with specific characters in name

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)
Discussion started by: murphysm
6 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

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)
Discussion started by: sharpsharkrocks
3 Replies

10. Solaris

To copy the files newer than specific date

Dear all, Can you help me in copying files newer than speciifc date Thanks in advance, Rajesh (3 Replies)
Discussion started by: RAJESHKANNA
3 Replies
Login or Register to Ask a Question