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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
04-04-2011, 08:12 PM
#1
LQ Newbie
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 17
Rep:
[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.
04-04-2011, 08:34 PM
#2
Senior Member
Registered: Apr 2008
Distribution: PCLinux, Ubuntu, Peppermint
Posts: 2,721
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.
04-04-2011, 08:50 PM
#3
Member
Registered: Apr 2010
Posts: 217
Rep:
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.
04-04-2011, 09:25 PM
#4
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 299
Rep:
Many commands use two hyphens to indicate the end of options. Try
Code:
mv -- '-abc.txt' 'abc.txt'
2 members found this post helpful.
04-04-2011, 09:32 PM
#5
LQ Newbie
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 17
Original Poster
Rep:
Quote:
Originally Posted by
kurumi
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
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.
04-04-2011, 09:42 PM
#6
Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,320
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.
04-04-2011, 09:48 PM
#7
LQ Newbie
Registered: Apr 2011
Location: Hanoi, Vietnam
Distribution: Ubuntu, OpenSuSE, Debian, Gentoo
Posts: 17
Original Poster
Rep:
Quote:
Originally Posted by
David the H.
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.
04-04-2011, 09:56 PM
#8
Member
Registered: Apr 2010
Posts: 217
Rep:
Quote:
Originally Posted by
xuta
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(....)'
04-05-2011, 12:42 PM
#9
Senior Member
Registered: Jul 2006
Location: London
Distribution: CentOS, Salix
Posts: 1,509
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.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 06:55 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News