Bash: renaming file issues.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: renaming file issues.
# 1  
Old 02-19-2012
Bash: renaming file issues.

Hi, i want to rename a group of directories and files of my music, some items are like this:

[Artist] - [Album], for directories.
[Artist] - [Song title], for files.

I want to do something like this:

[Album], for directories.
[Song title], for files.

This is my code:
Code:
#!/bin/bash
for fname in *.mp3; do
    echo item: $fname
    mv -f $fname ${fname#*-}
done

This is a reduced list of my output:
Code:
user@intel-computer:~/Music/tmp$ ./trim_filename.sh
item: 01 - Bad Romance.mp3
mv: target `Romance.mp3' is not a directory
item: 01 - Just Dance.mp3
mv: target `Dance.mp3' is not a directory
item: 02 - Alejandro.mp3
mv: target `Alejandro.mp3' is not a directory
item: 02 - LoveGame.mp3
mv: target `LoveGame.mp3' is not a directory
item: 03 - Paparazzi.mp3
mv: target `Paparazzi.mp3' is not a directory
item: 04 - Poker Face.mp3
mv: target `Face.mp3' is not a directory
item: 05 - Dance In the Dark.mp3
mv: target `Dark.mp3' is not a directory
item: 06 - Telephone.mp3
mv: target `Telephone.mp3' is not a directory
user@intel-computer:~/Music/tmp$

Can anyone throw some pointers for this?
Thank you for your kind attention.
# 2  
Old 02-19-2012
Because your filenames contain spaces, you need to quote the variable on your move command.

Code:
mv -f "$fname" "${fname#*-}"

The way it was coded, mv was seeing more tokens than just source-file and destination-file causing it to want to treat the last token as a directory name.
This User Gave Thanks to agama For This Post:
# 3  
Old 02-19-2012
Thanks Agama for your help, the script works fine now.
Code:
#!/bin/bash
for fname in *.mp3; do
    mv -f "$fname" "${fname#*- *}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format : abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23 abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35 abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37 abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

2. Shell Programming and Scripting

Recursive File Renaming & Natural Sorting (Bash)

NB! I have already started a thread on this subject on ComputerHope (with the thread title "Recursive File Renaming & Logical Sorting"). However, on ComputerHope they are perhaps more specialized in Windows Command Prompt, and not that much in shell scripts for Bash (I guess). I have a bulk... (1 Reply)
Discussion started by: JewzeyfGhewbelz
1 Replies

3. Shell Programming and Scripting

Bash/cron issues

Hi all, I am trying to run a cronjob to push my files to my git repo once a week and output a prompt to a logfile, my script works fine if I invoke it manually but my cronjob wont run for some reason, I have sourced the file, and restarted my Mac to no avail, right now I believe I have the cronjob... (8 Replies)
Discussion started by: gmenfan83
8 Replies

4. Shell Programming and Scripting

Bash Case Issues..

Hi, I'm having some trouble with using "case...esac" in Bash. I've googled it and am stuggling to understand the syntax and how to do certain things. Firstly, I want to be able to choose a case based on a variable number For example, I have in my code a place where a user can enter... (2 Replies)
Discussion started by: Ste_Moore01
2 Replies

5. Shell Programming and Scripting

Bash Script Issues (If statement for file copying)

Writing a bash script for use with Geektool, pulls the battery info, and shuffles images around so that an Image geeklet can display the correct expression as the desktop background. (Eventually I intend to make it more intricate, based on more variables, and add more expressions) I'm extremely... (1 Reply)
Discussion started by: The_Ardly374
1 Replies

6. Shell Programming and Scripting

Unix bash script (mv issues);

Hey guys, I've registered here as I need urgent help. This is assignment for school and as you can see below I've completed the work. I'm simply stuck on one area. :wall: This script takes the first parameter (which is to be the new extension) and each parameter after that is a file... (1 Reply)
Discussion started by: Cynosure
1 Replies

7. Shell Programming and Scripting

BASH Batch renaming insert additional zero into filename

Hi all, Wondering how this could be accomplished........ a directory contains sequentially numbered files from fw01 to fw999. How would I insert an additional zero so that the directory lists these files in a proper manner? (i.e. all double digit files from fw01 to fw99 would become... (3 Replies)
Discussion started by: putter1900
3 Replies

8. Shell Programming and Scripting

Bash Script issues

So what i am trying to do is write a script that takes in any number of scrambeled words and unscrambles them. I have already addressed the issues of partial matches, exits silently if there are no matches, and finds words regardless of case that they were input. but while trying to get it so... (3 Replies)
Discussion started by: alindner
3 Replies

9. Shell Programming and Scripting

Bash script issues

Hi. The below part of my bash script kicks out the following error message: $ ./extract_eod_report_stats_new.sh 2010-04-23 ./extract_eod_report_stats_new.sh: line 204: syntax error near unexpected token `(' ./extract_eod_report_stats_new.sh: line 204: `TRANSACTIONS_RECEIVED_TOP=`grep... (6 Replies)
Discussion started by: Peter.Lovell
6 Replies

10. Shell Programming and Scripting

Renaming files in a bash script

I'm doing a short batch script to compile po files producing output binary mo files. The compilation command is: msgfmt -o file.mo file.po so in order to compile I am appending .mo to the varible in a loop. It goes something like this: for i in `find . -name "*.po"` do echo... (2 Replies)
Discussion started by: Breen
2 Replies
Login or Register to Ask a Question