Different rename command and usage, depending on Linux distribution

Written by - 0 comments

Published on - Listed in Linux


When using tito to build a local (-only) SRPM or RPM package, the resulting package file contains multiple suffixes, including the current git commit:

root@centos7 /opt/mypackage # tito build --srpm -o /tmp/tito/ --offline --test
Creating output directory: /tmp/tito
Building package [mypackage-1.1.18-1]
Wrote: /tmp/tito/mypackage-git-3.ea801b1.tar.gz
Wrote: /tmp/tito/mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm

That's information I do not need for the final package name.

File renaming with rename command

Easy, I thought. I created an empty file with the same file name on my local machine (running Linux Mint) to do some local tests.

ckadm@mintp ~ $ mkdir -p /tmp/tito
ckadm@mintp ~ $ touch /tmp/tito/mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm

ckadm@mintp /tmp/tito $ ls -la
total 24
drwxrwxr-x  2 ckadm ckadm  4096 Nov  1 13:39 ./
drwxrwxrwt 29 root  root  20480 Nov  1 13:39 ../
-rw-rw-r--  1 ckadm ckadm     0 Nov  1 13:39 mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm

Then I used my beloved rename command to remove the git relevant pars of the file name:

ckadm@mintp ~ $ rename "s/.git.*/.src.rpm/" /tmp/tito/*.src.rpm

This perfectly worked and now shows the src rpm file with the wanted file name:

ckadm@mintp /tmp/tito $ ls -la
total 24
drwxrwxr-x  2 ckadm ckadm  4096 Nov  1 13:43 ./
drwxrwxrwt 29 root  root  20480 Nov  1 13:39 ../
-rw-rw-r--  1 ckadm ckadm     0 Nov  1 13:43 mypackage-1.1.18-1.el7.src.rpm

rename: not enough arguments

But surprise, surprise when the same rename command ran in a GitLab pipeline which uses CentOS 7, Rocky Linux 8 and Rocky Linux 9; the pipeline failed because the rename command failed (on all three distributions):

rename: not enough arguments

I was able to reproduce the same error on a CentOS 7 VM:

root@centos7 ~ # mkdir -p /tmp/tito
root@centos7 ~ # touch /tmp/tito/mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm
root@centos7 ~ # cd /tmp/tito/
root@centos7 /tmp/tito # rename "s/.git.*/.src.rpm/" mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm
rename: not enough arguments

Usage:
 rename [options] expression replacement file...

Options:
 -v, --verbose    explain what is being done
 -s, --symlink    act on symlink target

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see rename(1).

rename is prename on EL distributions

It turns out there are two different rename commands in the Linux world:

  • rename from util-linux package, which is by default installed on EL (Enterprise Linux) based distributions such as Fedora, CentOS, RedHat, RockyLinux et al
  • rename as Perl script, which is by default installed on Debian based distributions and their derivatives, such as Ubuntu, Linux Mint, etc.

There's a great difference in between these two commands. The latter, the Perl script, supports sophisticated renaming with PCRE (Perl Regular Expressions) - which I used above.

So how can this be solved in my pipeline using CentOS and Rocky Linux now? By installing the package containing the same Perl script! The package and also the command is called prename on EL distributions.

root@centos7 /tmp/tito # yum install prename
[...]
Installed:
  prename.noarch 0:1.9-5.el7 

Complete!

root@centos7 /tmp/tito # prename "s/.git.*/.src.rpm/" mypackage-1.1.18-1.el7.git.7.ea801b1.src.rpm

root@centos7 /tmp/tito # ls -la mypackage*
-rw-r--r-- 1 root root 0 Nov  1 14:16 mypackage-1.1.18-1.el7.src.rpm

And it worked! =)

After adjusting the GitLab pipeline jobs (.gitlab-ci.yml) the pipeline ran through again.


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   OpenSearch   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder