Shell/Bash Shortcut For Bulk Renaming of Files in a Folder

Shell/Bash shortcut for bulk renaming of files in a folder

I would suggest something like this:

for i in *-doc-*.txt; do mv "$i" "${i/*-doc-/doc-}"; done

${i/*-doc-/doc-} replaces the first occurrence of *-doc- with doc-.

If you need to do more than one replacement (see comment number 1), you need to use the ${var//Pattern/Replacement} variant. If you need to replace the beginning of the name you need to use ${var/#Pattern/Replacement}, if you need to replace the end (ie: the extension) you need to use the ${var/%Pattern/Replacement} form.

See Shell Parameter Expansion for more details. This expansion is bash specific.

Rename multiple files in a folder, add a prefix (Windows)

Option 1: Using Windows PowerShell

Open the windows menu.
Type: "PowerShell" and open the 'Windows PowerShell' command window.

Goto folder with desired files: e.g. cd "C:\house chores"
Notice: address must incorporate quotes "" if there are spaces involved.

You can use 'dir' to see all the files in the folder. Using '|' will pipeline the output of 'dir' for the command that follows.

Notes: 'dir' is an alias of 'Get-ChildItem'. See: wiki: cmdlets.
One can provide further functionality. e.g. 'dir -recurse' outputs all the files, folders and sub-folders.

What if I only want a range of files?

Instead of 'dir |' I can use:

dir | where-object -filterscript {($_.Name -ge 'DSC_20') -and ($_.Name -le 'DSC_31')} |

For batch-renaming with the directory name as a prefix:

dir | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}

Option 2: Using Command Prompt

In the folder press shift+right-click : select 'open command-window here'

for %a in (*.*) do ren "%a" "prefix - %a"

If there are a lot of files, it might be good to add an '@echo off' command before this and an 'echo on' command at the end.

Script to rename files

I need about 2 minutes to write such script for *NIX systems (may be less), but for Windows it is a long song ... ))

I've write simple VBS script for WSH, try it (save to {script-name}.vbs, change Path value (on the first line of the script) and execute). I recommend to test script on small amount of data for the first time just to be sure if it works correctly.

Path = "C:\Users\rootDirectory"
Set FSO = CreateObject("Scripting.FileSystemObject")

Sub visitFolder(folderVar)
For Each fileToRename In folderVar.Files
fileToRename.Name = "Agreement " & fileToRename.Name
Next
For Each folderToVisit In folderVar.SubFolders
visitFolder(folderToVisit)
Next
End Sub

If FSO.FolderExists(Path) Then
visitFolder(FSO.getFolder(Path))
End If

Bash script for renaming files and moving into different folder

You can achieve this by using a counter and a for..in..do...done loop.

Here's an example:

#!/bin/bash

FILES=./files/*
TARGET=~/Desktop/dist # make sure that folder exists!
COUNTER=1 # if you want to start from 0, just put 0 there
for f in $FILES
do
echo "Processing $f file..."
mv $f $TARGET/$COUNTER"_"${f##*/}
let "COUNTER++"
done

How to replace all spaces by underscores in all file names of a folder?

A one liner

cmd /e:on /v:on /c "for %f in ("* *.exe") do (set "n=%~nxf" & set "n=!n: =_!" & ren "%~ff" "!n!" )"

Spawn a cmd instance, with extensions and delayed expansion enabled, and for each exe file with spaces in name, replace spaces with underscores and rename the file with the new name

Unix Renaming Files

The answer Ciprian gave is certainly an option but I feel it's limiting.
The solution below is much more flexible as you don't have to actually count anything and you can remove text from any position rather than just the end.

The following command (1 line) will remove any mention of .temp in all the files:

for filename in *; do mv "$filename" "${filename//.temp/}"; done

Note The "*" means all files in current folder. You can use *.temp to achieve exactly the same result as Ciprian's method. (that is, only removing .temp from files ending with .temp)



Related Topics



Leave a reply



Submit