LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
LinkBack Search this Thread
Old 03-07-2013, 07:49 AM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Rep: Reputation: 30
copy a random file from one location to another


ok i have a task i want to do but am not sure on where to even start with this one so will try to explain what i want to do

i have a folder full of random files and some start with (for example) 'test - ' (test - something random.ext etc) i want to pick one of those at random and copy it to another location

any ideas?
 
Old 03-07-2013, 07:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
There is a program called rl that can randomize lines (and more).

This would solve the random file problem using rl:
Code:
ls test* | rl -c 1
I know for sure rl is part of the Debian repo and its also easy to compile yourself (rl home)
 
Old 03-07-2013, 07:57 AM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
There is a program called rl that can randomize lines (and more).

This would solve the problem using rl:
Code:
ls test* | rl -c 1
I know for sure rl is part of the Debian repo and its also easy to compile yourself (rl home)
fast reply ty

i get 'rl: command not found' with that and have been using find and shuf and have this so far:

Code:
find . -iname "test*.ext" -print | shuf -n1
only problem is i don't know how to make it copy it to another location

Last edited by steve51184; 03-07-2013 at 08:00 AM.
 
Old 03-07-2013, 08:03 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by steve51184 View Post
i get 'rl: command not found'
install the program......

Quote:
with that and have been (so far) using this find and shuf and have this so far:

Code:
find . -iname "test*.ext" -print | shuf
only problem is i don't know how to pick one of the random lines it out puts and to then copy it to another location
This picks one file using shuf:
Code:
find . -iname "test*.ext" -print | shuf -n 1
The following copies the file:
Code:
cp $(find . -iname "test*.ext" -print | shuf -n 1) /tmp
 
Old 03-07-2013, 08:08 AM   #5
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
got an error with that:

Quote:
# cp $(find /some/folder -iname "test*.ext" -print | shuf -n1) "/some/folder/test - random.ext"
cp: missing destination file operand after `/some/folder/test - random.ext'
Try `cp --help' for more information.
 
Old 03-07-2013, 08:12 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
I assume from the above error that the file in question has spaces in it?

Try this:
Code:
cp "$(find . -iname "test*.ext" -print | shuf -n 1)" /tmp
 
Old 03-07-2013, 08:14 AM   #7
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
got it working thank you

Last edited by steve51184; 03-07-2013 at 08:17 AM.
 
Old 03-07-2013, 08:18 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Ok, without knowing what the input looks like we keep guessing.

Post the following:
Code:
find . -iname "test*.ext" -print | tail -n 10


What did you do to fix it?
 
Old 03-07-2013, 08:24 AM   #9
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
it needed the first folder name to end in a /

Code:
cp "$(find /some/folder/ -iname "test*.ext" -print | shuf -n1)" "/some/folder/test - random.ext"
^ is the final working code
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to copy tar file to other location daniesh Solaris / OpenSolaris 20 05-03-2011 06:53 AM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
copy in terminal from smb location Byenary Linux - Networking 5 09-29-2006 05:09 AM
Variable at random location in memory? Ephracis Linux - Software 2 08-27-2006 05:49 AM
Installation Freezes at random points during copy. idrise Fedora - Installation 1 11-02-2005 04:27 AM


All times are GMT -5. The time now is 03:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration