LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   [Problem] Mv command meet problem when file name has prefix "-" (https://www.linuxquestions.org/questions/linux-general-1/%5Bproblem%5D-mv-command-meet-problem-when-file-name-has-prefix-872990/)

xuta 04-04-2011 08:12 PM

[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.

yancek 04-04-2011 08:34 PM

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.

kurumi 04-04-2011 08:50 PM

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")}'

Kenhelm 04-04-2011 09:25 PM

Many commands use two hyphens to indicate the end of options. Try
Code:

mv -- '-abc.txt' 'abc.txt'

xuta 04-04-2011 09:32 PM

Quote:

Originally Posted by kurumi (Post 4314076)
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 (Post 4314101)
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.

David the H. 04-04-2011 09:42 PM

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


xuta 04-04-2011 09:48 PM

Quote:

Originally Posted by David the H. (Post 4314111)
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.

kurumi 04-04-2011 09:56 PM

Quote:

Originally Posted by xuta (Post 4314105)
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(....)'

DavidMcCann 04-05-2011 12:42 PM

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.


All times are GMT -5. The time now is 04:50 AM.