BreadCrumbs: Bash Rename File Extensions

Bash Rename File Extensions

From Luke Jackson

(Difference between revisions)
Jump to: navigation, search
Revision as of 19:47, 26 October 2008 (edit)
Ljackson (Talk | contribs)

← Previous diff
Current revision (21:37, 28 October 2018) (edit)
Ljackson (Talk | contribs)
(Syntax)
 
Line 18: Line 18:
for i in *.JPG; do mv $i ${i%%.JPG}.jpg; done for i in *.JPG; do mv $i ${i%%.JPG}.jpg; done
 +
 + for f in *.jpg; do mv "$f" "${f%.jpg}"; done
If you would like to use this function for other types of files here is a basic template: If you would like to use this function for other types of files here is a basic template:
Line 23: Line 25:
for i in *.<EXISTING_EXT>; do mv $i ${i%%.<EXISTING_EXT>}.<RESULTING_EXT>; done for i in *.<EXISTING_EXT>; do mv $i ${i%%.<EXISTING_EXT>}.<RESULTING_EXT>; done
-Another example+Another examples
for i in *.JPG; do j=`echo $i | cut -d . -f 1`; j=$j"_from_mark.jpg"; echo $j; mv $i $j; done; for i in *.JPG; do j=`echo $i | cut -d . -f 1`; j=$j"_from_mark.jpg"; echo $j; mv $i $j; done;
 +
 + for i in *.py; do cp "$i" "${i%-*}.py"; done
 +
 + for i in *.py; do mv "$i" "${i%.py}-json.py"; done
 +
 +String Operations
 +
 + for i in *; do mv $i ${i#1440.jpg.*}.jpg; done
 +
 + printf "%s\n" img{00{1..9},0{10..99},{100..999}}.png
 +
 +Basic Example (Files without File Extensions)
 +
 + for f in *; do mv "$f" "$f.jpg"; done
 +
[[Category:Mac OS X]] [[Category:Mac OS X]]
[[Category:Linux]] [[Category:Linux]]

Current revision

Contents

Summary

This is a simple function to quickly rename many files using Bash. It can be very useful for renaming pictures, mp3s, and movies.

Supported Operating Systems

  • Mac OS X and Terminal
  • Unix
  • Linux

Requirements

  • Bash

Syntax

Below is a simple example for renaming uppercase jpg files to lowercase:

for i in *.JPG; do mv $i ${i%%.JPG}.jpg; done
for f in *.jpg; do mv "$f" "${f%.jpg}"; done

If you would like to use this function for other types of files here is a basic template:

for i in *.<EXISTING_EXT>; do mv $i ${i%%.<EXISTING_EXT>}.<RESULTING_EXT>; done

Another examples

for i in *.JPG; do j=`echo $i | cut -d . -f 1`; j=$j"_from_mark.jpg"; echo $j; mv $i $j; done;
for i in *.py; do cp "$i" "${i%-*}.py"; done
for i in *.py; do mv "$i" "${i%.py}-json.py"; done

String Operations

for i in *; do mv $i ${i#1440.jpg.*}.jpg; done
printf "%s\n" img{00{1..9},0{10..99},{100..999}}.png

Basic Example (Files without File Extensions)

for f in *; do mv "$f" "$f.jpg"; done
Personal tools