LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-11-2005, 03:03 PM   #1
prashln
LQ Newbie
 
Registered: Feb 2005
Posts: 2

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


hi,
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.
tia,
-prash.
 
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
Padma
Member
 
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
prashln
LQ Newbie
 
Registered: Feb 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
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.

or

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
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I'm still more comfy with bash so here goes.....
Code:
#!/bin/bash
dir=/home/images
for i in `ls $dir` ; do
  j=`echo $i | sed 's/^-//'`
  mv "$dir/$i" "$dir/$j"
done
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'
Code:
#!/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
LasseW
Member
 
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
yeco
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.
  


Reply


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


All times are GMT -5. The time now is 06:28 PM.

Main Menu
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