Can't get mv to work properly


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Can't get mv to work properly
# 1  
Old 01-20-2017
Can't get mv to work properly

Trying to use mv in a shell script but for some reason this does not work:
Code:
for f in *.wav;do mv $f $f.bwf;done

I get this:
Code:
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

So it's like I'm using 'mv' wrong but I can't see how.

This works so the contens of the folder is read properly in the for-loop:
Code:
for f in *.wav;do echo $f;done

---------- Post updated at 01:56 AM ---------- Previous update was at 01:43 AM ----------

There are blank spaces in the filenames, do I need to take that into account?
# 2  
Old 01-20-2017
Yes! Try:
Code:
for f in *.wav;do mv "$f" "$f.bwf";done

# 3  
Old 01-21-2017
Yes, that works. Thanks!
But I realized i need to remove the ".wav" before adding the ".bwf".
Which is the most convenient method for that?

---------- Post updated at 10:33 AM ---------- Previous update was at 10:15 AM ----------

A tiny remark for others like me who've missed this.

I edited my script in the standard macOS Texteditor and the quotation character I get there is not the right one for bash. At least not with a Swedish keyboard.
The one used in Don's post works and looks like this:
Code:
"

The one I get from the Texteditor does not work and looks like this:
Code:

A tiny but significant difference. Almost invisible in some typefaces!
# 4  
Old 01-21-2017
So, we move up to the next level in using shell variables (adding parameter expansions to the steps involved):
Code:
for f in *.wav;do mv "$f" "${f%wav}bwf";done

And, concerning your "tiny remark"; we usually recommend that you learn to use vi when editing shell scripts instead of using "pretty-printing editors" like TextEdit. The vi editor may have a steep learning curve, but you will learn all about using regular expressions (that you can use in sed and grep and find) and editing commands that you can use not only in vi but also in sed, shell command history expansions, ed, ex, and cursor motion commands that apply in many other interactive commands, AND you'll get the single-quotes, double-quotes, and back-quotes you need without a pretty-printing editor's thinking that other characters might look prettier but then make your scripts fail miserably since shells don't recognize them as valid syntax.

Last edited by Don Cragun; 01-21-2017 at 06:06 AM.. Reason: Fix typo: s/scrips/scripts/
# 5  
Old 01-21-2017
Thanks, that works nicely.

I should have a look at vi.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Having trouble getting my interactive perl script to work properly

So I'm making an interactive perl script, but I can't get it to work properly. I'm trying to make a script that tell the user to input either 'q' or 'Q' to terminate the program, or 'c' to continue it. If they input anything other than those three keys, it should prompt the user again and again... (5 Replies)
Discussion started by: Eric1
5 Replies

2. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies

3. Red Hat

TAB button doesn't work properly

Hi, I have list of files under a directory as shown below. /home/root -rw-r--r-- 1 user 0 Aug 27 06:08 rough.txt -rw-r--r-- 1 user 0 Aug 27 06:08 test.sh Now when i use to read the rough.txt, i did cat r and then pressed TAB button. Below is the resulting string after pressing TAB... (2 Replies)
Discussion started by: vel4ever
2 Replies

4. UNIX and Linux Applications

Gvim doesn't work properly

Hello All, I am using gvim ( redhat linux machine). backspace doesnot work properly. can some boby suggest a solution ? i have checked with older version. backspace works in it. Thanks Shiv (1 Reply)
Discussion started by: shiv.emf
1 Replies

5. Shell Programming and Scripting

A script doesnt work properly when crontab

Hi, I have a script which does a tar and sends it to another server as backup. Script is as below # Locations to be backed up. Seperate by space BACKUP_LOCATIONS=/repos/subversion BACKUP_BASE_FOLDER=/bakpool BACKUP_FILE_NAME_ROOT=svn-backup START_TIME_DISP=`date` START_TIME=`date... (11 Replies)
Discussion started by: digitalrg
11 Replies

6. Solaris

Solaris Network doesn't work properly

Hi to all! I want to learn step by step easily how to configure my Solaris for network. I know alot about Solaris Network configuration. But I have some problems. When I install Solaris, and I plug-in my network cable to Solaris. Then I run: ifconfig -a plumb then I do ifconfig bge0 dhcp... (7 Replies)
Discussion started by: SecureXCode
7 Replies

7. AIX

lvm_queryvg call does not work properly and results in a sudden memory rise.

On AIX 5.3 host, the lvm_queryvg call does not work properly and results in a sudden memory rise. This is happening on one particular host and the call works fine on another host. Is this a known issue and is there any patch available for this? (0 Replies)
Discussion started by: sandiworld
0 Replies

8. HP-UX

pstat_getdisk() call doesn’t work properly in HPUX 11.31 (11i V3)

As per the man page, pstat_getdisk() call returns the number of instances, which could be 0 upon successful completion, otherwise a value of -1 is returned. Please have a look at this sample program -> #include <stdio.h> #include <sys/pstat.h> int main() { int j = 0, ret; struct... (2 Replies)
Discussion started by: sandiworld
2 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies
Login or Register to Ask a Question