LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2009, 01:01 AM   #1
buccaneere
Member
 
Registered: Nov 2007
Posts: 213

Rep: Reputation: 16
CLI syntax for rename function?


I want to rename multiple files (hundreds) that are now on an NTFS partition.

Would this be better done in Windows environment, or Linux?

How do I delete characters from multiple files' titles.

MS knowledge base has syntax for ADDING characters, but not for DELETING characters.

For adding a character:
Quote:
For example, if you had files that are named smitha.doc, smithb.doc, and smithc.doc, you could use the ren smith*.doc smythe*.doc command. All file names automatically show the new spelling, and are renamed smythea.doc, smytheb.doc, and smythec.doc.
But I want to DELETE the first 4 characters from multiple files. I have something like this:

br3tMary.doc
br3tJohn.doc
br3tLindsey.doc
br3tMartin.doc
br3tLisa.doc

In Windows environment, I'm guessing this:
Code:
ren  br3t*.doc  *.doc
What about filepath syntax?

How 'bout Linux syntax?
 
Old 10-19-2009, 01:34 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
For something like this, I doubt if there is any real difference between Linux and Windows. I have the impression that CLI work is easier and more powerful in Linux, but I nver did it in Windows--so I have nothing concrete.

From Linux, accessing the NTFS stuff should be no issue--most modern distros come with NTFS support set up by default.

Quote:
ren br3t*.doc *.doc
(For Linux, replace "ren" with "mv".) That syntax will not work in Linux, and I would be surprised if it did in Windows. At least in Linux, I know of NO case where you can have a wildcard in the target of a command like mv, cp, etc.

Assuming the files are all in one directory:
Code:
for oldname in $(ls); do
   newname = echo $oldname|cut -c 5-
   mv $oldname $newname
done
Probably will not work if it encounters Window filenames with spaces in them. Watch out for other special characters also.

Last edited by pixellany; 10-19-2009 at 01:36 AM.
 
Old 10-19-2009, 02:19 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
Assuming the files are all in one directory:
Code:
for oldname in $(ls); do
   newname = echo $oldname|cut -c 5-
   mv $oldname $newname
done
Code:
for oldname in *
 
Old 10-19-2009, 02:31 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by buccaneere View Post
I want to rename multiple files (hundreds) that are now on an NTFS partition.

Would this be better done in Windows environment, or Linux?
doesn't matter, as long as there are tools for you to do renaming..

Quote:
But I want to DELETE the first 4 characters from multiple files. I have something like this:
on windows, you can use vbscript..better than DOS(cmd.exe) in many ways and it comes natively installed. See here example 6 for renaming using vbscript.
 
Old 10-19-2009, 05:44 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
rename s/^br3t// *.doc

jlinkels
 
Old 10-19-2009, 12:00 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
Code:
for oldname in *
Noted!!---I guess i instintinctively avoid that because it is less obvious what it does. For example, will it see hidden files by default?

these 3 constructs provide a clear view of what will happen:

Code:
for oldname in $(ls)

for oldname in $(ls -a)

for oldname in $(ls -R)
etc.......

Last edited by pixellany; 10-19-2009 at 12:08 PM.
 
Old 10-19-2009, 12:07 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by jlinkels View Post
rename s/^br3t// *.doc

jlinkels
Does not work here...And that syntax does no appear in the man page.
 
Old 10-19-2009, 12:30 PM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by pixellany View Post
Does not work here...And that syntax does no appear in the man page.
jlinkels@jlinkels_pc:/tmp/rn$ ll br3*
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 br3tAlice.doc
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 br3tJohn.doc
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 br3tMary.doc
jlinkels@jlinkels_pc:/tmp/rn$ rename s/^br3t// *.doc
jlinkels@jlinkels_pc:/tmp/rn$ ll
total 0
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 Alice.doc
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 John.doc
-rw-r----- 1 jlinkels users 0 2009-10-19 13:22 Mary.doc
jlinkels@jlinkels_pc:/tmp/rn$

Besides, the man states:
For example, to rename all files matching "*.bak" to strip the extension, you might say
rename 's/\.bak$//' *.bak

So I guessed it would work ^start as well.

However, different version of rename are around, it used to be a perl script, in Debian it was a binary for some time and now it is perl again.

jlinkels
 
Old 10-19-2009, 01:59 PM   #9
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,569

Rep: Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499
Another way using rename:

Code:
rename br3t "" br3t*
 
Old 10-19-2009, 02:41 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I obviously have a different version of rename---the "s" command does not work.

From the man page:
Quote:
AVAILABILITY
The rename command is part of the util-linux-ng package and is available from
ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.

1 January 2000 RENAME(1)
And pacman -Q says:
util-linux-ng 2.16-1 (most recent version)

Available:
community gprename 2.6.3-1
community krename 4.0.0-1

Last edited by pixellany; 10-19-2009 at 02:44 PM.
 
Old 10-19-2009, 03:56 PM   #11
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
My rename is a symbolic link to prename which is [obviously] a Perl script. It is part of the package perl in Debian Lenny.
There are an awful lot of rename packages around so I am not surprised that they differ in syntax.

jlinkels
 
Old 10-19-2009, 06:59 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
Noted!!---I guess i instintinctively avoid that because it is less obvious what it does. For example, will it see hidden files by default?
ls by itself doesn't display hidden files.
Code:
for files in .*
do
  echo "hidden file: $i"
done
Quote:
Code:
for oldname in $(ls)
this is same as
Code:
for oldname in *
Quote:
Code:
for oldname in $(ls -a)
this is same as
Code:
for oldname in .* *
of course, if one needs the added functionality of ls with its other switches then i guess its still alright, however always take care of "spaces in filename" problem when using for loop...if not, use a while loop
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How does the C function rename work in Linux? dman65 Programming 1 03-09-2009 08:54 AM
LXer: Mass-Rename Files Using an Easy CLI Method LXer Syndicated Linux News 0 07-07-2008 06:20 PM
grep CLI syntax: how to find A OR B? Kropotkin Linux - Newbie 3 01-16-2007 09:45 PM
awk cli to rename a list of files... pld Linux - General 4 02-15-2005 10:57 PM
how to rename via CLI with a space in the name Lleb_KCir Linux - General 6 05-19-2004 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 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
Open Source Consulting | Domain Registration