Tag Info

New answers tagged

0

You are not using regular expressions correctly. tes* means te with any number of ss following it, so test-file-1 will be renamed to file 1t-file-1: $ rename -n 's/tes*/file 1/' * test-file-1 renamed as file 1t-file-1 Similarly, ^* will match the empty string appearing at the start, so in effect it's like ^, but with a an inifinite loop: $ rename -n ...


2

To squash multiple hyphens (one hyphen followed by one or more hyphens) into a single one for all files in the current directory use: rename 's/--+/-/g' -- * The -- is important if files start with a hyphen, otherwise they would be interpreted as command line arguments. The * expands to the list of files in the current directory.


0

In bash: # renaming files in the current directory only for f in *.MP3; do mv "$f" "${f%.MP3}.mp3"; done # renaming files in subdirectories as well for f in *{,/*}.mp3; do mv "$f" "${f%.MP3}.mp3"; done


1

Using Perl's rename : -2 : $ rename -n 's@\b\d+\b@sprintf("%04d", $& - 2)@e' 0100.png 0100.png -> 0098.png +1 : $ rename -n 's@\b\d+\b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png 0001.png -> 0002.png 0002.png -> 0003.png You can remove the -n (dry-run mode switch) when your tests become valids. There are other tools with the same ...


3

I would probably end up using temporary directory in this case: for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#${file%.png}+1))).png done The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers. For example: $ touch 0001.png 0002.png 0010.png 0020.png 0100.png ...


2

The solution for (1) Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible. ls *.png | sort -rn | while read ...; do ... mv ...; done The solution for (2) Determine the number of digits (if it not the same for all files) and then use printf for keeping that length: printf ...


2

rename works this way because it can move files between directories. Like mv, it acts on the whole path, not just on the last component. If you only want to modify the last component, you can anchor your regexp at (\A|?<=/), and make sure that it doesn't match any / and only matches at the last /. rename 's~(\A|?<=/)(?=[^/]*)\z~assi_~' ...


1

Just increment a variable at each iteration. Since rename sets use strict, you'll need to use a global variable $::i (or use no strict, or declare the variable with our). rename 'our $i; s/.*/gg_$i/; ++$i' * If you want to pad the number with leading zeroes, count the number of files. The list of files is in @ARGV. rename 'our $i; my $width = ...


3

Provided that there is an absence of ' single-quotes in the filenames, you might do this like: i=0; set -- for f in * do set -- "$@" "$f" gg "$((i+=1))" "${f#"${f%.*}"}" done printf "mv '%s' '%s%03d%s'\n" "$@" | sh That way if a filename contains a . then it and whatever follows it is retained, else that field is empty and printf prints nothing there. I ...


6

You don't really need the rename command for this, you can do it directly in the shell: c=0; for i in *; do let c++; mv "$i" "gg_$(printf "%03d" $c)${i#"${i%.*}"}"; done The printf "%03d" $c will print the $c variable, padding it with 3 leading 0s as needed. The let c++ increments the counter ($c). The ${i#"${i%.*}"} extracts the extension. More on that ...


2

This one worked for me. (But the rename command used here is a perl rename). ls -1 -c | xargs rename -n 's/.*/our $i; sprintf("gg%04d", $i++)/e' However, the above assumes there are no files that have spaces in their names. Otherwise it breaks. Also, parsing ls output is really not a good idea. However, since it breaks when there are spaces on file ...


0

try ls *.pnm | awk -F. '{printf "mv %s img%d.pnm\n",$0,NR;}' | bash where ls *.pnm | make a list of pnm's file awk -F. '{printf "mv %s img%d.pnm\n",$0,NR;}' launch awk -F. use . as separator '{printf "mv %s img%d.pnm\n",$0,NR;}' print instruction to move and number file. | bash instruction are give to bash. you can even preview with ls *.pnm | awk ...


2

Try the following... unset i; for f in *.pnm; do mv "$f" "img$((++i)).pnm";done



Top 50 recent answers are included