One Liner to Rename Bunch of Files

One liner to rename bunch of files

If you happen to be using Linux, you may also have a perl script at /usr/bin/rename (sometimes installed as "prename") which can rename files based on more complex patterns than shell globbing permits.

The /usr/bin/rename on one of my systems is documented here. It could be used like this:

rename "s/pattern1/pattern2/" pattern1.*

A number of other Linux environments seem to have a different rename that might be used like this:

rename pattern1 pattern2 pattern1.*

Check man rename on your system for details.

One-liner Perl command to rename files


perl -MFile::Copy -we 'for (glob "*.csv") { my ($name) = /^(.+).csv/i; move($_, $name . ".out"); }'

To remove the header matching Data for:

perl  -MFile::Copy -MTie::File -wE 'for (glob '*x.csv') { tie my @file,
"Tie::File", $_ or die $!; shift @file if $file[0] =~ /^Data for/;
untie @file; my ($name) = /^(.*).csv/i; move($_, $name . ".out"); }'

But then it's really not a one-liner anymore...

use strict;
use warnings;
use Tie::File;
use File::Copy;
use autodie;

for (@ARGV) {
tie my @file, "Tie::File", $_;
shift @file if $file[0] =~ /^Data for/;
untie @file;
my ($name) = /^(.*).csv/i;
move($_, $name . ".out");
}

And use with:

$ script.pl *.csv

Rename multiple files in a folder, add a line counts as prefix (Powershell)

What you are doing is almost fine, the error comes from trying to concatenate an int with a string, PowerShell attempts type conversion of all elements to the type of the leftmost object in the operation:

The operation that PowerShell performs is determined by the Microsoft .NET type of the leftmost object in the operation. PowerShell tries to convert all the objects in the operation to the .NET type of the first object. If it succeeds in converting the objects, it performs the operation appropriate to the .NET type of the first object. If it fails to convert any of the objects, the operation fails.

Since the leftmost object in this case (the .Length property) is of the type int PowerShell attempts to convert the rest to int and it fails, for example:

PS /> 1 + 'A'
InvalidArgument: Cannot convert value "A" to type "System.Int32". Error: "Input string was not in a correct format."

This would be easily fixed by type casting the returned value to sring:

-NewName { [string](Get-Content $_).Length + "_" + $_.BaseName + $_.Extension }

Or for example using string formatting or the -f format operator:

-NewName { '{0}_{1}{2}' -f (Get-Content $_).Length, $_.BaseName, $_.Extension }

As for "the bonus", with string formatting see How do I control the number of integral digits?

In this case you can use {0:0000}, as an example:

(0..1000).ForEach({'{0:0000}' -f $_})

On the other hand, if you have many lengthy files, [System.IO.File]::ReadAllLines(...) is likely to be faster than Get-Content:

-NewName { '{0:0000}_{1}' -f [System.IO.File]::ReadAllLines($_).Length, $_.Name }
# OR
$io = [System.IO.File]
-NewName { '{0:0000}_{1}' -f $io::ReadAllLines($_).Length, $_.Name }

Multiple file renaming based on regex pattern + renaming within files based on new file names

This solved it for me:

  • File renaming e.g. as described here: https://superuser.com/questions/16007/how-can-i-mass-rename-files
  • Batch replacement with this VS Code plugin: https://marketplace.visualstudio.com/items?itemName=angelomollame.batch-replacer
  • since I had the said table (old vs. new name) prepared, a simple regex replacement of the table entries did the trick to meet the prerequisites for the plugin, i. e. the old names were replaced by replace "old_file_name" and the new names by with "new_file_name"; then, just copy & paste everything for this plugin as described there and all is replaced

Rename multiple files in cmd


for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"

If you use the simple for loop without the /f parameter, already renamed files will be again renamed.

Renaming multiple files in a directory using Python

You are not giving the whole path while renaming, do it like this:

import os
path = '/Users/myName/Desktop/directory'
files = os.listdir(path)


for index, file in enumerate(files):
os.rename(os.path.join(path, file), os.path.join(path, ''.join([str(index), '.jpg'])))

Edit: Thanks to tavo, The first solution would move the file to the current directory, fixed that.

Renaming bunch of files with xargs

I don't know why anyone would try to engage sed for this. Probably not xargs or seq, either. Here's a pure-Bash one-liner:

(x=1; for f in *.jpg; do mv "$f" "Something$((x++)).jpg"; done)

At its core, that's a for loop over the files you want to rename, performing a mv command on each one. The files to operate on are expressed via a single glob expression, but you could also name them individually, use multiple globs, or use one of a variety of other techniques. Variable x is used as a simple counter, initialized to 1 before entering the loop. $((x++)) expands to the current value of x, with the side effect of incrementing x by 1. The whole thing is wrapped in parentheses to run it in a subshell, so that nothing in it affects the host shell environment. (In this case, that means it does not create or modify any variable x in the invoking shell.)

If you were putting that in a script instead of typing it on the command line then it would be more readable to split it over several lines:

(
x=1
for f in *.jpg; do
mv "$f" "Something$((x++)).jpg"
done
)

You can type it that way, too, if you wish.

One Line PowerShell Batch Rename based on File Contents

1) remove duplicates using -List switch in Select-String
2) you need to really pipe the objects into the for loop

Try this?

Select-String -Path .\*.out -pattern 'ST~867' -SimpleMatch -List | Select-Object Path | ForEach-Object { Rename-Item $_.path ($_.path -replace 'out$','867.out') } 


Related Topics



Leave a reply



Submit