Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Bulk rename using terminal
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-21-2008   #11 (permalink)
linny
Just Joined!
 
Join Date: May 2006
Posts: 11
I keep trying this,and I keep doing something wrong.
Code:
$ for FILE in /home/user/Desktop/test2/ ; do
> mv $FILE ${FILE##*-}
> done
mv: cannot move `/home/user/Desktop/test2/' to a subdirectory of itself, `/home/
I don't want to move to another directory,just rename the files and keep them in the same directory.
NOTE:Until I feel comfortable doing this,I'll be testing these commands on test files or copied files in test directories.
Code:
$ for FILE in /home/user/Desktop/test2
>  NEWNAME="${FILE##*_}"
bash: syntax error near unexpected token `NEWNAME="${FILE##*_}"'
Here I didn't even get the chance to finish the command.
I have no clue as to the syntax error here.

I found this yesterday at another forum and this seems to be working.
Here,I opened a terminal in the directory I want to change the file names in,rather than cd to the directory or state the full path to the directory in the command itself.
I've ran into problems before with using the /path/to/file in the command because there was a space in front of the filename I didn't notice.
Instead of
/home/user/Desktop/practice/test.txt
it was
/home/user/Desktop/practice/ test.txt
What a problem that was because all the commands were in proper order,and not working,because the /path/to/filename wasn't correct when I didn't see the space.
I was several days banging my head because of this space.
I look for this a lot closer now.

Anyhow,this command appears to work good.
Code:
$ for f in *test*.txt; do mv -vf "$f" "${f#*-}"; done
`01-test1.txt' -> `test1.txt'
`02-test2.txt' -> `test2.txt'
`03-test3.txt' -> `test3.txt'
$
Just because this seems to be working,I don't want to abandon what has been suggested here.
I want to get through this and understand where I am making my mistakes with the examples presented here.
There is something I'm doing wrong,just like not seeing a space as I mentioned above.

What is the difference between this command I found at another forum and the commands given here?
I've tried them all except the one ghostdog74 has given.
I'll get to trying that next.

I appreciate every reply.
linny is offline   Reply With Quote
Old 06-21-2008   #12 (permalink)
ghostdog74
Linux User
 
Join Date: Aug 2006
Posts: 334
Code:
for FILE in /home/user/Desktop/test2/abc-*
....
ghostdog74 is offline   Reply With Quote
Old 06-21-2008   #13 (permalink)
Freston
Linux Enthusiast
 
Freston's Avatar
 
Join Date: Mar 2007
Posts: 526
Quote:
Originally Posted by linny
I keep trying this,and I keep doing something wrong.
Code:
$ for FILE in /home/user/Desktop/test2/ ; do
> mv $FILE ${FILE##*-}
> done
mv: cannot move `/home/user/Desktop/test2/' to a subdirectory of itself, `/home/
I don't want to move to another directory,just rename the files and keep them in the same directory.
Oh, no problem. This mistake was on my side. It should have been
for FILE in /home/user/Desktop/test2/* ; do

Sorry the asterisk(*) somehow got lost.



And then:
Quote:
Originally Posted by linny
Code:
for FILE in /home/user/Desktop/test2
>  NEWNAME="${FILE##*_}"
bash: syntax error near unexpected token `NEWNAME="${FILE##*_}"'
The syntax is:
for {argument} do {command} done
Bash does not expect to set variables during the {argument} part of the syntax.

Quote:
Originally Posted by linny
Code:
$ for f in *test*.txt; do mv -vf "$f" "${f#*-}"; done
`01-test1.txt' -> `test1.txt'
`02-test2.txt' -> `test2.txt'
`03-test3.txt' -> `test3.txt'
$
(...)
What is the difference between this command I found at another forum and the commands given here?
None. It's the same. Only it uses relative path and a simplified name for the variable. But the basic mechanism, a 'for loop' and parameter substitution as a means to adjust filenames, is the exact same. And it is written in one line rather than spread over several.

It boils down to preference. I tend to spread these things over several lines as it makes it easier to read. And I tend to give variables full names in ALL CAPS. It just makes their function easier to understand.

EDIT: There are some excellent links in ghostdog74's sig. That may help you understand what actually goes on in looping constructs a bit better than I can explain them
__________________
Can't tell an OS by it's GUI
Freston is offline   Reply With Quote
Old 06-21-2008   #14 (permalink)
linny
Just Joined!
 
Join Date: May 2006
Posts: 11
Quote:
It should have been
for FILE in /home/user/Desktop/test2/* ; do
OK.Now it's working!
Quote:
There are some excellent links in ghostdog74's sig
I'll certainly have a look at them.

I have several books on bash,as well as python,C++ and others,but I needed a working example for something I really want to do.
Like rename hundreds of .mp3 files.

I have always wanted to learn bash scripting,but I needed a initiative to get going.
Maybe this is the initiative that I needed.
I see how much time a bash command can save someone from doing things the GUI way.
Hit enter and it's done.It can't get any faster than that.
Getting to where hitting enter gives the desired results is the hard part.

Now with these working examples I'll have to study why they work,and especially try to understand why the ones I tried before didn't.
I saved the output of all the terminal readouts,even the failures.

Thanks to everyone for their time.
It has been highly appreciated.
linny is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:45 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0