How To Rename Files Inwards Bulk

Consider this mutual scenario. Influenza A virus subtype H5N1 directory contains multiple files that are named using a mutual convention: for example, image-001.png, image-002.png, image-003.png, etc. You desire to rename the files to, say, upload-001.png, upload-002.png, upload-003.png, etc.

The coders amid us tin write a bash script to automate the process. For expedience, this post service shows how to role the built-in rename ascendancy to laissez passer the same goal.

The next ascendancy replaces the commencement occurrence of image amongst upload inward the file lift for all PNG files inward the electrical current directory.

$ rename -v 's/image/upload/' *.png image-001.png renamed equally upload-001.png image-002.png renamed equally upload-002.png image-003.png renamed equally upload-003.png

Notes:

  • -v

    Produce verbose output, listing each file amongst its onetime as well as novel names.

  • 's/image/upload/'

    Provide a valid PERL aspect to alteration the file names.

  • *.png

    Specify the files to hold upward renamed.

The total ability of PERL expressions is at your disposal to specify the file renaming rule. Influenza A virus subtype H5N1 thorough explanation of PERL expressions is beyond the compass of this post. The next alone samples roughly useful PERL expressions.

  • Global replace.

    If y'all desire to supersede ALL occurrences of a designing inward a file name, y'all must specify a global replace. For example, y'all desire to supersede all spaces inward the file names amongst null, that is, delete all spaces.

    $ rename -v 's/ //g' *.pdf Boarding Pass - Derek 2014-05-19.pdf renamed equally BoardingPass-Derek2014-05-19.pdf Boarding Pass - Erin 2014-05-19.pdf renamed equally BoardingPass-Erin2014-05-19.pdf capture_still_frame (1).pdf renamed equally capture_still_frame(1).pdf capture_still_frame (2).pdf renamed equally capture_still_frame(2).pdf
  • Match whatever grapheme inward a list.

    Suppose y'all desire to supersede all occurrences of spaces, underscores, as well as circular brackets amongst the dash character. Here is how.

    $ rename -v 's/[_ ()]/-/g' *.pdf

    Any grapheme inside the foursquare brackets is matched, as well as replaced yesteryear a dash.

  • Back reference variables.

    Suppose the file names follow this convention: prefix-sequence.suffix. For instance, usa-001.png. My chore is to switch the lodge of the prefix as well as the sequence number, resulting inward names such equally 001-usa.png. You tin laissez passer the lawsuit yesteryear using dorsum reference variables equally follows:

    $ rename -v 's/(.+)-([0-9]{3})/$2-$1/' *.png india-003.png renamed equally 003-india.png uk-002.png renamed equally 002-uk.png usa-001.png renamed equally 001-usa.png

    The to a higher house illustration has ii dorsum reference variables separated yesteryear a dash: $1 as well as $2. The commencement variable $1 is defined yesteryear (.+), which agency 1 or to a greater extent than characters. The minute variable $2 is defined yesteryear ([0-9]{3}), which agency precisely iii digits, each digit beingness from 0 to 9. The replacement pattern, $2-$1, specifies that $2 directly comes earlier $1, amongst a dash inward between.

To avoid potential mistakes, y'all tin commencement create a mock run of the rename ascendancy earlier genuinely running it for real. Specify the -n option, which stands for 'no-act'.

$ rename -n 's/image/upload/' *.png image-001.png renamed equally upload-001.png image-002.png renamed equally upload-002.png image-003.png renamed equally upload-003.png

If all the suggested replacements await reasonable, thence become ahead to run the ascendancy without the -n.

Berlangganan update artikel terbaru via email:

0 Response to "How To Rename Files Inwards Bulk"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel