Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 02-11-2005, 03:03 PM   #1
LQ Newbie
Registered: Feb 2005
Posts: 2

Rep: Reputation: 0
renaming file names beginning with "-"

i ran a perl script using "rename" that removes the first few characters of files in a particular folder. now i realise i am left with a bunch of files that begin with "-" and i am unable to rename them using either perl's "rename" command or the unix "mv" command because the "-" interferes with the options.
any help will be much appreciated.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-11-2005, 03:05 PM   #2
Registered: Aug 2003
Location: Omaha, NE, USA
Distribution: PCLinuxOS 2007
Posts: 808

Rep: Reputation: 30
You need to 'escape' the leading "-". Either use quotes, e.g.

mv "-filename" filename

or use a backslash character, e.g.

mv \-filename filename
Old 02-11-2005, 03:10 PM   #3
LQ Newbie
Registered: Feb 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Originally posted by Padma
You need to 'escape' the leading "-". Either use quotes, e.g.

mv "-filename" filename

or use a backslash character, e.g.

mv \-filename filename
i tried this but it does not work:

prash@mantra:/mnt/h/Books/Tech$ mv "- XXX.pdf" XXX.pdf
mv: invalid option --
Try `mv --help' for more information.


prash@mantra:/mnt/h/Books/Tech$ mv -\ XXX.pdf XXX.pdf
mv: invalid option --
Try `mv --help' for more information.
Old 02-11-2005, 04:22 PM   #4
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I'm still more comfy with bash so here goes.....
for i in `ls $dir` ; do
  j=`echo $i | sed 's/^-//'`
  mv "$dir/$i" "$dir/$j"
Here is similar with perl, I just don't remember how to tell it only remove leading dash like is done with sed -e 's/^-//g'
#!/usr/bin/perl -w

@files = (`ls /home/images/*`);

foreach $i ( @files ) {
	chomp $i;
	$newname = $i;
	$newname =~ s/-//;
	print "The file '$i' is renamed to '$newname'\n";
	system (`mv '$i' '$newname'`);

Last edited by homey; 02-11-2005 at 04:48 PM.
Old 02-12-2005, 05:29 AM   #5
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
You can escape the leading dash by preceding it with a path. In addition you need quotes or a backslash to take care of the space:

mv ./"- XXX.pdf" XXX.pdf
Old 05-01-2010, 01:47 AM   #6
LQ Newbie
Registered: May 2010
Location: Costa Rica
Distribution: Darwin, RedHat, Debian
Posts: 1

Rep: Reputation: 2
My two cents.

I know this thread is veeeery long, but it still comes up in the google search.

So here it is.

Escaping the file name with \ or "" won't work at all. What you need to do is use the end-of-options flag like this:

mv -- -evilfile goodfile

Hope this helps.
2 members found this post helpful.


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
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Renaming Multiple Files with a "." on the end of the filename dbowles Programming 1 05-09-2005 04:28 PM
Escaping "-" at the beginning of a file! thebored Linux - General 2 04-08-2005 06:58 PM
How to fix the "login" window in the beginning, in mangrake 9.2 ? freaker2k5 Linux - General 0 09-22-2004 05:47 AM
Can't locate object method "splitpath" via package "File::Spec" RobJohnston Linux - General 2 06-28-2003 09:59 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:03 PM.

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