33

In zsh, when I have to create a bunch of files with zsh, I usually do something like:

for x in $(seq 1 1000); do .....; done

This works fine, it gives me files with names foo-1.txt .. foo-1000.txt.
However, these files do not sort nicely, so I would like to zero-pad the $x variable, thus producing names of foo-0001.txt .. foo-1000.txt.

How to do that in zsh? (and bonus question, how to do it in bash?)

Mat
  • 202,337
  • 40
  • 393
  • 406
ervingsb
  • 653
  • 8
  • 9

6 Answers6

35

For reference sake, if you do not have control over the generation of the numeric values, you can do padding with:

% value=314
% echo ${(l:10::0:)value}
0000000314
% echo $value
314
Francisco
  • 3,980
  • 1
  • 23
  • 27
  • 5
    Seeing that ${(l... is not very search-engine friendly, look this up under Parameter Expansion Flags in zshexpn(1). Also paired delimiters can be used to make the syntax a bit more intuitive: ${(l{10}{0})value}. – Roman Odaisky Jul 22 '13 at 20:57
  • 1
    Strangely, zmv doesn’t understand that form, though parentheses work fine: ${(l(10)(0))value}. – Roman Odaisky Jul 22 '13 at 21:11
  • echo ${(l:3::0:)$((++value))} – zzapper Jan 27 '22 at 13:39
  • 1
    a strange caveat: if the value is longer than the given length the first digits are cut of, e.g. for l=3 and a value of 1234 the output is 234. – A. Rabus Jul 29 '22 at 07:50
19

Use the -w flag to seq (in any shell):

$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10
Mat
  • 202,337
  • 40
  • 393
  • 406
12

You can use bash's brace expansion:

$ for n in file-{0001..1000}.txt; do echo $n; done
file-0001.txt
file-0002.txt
file-0003.txt
file-0004.txt
file-0005.txt
file-0006.txt
file-0007.txt
file-0008.txt
file-0009.txt
file-0010.txt
...
file-0998.txt
file-0999.txt
file-1000.txt

Works in zsh too.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
9

in Zsh:

% my_num=43
% echo ${(l(5)(0))my_num}
00043
Konstantin Nikitin
  • 2,256
  • 1
  • 17
  • 12
2

Works in bash (don't know for zsh):

echo foo{0000..1000}.txt 
user unknown
  • 35,537
  • 11
  • 75
  • 121
1
  1. printf
    Using printf to pad with zeros works in any shell:

    $ printf 'foo%03d.txt\n' $(seq 4)
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt
    
    $           printf  'foo%03d.txt\n' {1..4}       # works in bash, ksh, zsh.
    $ n=4;      printf  'foo%03d.txt\n' {1..$n}      # only ksh, zsh
    $ n=4; eval printf '"foo%03d.txt\n" {1..'"$n"'}' # workaround needed for bash
    
  2. Use brace expansion (only bash and zsh keep the leading zeros ksh don't).

    $ printf '%s\n' foo{1..004}.txt
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt