Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have thousands of files whose names are of the form:

sequence_1_0001.hmf  
sequence_1_0002.hmf  
sequence_1_0003.hmf
...  

sequence_1_0001.jpg  
sequence_1_0002.jpg
sequence_1_0003.jpg
...  

sequence_2_0001.hmf  
sequence_2_0002.hmf  
sequence_2_0003.hmf
...  

etc..

I'm using sftp to transfer only the files whose suffix and prefix are 'sequence_1' and '.hmf', respectively. However, I don't want to transfer all the files that match this format. Instead, I want to transfer every hundreth file. That is:

sequence_2_0001.hmf  
sequence_2_0101.hmf  
sequence_2_0201.hmf
...  

Is there a way to do this?

share|improve this question
    
For your specific case, wouldn't 'sequence_1' and '01.jpg' as prefix and suffix work? –  Deepak Mittal Apr 26 '12 at 14:26
    
@DeepakMittal: I'm not sure what you mean... could you elaborate? –  Paul Apr 26 '12 at 14:28
3  
You said you're using sftp with prefix and suffic as 'sequence_1' and '.jpg' respectively. So just change suffix to '01.jpg' and it would work, right?. The only accepted files then would be sequence_1_xx01.jpg, where xx can be any number/character. –  Deepak Mittal Apr 26 '12 at 14:31
    
You may want to double-check your copy pasted sections for consistency since some of it still mentions .hmf and the wrong sequence_ prefix. –  jw013 Apr 26 '12 at 14:32
    
@jw013: Thanks for the heads up... I fixed the question so that it has 'hmf', as I meant to put :) –  Paul Apr 27 '12 at 13:06

2 Answers 2

up vote 2 down vote accepted

Your examples and description are inconsistent. Going by the list sequence_2_0001.hmf, sequence_2_0101.hmf, sequence_2_0201.hmf, …, you can use the ? wildcard to match any one character.

get sequence_2_??01.hmf

You can use character sets to match one file every 20 (for example). [02468] matches any one of the digits 0, 2, 4, 6 or 8.

get sequence_2_[02468]?01.hmf
share|improve this answer

You can generate the file list with something like this:

printf %s\\n sequence_[0-9]*.jpg | sort -t_ -k2n | sed -n '1~100p'
share|improve this answer
    
When possible, it's best to avoid making assumptions about file names, but otherwise. Assumptions such as file names being newline delimited should be clearly advertised. first~step is a GNU extension to sed line-addressing, and not widely portable. The -n in sort is not useful because the file names here are prefixed by a string and the numeric portions are properly padded anyways. The find will likely go deeper than the OP intended. In this particular special case, the approach in @Gilles' answer and @DeepakMittal's comment is much more efficient and robust. –  jw013 Apr 27 '12 at 2:32
    
@jw013, the OP specifically mentioned the filename template. I intended sort -n to sort sequence_1, sequence_2 ... sequence_10 correctly, but it should have been sort -t_ -k2n. Other than that I agree with your comments. –  Thor Apr 28 '12 at 18:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.