How to batch rename files from command line? (Solved)

Using applications, configuring, problems
Message
Author
april

How to batch rename files from command line? (Solved)

#1 Post by april »

rename 's/%20/ /g' *.mp4

I'm trying to rename a bunch of files in one directory
opening a terminal in the directory and issuing the command gives no change and no errors

Anyone know whats going on here?
I don't think its handled by busybox

User avatar
Semme
Posts: 8399
Joined: Sun 07 Aug 2011, 20:07
Location: World_Hub

#2 Post by Semme »


User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#3 Post by rcrsn51 »

What are you trying to do? Take all the spaces out of the filenames? Please give an example.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#4 Post by SFR »

Someone was calling me? :wink:

Code: Select all

for FILE in *.mp4; do mv "${FILE}" "${FILE//%20/ }"; done
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

april

#5 Post by april »

Thanks

Code: Select all

for FILE in *.mp4; do mv "${FILE}" "${FILE//%20/ }"; done
gives " are the same file " message in a terminal and nothing gets changed

The files look like this
"bob%20the%20builder%20does%20things.mp4"
and I want to get them to look like this
"bobthebuilderdoesthings.mp4"

There is something strange about the behaviour of "%20"


I also tried this from Semme's suggestion-thanks Semme

Code: Select all

# $ for mp4 in *; do new_name=$(echo $mp4 | sed -e 's/%20/ /g' | sed -e 's/
/ /g'); echo file $mp4 will be renamed to $new_name;mv -v $mp4 "$new_name"; done
bash: syntax error near unexpected token `do'
# 
The second sed was left in just because I didn't think it would affect anything

Edit - I finally got one to work albeit slowly , one at a time
Its from the second suggestion by Semme

Code: Select all

#!/bin/sh for filename in *$1* do mv -fT "$filename" `echo $filename | sed -e "s/$1/$2/"` done
The above saved as "massren" and made executeable
then

Code: Select all

massren %20 ""
changes one at a time in each filename so I just kept doing it until it gave an error and voila
Last edited by april on Mon 02 Jun 2014, 22:58, edited 1 time in total.

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#6 Post by RSH »

Code: Select all

#!/bin/sh
#------------------------------------------------------------------------------

# Submitted File
subfile="$1"
echo "`basename "$subfile"`" > /tmp/subfile_tmp

# Parts in File Name to be removed - if needed, add more here
FPARTS="%20 - _"

for FPART in $FPARTS;
do
	S_OUT="$FPART"
	S_IN=""
	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
	do
		sed -i 's|'$S_OUT'|'$S_IN'|' /tmp/subfile_tmp
	done
done
# Remove real Spaces in File name
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
do
	sed -i 's| ||' /tmp/subfile_tmp
done

read newname < /tmp/subfile_tmp

mv "$subfile" "`dirname "$subfile"`/$newname"

rm -f /tmp/subfile_tmp

exit 0
This is a Script that I'm still using to remove parts like %20 from a filename.
Last edited by RSH on Mon 02 Jun 2014, 22:08, edited 1 time in total.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#7 Post by GustavoYz »

Hi, "%20" is escape string for empty space as used by URLs.

Try this:

Code: Select all

for i in *.mp4; do mv $i $(echo $i | sed 's|%20||g' ); done
Or even better, use detox.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#8 Post by SFR »

april wrote:gives " are the same file " message in a terminal and nothing gets changed
[snip]
There is something strange about the behaviour of "%20"
That's strange indeed, but let's see if we get the same results of this one:

Code: Select all

# a="abc%20def%20ghi"; echo $a; echo ${a//%20/}
abc%20def%20ghi
abcdefghi
# 
The only thing I can think of ATM is older version of Bash (3.something.something perhaps?).
If that's the case, just try the above line in 'ash' or 'busybox sh' shell instead.

EDIT: or use RSH's/GustavoYz's sed solutions, which should work regardless of used shell. :wink:

EDIT2: I just found that if it's Bash-3.X.X, the % sign has to be escaped:

Code: Select all

a="abc%20def%20ghi"; echo $a; echo ${a//\%20/}
Greetings!
Last edited by SFR on Tue 03 Jun 2014, 10:28, edited 1 time in total.
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#9 Post by rcrsn51 »

SFR's version works for me, except that there is an extra space before the final }.

april

#10 Post by april »

Thanks all it will take a little time to check these out
However changed above post with this
Edit - I finally got one to work albeit slowly , one at a time
Its from the second suggestion by Semme
Code:
#!/bin/sh for filename in *$1* do mv -fT "$filename" `echo $filename | sed -e "s/$1/$2/"` done

The above saved as "massren" and made executeable
then
Code:
massren %20 ""
changes one at a time in each filename so I just kept doing it until it gave an error and voila

Code: Select all

for i in *.mp4; do mv $i $(echo $i | sed 's|%20||g' ); done 
Worked brilliantly thank you GustavoYz

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#11 Post by Flash »

Or you can open the directory with ROX, select all the files whose names you want to change, right-click on them and choose Rename from the menu. Enter %20 in the first box. leave the second box empty, and see what happens when you hit Enter. ROX won't actually change the file names until you click on Rename at the bottom, so if you don't like the way it looks, just Cancel out.

april

#12 Post by april »

Well theres one I hav'nt seen before thanks Flash .Works well only one %20 at a time but you can see them change before commiting

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#13 Post by Puppus Dogfellow »

could the built in gFnRename utility be of any use to you?

april

#14 Post by april »

Thats another that works and I have never used that before .
Thanks Puppus Dogfellow

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#15 Post by Geoffrey »

Renaming in XFCE wm using Thunar Bulk Rename works
Attachments
rename.jpg
(37.54 KiB) Downloaded 249 times
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

gcmartin

#16 Post by gcmartin »

@Flash shows a way to use ROX. I would like to do so,but need help.
I have a folder with files in it that end with a date ; such as " - 20120407.txt". There are 27 such files in the folder. In ROX, I
  1. highlight the files
  2. right-click to Rename
But, I am lost as to how to set the 2 top typeable boxes to allow me to delete the " - 20120407" portion in the name
for all of the files. Question #1 - How would I use ROX Rename to do this?

When I type - 20120407 in the left box and leave the right empty, I get

Code: Select all

None of the names changed, Nothing to do.
Question #2 - And, if I wanted to change the datestamp to, say, "tested and confirmed", how would I do that, too?

Guidance anyone?
Attachments
Screenshot.jpg
What must I put in the left and right boxes to remove datestamp
(26.46 KiB) Downloaded 297 times

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#17 Post by Flash »

It's good that all the file names you want to change end with the same date.txt. That makes it easy (I hope. :roll: )
Try this after highlighting the files and choosing Rename: in the left ROX box put date.txt. In the right ROX box put tested and confirmed.txt. See if that works.

gcmartin

#18 Post by gcmartin »

Same result.
Attachments
Cannot Batch Rename.jpg
Filled in &quot;Replace&quot; and &quot;With&quot;. Then clicked &quot;Rename&quot; button. ... Error
(37.07 KiB) Downloaded 264 times

gcmartin

#19 Post by gcmartin »

Hark! The problem is discovered.

ROX's bulk rename does what it is designed to do, but, for a "new to app" user it is all-too-easy to NOT notice that each clickable field has mouse popups associated.

But, there is NO mouse popups to provide the steps via an example on how to use the app. I can see how it did confuse.

To use this ROX app, one needs to know that it is a 3 step process. The process, I think, is a good one but, this screen is NOT helpful if you don't have someone to keep nudging you into the right direction. Way too many minutes and support personnel to use something which should be so obvious.

OK, the 3 steps are (for anyone else)
  1. Fill-in the "Replace" and the "With" fields
  2. Click the "Apply" button, on the very right-side of the fields, to visually verify the action that the tool will take
  3. Click "Rename" button, at bottom of window, to carry out the actions you verified
Done!

BTW @Flash, thanks, again.
Attachments
Apply button's use is obscured.jpg
After filling Replace-With fields, click Apply button to verify the intended action.
If satisfied with the intended action,Click the Rename button to complete the action.
This is a first, second, then last step type of screen with little help for first-ti
(43.48 KiB) Downloaded 249 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#20 Post by MochiMoppel »

gcmartin wrote:Hark! The problem is discovered.
Really?
Try this:
Attachments
rename.png
(25.44 KiB) Downloaded 246 times

Post Reply