Recursive copy to a RANDOM name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Recursive copy to a RANDOM name
# 1  
Old 10-17-2008
Recursive copy to a RANDOM name

Dear Group,

after trying numerous suggestions and racking my brain I cannot manage something which seems so simple.

Essentiallly, I would like to perform a recursive copy to a destination but give it a random name.

I assumed (incorrectly) that the following would work:

cp -r /destination_folder/$RANDOM

but I only get the response: cp: missing destination file operand after `/destination_folder/19977'

Can someone with a bigger brain than mine please help?

Many thanks,

Roki
# 2  
Old 10-17-2008
cp takes two arguments
cp fromdirectory/file todirectory/file

There is also a problem with what you are doing. If you were to try this, say a few hundred times, you would be likely to get duplicates. In other words $RANDOM is random and will produce the same value again sometime in the future.
# 3  
Old 10-17-2008
Hi Jim,

Many thanks for the reply.

You are right in saying that I would get duplicates...I now remember why I was trying to get the original filename next to the $RANDOM variable. I gave up after many attempts.
There are thousands of files in these directories.

Does this mean that the only easy way to do this is by writing a bash script?:

#! /bin/bash

for filename in `find . -type f`; do

cp $filename /destination_folder/$filename.$RANDOM

done


when I try to run this it just hangs.

Any suggestions?

Thanks,

R
# 4  
Old 10-17-2008
If there are thousands of files your script will run a while. If there are sub-directories on the source side, you will also get errors.

What are you trying to accomplish? It looks like you are just duplicating a directory.
# 5  
Old 10-17-2008
Yes I there are subirectories on the source side.

I'm trying to clean up thousands of image files which I have collected over the years. Unfortunately I haven't been keeping track of what copies I have where. It is very likely that there are duplicates of files spread out.

My goal is to consolidate these files into one directory and remove duplicates using md5sum.

The folder structures they are in have been copied over from Windows systems which means that there is white space in some of the path names (and possibly other characters) which may make copying them less than straightforward. This is why I thought of doing a recursive copy to one directory and incorporating a random name to the file would help.

If you have an easy way to do this, I would greatly appreciate hearing it.

I've tried experimenting with bash scripts but none have worked (which probably says more about my scripting skills than anything else.)

Thanks
# 6  
Old 10-17-2008
Count the files so we know how many:
find . -type f | wc -l


Post a few sample filenames showint desired transformation:

grand canyon.gif -> 000123456.gif
or
grand canyon -> 00001112233

or whatever. There are ways to to get unique random numbers, but do they really need to be random? Naming them 1, 2, 3, etc is a snap.
# 7  
Old 10-17-2008
Hello Perderabo,

the total file count with the command is 88127.

The typical folder/file structure is:

with spaces
./cdell/Documents and Settings/Roki/My Documents/Exported Pics/images/Fl_0112.jpg

or with square brackets
./epd/Pics/Chris/Temp Folder/665109_7_t[1].jpg

To be honest, the numbers don't need to be random at all. The only reason I suggested random number is that that way I would be sure not to have a duplicate filename if concatenated with an actual filename.

The goal would be to have all the 88127 files inside one folder so that I can easily remove duplicates based on md5 hash.

I hope I've explained myself clearly.

Many thanks again.

Roki.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Recursive copy of Folders with files

Dear All, I will appreciate any help received. Our system is running on hpux v1 My problem is as follows: We have many customer folders with name fd000100, fd000101 and so on e.g. (Testrun)(testsqa):/>ll /TESTrun/fd000100 total 48 drwxrwx--- 2 fq000100 test 96 Jun 27 2004... (17 Replies)
Discussion started by: mhbd
17 Replies

2. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

3. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

4. UNIX for Dummies Questions & Answers

recursive copy into a directory and all its subdirectories...

I want to copy a file from the top directory into all the sub-folders and all of the sub-folders of those sub-folder etc. Does anyone have any idea how to do this? Thanks in advance of any help you can give. (3 Replies)
Discussion started by: EinsteinMcfly
3 Replies

5. Shell Programming and Scripting

recursive search and copy

Hello again. Well, I need help again sooner as I thought. Now I want to search for files with a known name within all subdirs, and copy the to differently named files in the same directory. For example if I had only one file to copy, I would just usecp fileName newFileNamebut to do this... (1 Reply)
Discussion started by: cabaciucia
1 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. Shell Programming and Scripting

scp : recursive copy doesn't work

Hi I have a problem with scp command : I try to copy a list of files from source to destination, it works fine but if there is a directory not existing on destination, it doesn't automatic create. Syntax : scp -rp /<PATH>/<NEW_DIR>/<FILE> <USER_DEST>@<HOST_DEST>:<PATH>/<NEW_DIR>/<FILE>... (5 Replies)
Discussion started by: madmat
5 Replies

8. UNIX for Dummies Questions & Answers

help with recursive copy command

Hi Guys, I am experiencing a problem right now while copying a directory as well as its subdirectories to my target directory. I know this is a very simple UNIX command using cp -R source directory target directory. but unfortunatley while doing this an error comes up on the command line saying... (2 Replies)
Discussion started by: Knowledge_Xfer
2 Replies

9. Programming

recursive copy of the directory

I want to copy a directory recursively ( it again has directories) and the directory is on windows and is nfsmounted in vxWorks, i am using unix to develop the code for this, can any one suggest me how to copy the directories recursively. (7 Replies)
Discussion started by: deepthi.s
7 Replies

10. UNIX for Advanced & Expert Users

recursive copy of hidden files

I need to know how to copy hidden files recursively? cp -r sourceDir/* targetDir/. ignores the hidden files. Thank you!! (2 Replies)
Discussion started by: usfrog
2 Replies
Login or Register to Ask a Question