LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 10-12-2017, 03:44 PM   #1
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
"rename" command does not want in cent os 7


Hi,

I have several ps files with names like

115_378D-014R315.57753.ps.

I want to rename the files with a leading zero, something like,

0115_378D-014R315.57753.ps.

I used the following command

Quote:
rename 's/\d+/sprintf("%04d", $&)/e' *.ps
which worked well in ubuntu 16.10. But it does not work in Cent OS. How to make this command work ion cent os ?

Also is there any way to add this zero at the beginning of the filename using mv command ?

Please help me in this regard.

Biplab
 
Old 10-12-2017, 06:03 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,547

Rep: Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498Reputation: 2498
The mv command below will add a zero to the beginning of ALL files in the directory in which it is run.

Quote:
for i in *; do mv "$i" 0"$i"; done
The command below will add a zero to the beginning of ALL files with the .ps extension.

Quote:
for i in *.ps; do mv "$i" 0"$i"; done

Don't know about rename.
 
1 members found this post helpful.
Old 10-13-2017, 01:34 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Stock CentOS comes only with a crippled immitation of rename. In order to get the perl rename you'll have to install the CPAN module File::Rename.

Or you can get it with yum by adding the EPEL repository and then adding the package "prename". That will give you what you are looking for with a script named prename. You can then rename it to "rename" if you wish.
 
1 members found this post helpful.
Old 10-13-2017, 02:24 AM   #4
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by yancek View Post
The mv command below will add a zero to the beginning of ALL files in the directory in which it is run.



The command below will add a zero to the beginning of ALL files with the .ps extension.




Don't know about rename.
Thanks for the reply. But my problem is something different. Let me explain. Suppose I have 3 files like

Quote:
115_378D-014R315.57753.307353220123.gpt.fil_1.82ms_Cand.pfd.ps

93_181D-005R326.57781.265860916646.gpt.fil_0.67ms_Cand.pfd.ps

7_164D-005R316.57781.103152990363.gpt.fil_351.91ms_Cand.pfd.ps

I want to add zeros in the beginning in such a way that it will become

Quote:

0115_378D-014R315.57753.307353220123.gpt.fil_1.82ms_Cand.pfd.ps

0093_181D-005R326.57781.265860916646.gpt.fil_0.67ms_Cand.pfd.ps

0007_164D-005R316.57781.103152990363.gpt.fil_351.91ms_Cand.pfd.ps
I run this following script

Quote:

for a in *_*D-*R*.*.*.gpt.fil_*.*ms_Cand.pfd.ps; do


mv $a `printf %04d%s ${a%_*D-*R*.*.*.gpt.fil_*.*ms_Cand.pfd.ps} ${a##_*}`


done
and I got

Quote:
0115115_378D-014R315.57753.307353220123.gpt.fil_1.82ms_Cand.pfd.ps

009393_181D-005R326.57781.265860916646.gpt.fil_0.67ms_Cand.pfd.ps

00077_164D-005R316.57781.103152990363.gpt.fil_351.91ms_Cand.pfd.ps
in stead of

Quote:
0115_378D-014R315.57753.307353220123.gpt.fil_1.82ms_Cand.pfd.ps

0093_181D-005R326.57781.265860916646.gpt.fil_0.67ms_Cand.pfd.ps

0007_164D-005R316.57781.103152990363.gpt.fil_351.91ms_Cand.pfd.ps
is there anything wrong in this ? Can you help me out ?

Thanks in advance.

Biplab
 
Old 10-13-2017, 03:05 AM   #5
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Turbocapitalist View Post
Stock CentOS comes only with a crippled immitation of rename. In order to get the perl rename you'll have to install the CPAN module File::Rename.

Or you can get it with yum by adding the EPEL repository and then adding the package "prename". That will give you what you are looking for with a script named prename. You can then rename it to "rename" if you wish.
Thank you very much for the reply. I tried this as follows :

Quote:

[root@localhost Rename]# yum install perl-App-cpanminus
[root@localhost Rename]# cpanm File::Rename
Now rename is working fine. But can I use mv for the same purpose ? The reason i say this because it will work in every platform.

Biplab
 
Old 10-13-2017, 03:17 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
The perl version of rename should be there on every platform. If not, it is easy enough to install it.

You can do a bash-specific substitution:

Code:
for a in *_*D-*R*.*.*.gpt.fil_*.*ms_Cand.pfd.ps; do 
    b=${a%%_*ps}; 
    c=$(/usr/bin/printf "%06.f" $b); 
    d=${a/$b/$c}; 
    echo mv -i $a $d; 
done;
In some ways that is less portable because not everyone uses bash for a shell. But I guess since ksh and zsh can handle that parameter substitution, it's close enough.

My preference is the perl rename, I find it more convenient.
 
1 members found this post helpful.
Old 10-13-2017, 03:28 AM   #7
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Solved

Quote:
Originally Posted by Turbocapitalist View Post
The perl version of rename should be there on every platform. If not, it is easy enough to install it.

You can do a bash-specific substitution:

Code:
for a in *_*D-*R*.*.*.gpt.fil_*.*ms_Cand.pfd.ps; do 
    b=${a%%_*ps}; 
    c=$(/usr/bin/printf "%06.f" $b); 
    d=${a/$b/$c}; 
    echo mv -i $a $d; 
done;
In some ways that is less portable because not everyone uses bash for a shell. But I guess since ksh and zsh can handle that parameter substitution, it's close enough.

My preference is the perl rename, I find it more convenient.
Thank you very much. This script also works properly. Thanks a lot.

Biplab
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How could a computer technician use the "top" command with "ps" and "kill" to investigate how a system is misbehaving? geckono1 Linux - Newbie 13 07-03-2016 07:51 AM
Centos Server Failed @ Bootup: Missing "/sbin/blkid" & "fsck" command not found beagle7 Linux - Newbie 4 08-24-2012 01:33 AM
rename "wlan0_rename" to "eth0" replica9000 Linux - Wireless Networking 11 03-28-2008 10:39 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
wildcards with the "rename" command mattn Linux - General 3 05-13-2004 07:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 02:44 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