Pad file names with zeroes to improve sorting
This is actually just about renaming files using regular expressions (regex) to match files to rename. The specific use case was just so they sort better. Turned out I already had a similar little a post on the subject (rename files with file names matching regular expression). Anyway, the method below is a slight variation on the previous post because in this case I was renaming directories whose names consisted purely of digits.
Here's a list files (directories, actually) before:
$ ls
1 106 113 120 2 27 34 41 49 56 63 70 78 85 92
10 107 114 13 20 28 35 42 5 57 64 71 79 86 93
100 108 115 14 21 29 36 43 50 58 65 72 8 87 94
101 109 116 15 22 3 37 44 51 59 66 73 80 88 95
102 11 117 16 23 30 38 45 52 6 67 74 81 89 96
103 110 118 17 24 31 39 46 53 60 68 75 82 9 97
104 111 119 18 25 32 4 47 54 61 69 76 83 90 98
105 112 12 19 26 33 40 48 55 62 7 77 84 91 99
First, pad files 1-9 with two zeroes (e.g. rename 1 to 001):
$ ls | egrep "^[1-9]{1}$" | xargs rename 's/^/00/'
Result:
$ ls
001 009 106 113 120 20 28 36 44 52 60 68 76 84 92
002 10 107 114 13 21 29 37 45 53 61 69 77 85 93
003 100 108 115 14 22 30 38 46 54 62 70 78 86 94
004 101 109 116 15 23 31 39 47 55 63 71 79 87 95
005 102 11 117 16 24 32 40 48 56 64 72 80 88 96
006 103 110 118 17 25 33 41 49 57 65 73 81 89 97
007 104 111 119 18 26 34 42 50 58 66 74 82 90 98
008 105 112 12 19 27 35 43 51 59 67 75 83 91 99
Then, pad files 10-99 with one zero (e.g. rename 10 to 010):
$ ls | egrep "^[1-9]{1}[0-9]{1}$" | xargs rename 's/^/0/'
Result:
$ ls
001 011 021 031 041 051 061 071 081 091 101 111
002 012 022 032 042 052 062 072 082 092 102 112
003 013 023 033 043 053 063 073 083 093 103 113
004 014 024 034 044 054 064 074 084 094 104 114
005 015 025 035 045 055 065 075 085 095 105 115
006 016 026 036 046 056 066 076 086 096 106 116
007 017 027 037 047 057 067 077 087 097 107 117
008 018 028 038 048 058 068 078 088 098 108 118
009 019 029 039 049 059 069 079 089 099 109 119
010 020 030 040 050 060 070 080 090 100 110 120
If I wanted to pad to support four digit numbers, I'd add a zero to each of the above examples and do the following for to pad three digit numbers with a zero (BTW, the rename utility includes a helpful --no-act flag to show what would be done without doing it):
$ ls
0001 0010 1000 1111 300 555 999
$ ls | egrep "^[1-9]{1}[0-9]{2}$" | xargs rename -n 's/^/0/'
300 renamed as 0300
555 renamed as 0555
999 renamed as 0999
If I ran the above without the "-n" flag, I'd have:
$ ls
0001 0010 0300 0555 0999 1000 1111
Another long post on a trivial matter. Probably not worth the time, though here it is anyway in case it might help someone. I know when I'm learning something new, it's really helpful to have simple, even if redundant, examples for things to click.
Leave a comment