using gsed with cp to sort files in directory - every N file copy to new place


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using gsed with cp to sort files in directory - every N file copy to new place
# 1  
Old 10-12-2011
using gsed with cp to sort files in directory - every N file copy to new place

Hi all,

I'm having a problem with some basic piping issues...

I have been able to get in a directory and ls | gsed in order to list every N file for instance:

Code:
ls | gsed -n '2~5p'

The thing is I want to be able to copy the output files to a new directory. Basically directory /all has a bunch of jpegs in there. I need to copy every third one, starting at #2 to /selects.

Can some kind person point me on the way with this. I am on OS X but installing gsed as the OS X sed seems inconsistent with the examples I am using to get the initial output...

THANK YOU

Last edited by radoulov; 10-12-2011 at 04:20 PM.. Reason: Code tags!
# 2  
Old 10-12-2011
OSX's default sed is an ancient BSD incarnation which doesn't even support things like \n. gsed is a good idea.

I don't know how "2~5p" is supposed to work though, trying it on /etc/passwd I actually get more lines out of it than I put in!

How about
Code:
ls | awk '(NR%3)==0' | while read LINE
do
        echo cp "$LINE" /path/to/dest/
done

One of the simplest ways to use awk is an expression which tells it whether to print the current line or not. NR is the number of records. So it will print the current line only when the line number divided by 3 has a remainder of 0 -- so line 3, 6, 9, 12, ...

Then you feed it into a while loop and move files one by one.

If you wanted to avoid running cp 30,000 times, you could pipe them into xargs/tar:

Code:
ls | awk '(NR%3)==0' | xargs -I {} tar -cf - | tar -C /path/to/dest/ -xf -

running 'tar -cf - file1 file2 file3' bundles files into a tape archive printed directly to standard output. xargs converts file names in a pipeline into arguments fed into tar. Piping that directly into tar -xf extracts the files. -C tells it where. So it pipes the contents of all the files between the two tars and creates them where you want.
# 3  
Old 10-12-2011
Thank you so much Corona688 - that really helped me get moving this morning. Used your second example with perfect results.
# 4  
Old 10-12-2011
Just beware that if there are too many files, tar will get run more than once, causing the pipe to break early, so some may not be copied. My first example is technically safer, if less efficient.
# 5  
Old 10-12-2011
ok, good to know. this was an input of about 750 files, outputting 250 and from what I can tell at first glance seems to have worked well. I'll keep an eye on it. Thank you again for the help and lesson.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

2. Shell Programming and Scripting

Search for a file in all directories and place the file in that directory

Hi All, Daily I am getting the updated file. I have to search for this file in all directories and sub directories. If the file existed in a particular directory then move this updated file to that particular directory. If the file is not existed in any of the directories then place this... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. UNIX for Dummies Questions & Answers

How to search for a file having a particular character in a particular place in a directory.?

Hi Guys, I want to search for a specific file in a directory which have a "b" letter as the 3rd character in the name of the file. For Example : /abc/efg/ldbjfblkj.sh /abc/efg/erublkd.sh /abc/efg/eibueora.sh /abc/efg/kfvnmnb.sh Since we have 2 files with "b" as a 3rd character in... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

5. Shell Programming and Scripting

Copy files listed in text file to new directory

I am trying to write a script that will copy all file listed in a text file (100s of file names) to a new directory Assume script will run with main as current working directory and I know how many files/lines will be in List.txt Im trying to work up a test script using this model Contents of... (2 Replies)
Discussion started by: IAmTheGrass
2 Replies

6. UNIX for Dummies Questions & Answers

regularly copy files to different directory with different file extentions

I have a NAS and I upload my videos to it from time to time. the video format is 3gp but my media player PBO could only play avi. I want to copy these files to different directory and change the extention from 3gp to avi. (yes I want to keep the original version and create a duplicate version with... (0 Replies)
Discussion started by: momentum
0 Replies

7. UNIX for Dummies Questions & Answers

Copy dir/file from one place to another.

Hello all. I'm not getting the hang of Paths. I have a dir w/files that I want to copy to another dir. Right now I am in the "source" directory. I want to copy it to Ch7. "cp -r source Ch7". Ch7 was already created. 1st msg.: cannot stat `source`: No such file or dir. I typed pwd & got... (3 Replies)
Discussion started by: Ccccc
3 Replies

8. Solaris

What is the best way to copy data from place to another place?

Dear Gurus, I need you to advice or suggestion about the best solution to copy data around 200-300G from serverA(location A) to serverB(location B). Normally, I will share folder and then copy but it takes too long time(about 2 days). Do you have any suggestion or which way should be... (9 Replies)
Discussion started by: unitipon
9 Replies

9. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies

10. Solaris

Copy files from the file to another directory

I have created a file that has list of all the files I want to copy into another directory.Is there a way to do it? Thanks In advance (4 Replies)
Discussion started by: shreethik
4 Replies
Login or Register to Ask a Question