LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 04-04-2011, 09:12 PM   #1
xuta
LQ Newbie
 
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 24

Rep: Reputation: 0
[Problem] Mv command meet problem when file name has prefix "-"


I want to use mv command to rename from "-abc.txt" to "abc.txt". But
Eg
Code:
mv '-abc.txt' abc.txt
mv: invalid option -- 'a'
Try `mv --help' for more information.
Plz help me.
Thank you in advance.
 
Old 04-04-2011, 09:34 PM   #2
yancek
Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 5,120

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
Renaming just the one file with rename command:

Code:
rename -abc.txt ""abc.txt -abc*
If you have a number of similarly named files you need to take care you don't rename them all.
 
Old 04-04-2011, 09:50 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
you can use a programming language such as Ruby(1.9+), which doesn't get affected by such shell quirks
Code:
$ ruby -rfileutils -e 'Dir["-*"].each{|x|FileUtils.move(x, "/destination")}'
 
1 members found this post helpful.
Old 04-04-2011, 10:25 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Many commands use two hyphens to indicate the end of options. Try
Code:
mv -- '-abc.txt' 'abc.txt'
 
2 members found this post helpful.
Old 04-04-2011, 10:32 PM   #5
xuta
LQ Newbie
 
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kurumi View Post
you can use a programming language such as Ruby(1.9+), which doesn't get affected by such shell quirks
Code:
$ ruby -rfileutils -e 'Dir["-*"].each{|x|FileUtils.move(x, "/destination")}'
Code:
xuta@xuta-laptop:~/Public$ ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
xuta@xuta-laptop:~/Public$ ls
-abc.txt
xuta@xuta-laptop:~/Public$ echo 'File.rename("-abc.txt", "abc.txt")' | ruby
xuta@xuta-laptop:~/Public$ ls
abc.txt
xuta@xuta-laptop:~/Public$
Thank you for your idea.

---------- Post added 04-05-11 at 09:33 AM ----------

Quote:
Originally Posted by Kenhelm View Post
Many commands use two hyphens to indicate the end of options. Try
Code:
mv -- '-abc.txt' 'abc.txt'
It's is exactly what I find.
Thank you very much.
 
Old 04-04-2011, 10:42 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
You can also target the offending file by prefixing it with the full or relative path.
Code:
mv /path/to/-abc.txt abc.txt
mv ./-abc.txt abc.txt
 
2 members found this post helpful.
Old 04-04-2011, 10:48 PM   #7
xuta
LQ Newbie
 
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
You can also target the offending file by prefixing it with the full or relative path.
Code:
mv /path/to/-abc.txt abc.txt
mv ./-abc.txt abc.txt
+1 useful way.
Thank you so much.
 
Old 04-04-2011, 10:56 PM   #8
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by xuta View Post
Code:
xuta@xuta-laptop:~/Public$ ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
xuta@xuta-laptop:~/Public$ ls
-abc.txt
xuta@xuta-laptop:~/Public$ echo 'File.rename("-abc.txt", "abc.txt")' | ruby
xuta@xuta-laptop:~/Public$ ls
abc.txt
xuta@xuta-laptop:~/Public$
no need to echo. Just call ruby to execute
Code:
ruby -e 'File.rename(....)'
 
Old 04-05-2011, 01:42 PM   #9
DavidMcCann
Senior Member
 
Registered: Jul 2006
Location: London
Distribution: CentOS, Salix
Posts: 3,229

Rep: Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825
Good tip for the future: don't start file names with a hyphen. The shell expects names
1. to start with a letter, number, "_", or "." (hidden files).
2. to only contain letters, numbers, or the characters "_.-". No spaces or other symbols.
 
  


Reply

Tags
mv, option, rename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem in running a script with "./" prefix mahmoodn Ubuntu 3 05-24-2010 05:37 AM
[SOLVED] problem with "./configure --prefix=usr/local/apache --enable-module=so" dynamiteboy Linux - Newbie 12 09-15-2009 12:42 PM
Problem in compile library file command "make" reallynewbie2009 Linux - Newbie 4 09-14-2009 11:04 AM
"stalled" problem in " scp" command gentleshark Linux - Software 1 08-30-2005 03:08 AM
when i install the software "openfoam",i meet the following network problem ztdep Linux - Networking 0 07-22-2005 01:23 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration