find size, cp breaks directory structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find size, cp breaks directory structure
# 1  
Old 02-24-2010
find size, cp breaks directory structure

I'm using this now:
Code:
find /some/path/with/sourcefiles -type f -size -7M -exec /bin/cp -uv {} /some/path/ \;

but it doesn't preserve the directory structure, also I've tried it with
Code:
find /some/path/with/sourcefiles -type f -size -7M -exec /usr/bin/rsync -auv {} /some/path/ \;

but that doesn't either. I've also tried using
Code:
find /some/path/with/sourcefiles -type f -size -7M | cpio -pd /some/path/ \;

but I get "too many arguments" error. What am I doing wrong?
# 2  
Old 02-24-2010
Please specify (in words) what you actually want the script to do. It is not obvious from the examples.
# 3  
Old 02-24-2010
cp all the files less than a certain size in a source directory to another directory, but have the same directory and subdirectory in the new location.

Last edited by unclecameron; 02-24-2010 at 09:26 PM..
# 4  
Old 02-25-2010
There is no simple way. cp will only create a directory tree if it copies a complete tree to begin with. What might work as intended (untested)
Code:
find /some/path/with/sourcefiles -type f -size -7M -print | while read file
do
    dir=$( dirname $file )
    echo mkdir -p "/some/other/$dir"
    echo cp -uv "$file" "/some/other/$dir"
done

If it looks like it would work as intended, remove the echo on both lines.
# 5  
Old 02-25-2010
Quote:
find /some/path/with/sourcefiles -type f -size -7M | cpio -pd /some/path/ \;
This one was nearly there. There is a spurious "\;" at the end of the line and you need to be passing relative paths not absolute paths.

In your examples the destination and source path overlap because /some/path the same tree as /some/path/with/sourcefiles .
It only makes sense if the destination path is somewhere different.

This idea would preserve the directory structure under /some/path/with/sourcefiles in the destination tree. It also preserves file timestamps but not directory timestamps.
Check "cpio" syntax on your system as I can tell from your "find" command that yours is different from mine.

Code:
cd /some/path/with/sourcefiles
find . -type f -size -7M | cpio -pdmv /different/path2

# 6  
Old 02-25-2010
methyl:
Code:
find /sourcefiles -type f -size -7M | cpio -pdmv /home/some/path/testdir

worked, but I can't figure out how to copy updates only, didn't find it on <man cpio> or <info cpio> (I would've expected to use -u, but that's something different I think)

pludi:
I modified it slightly like this
Code:
find /sourcefiles -type f -size -7M -print | while read file
do
        dir=$(dirname $file)
        echo "mkdir -p /home/some/path/testdir"$dir
        echo "cp -uv "$file "/home/some/path/testdir"$dir
done

but I get the error:
Code:
dirname: extra operand `blah/blahblah/somefile.txt'
Try `dirname --help' for more information.

I must be getting close...
# 7  
Old 02-25-2010
The "copy updates only" requirement changes the specification somewhat. The "-u" switch to cpio will allow you to overwrite an existing file. This is not an "updates only" because every file is still copied.

How to copy "updates only"?
Initial feeling is to create a "timestamp" file on entry to the script and refer to the previous version of that "timestamp" file "prev_timestamp" in a "find -newer prev_timestamp" command. Obviously the first time through the script would have to be different from the second and subsequent times.

No idea what Operating System or Shell you have so specific syntax is not possible.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a particular directory in multiple file systems and calculate total size

Hello : I need some help in writing a ksh script which will find a particular directory in all the file systems in a server and finally report the total size of the direcotry in all the file systems. Some thing like this.. find /u*/app/oracle -type d -name "product" -prune and then... (1 Reply)
Discussion started by: Sam1974
1 Replies

2. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies

3. Shell Programming and Scripting

Find which dir is big size in a directory

Hi all, Could you please tellme the commadn which sorts the list of directories in a parent dir by their size. Thanks. (2 Replies)
Discussion started by: firestar
2 Replies

4. Shell Programming and Scripting

How to traverse directory structure and sum size of files?

How do I write a bash or ruby or perl or groovy script to print all the files in my directory tree that are one-to-two years old, the size of each file, and the sum of file sizes and then delete them? I was using find . -atime +365 -exec rm '{}' \; but the problem was that I could not... (5 Replies)
Discussion started by: siegfried
5 Replies

5. Shell Programming and Scripting

how to find the size of a directory alone

hi, i am using korn shell............... Any one please help me in solving the below question: question: i need to find the size of the directory alone... let us assume /root/kamal/hash1 is the directory, now i want to find the hash1 size .. ... (7 Replies)
Discussion started by: G.K.K
7 Replies

6. Shell Programming and Scripting

stop unix find on a directory structure after finding 1st occurrence

Hi, Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and... (3 Replies)
Discussion started by: jm0221
3 Replies

7. Shell Programming and Scripting

Find the size of a directory

Hi, I would really appreciate if you could help me with this. I have a directory structure like this :- /data Under data i have directories /data1 , /input_files , /output_files etc . Under these directories I have other subdirectories. What i am looking for is to find out the size of all... (5 Replies)
Discussion started by: divz
5 Replies

8. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

9. HP-UX

How find size of directory

Hi, How can find the size of the directory. If the directory has 1000 files. I want the total size of directory including all the files. the bdf command is just able to give only the volume size. It is not heling my cause. (2 Replies)
Discussion started by: truth
2 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question