Renaming files using regular expressions - Linux - regex

I have three files named
file1.txt
file2.txt
file3.txt
I am trying to rename them to
mynewfile-1.txt
mynewfile-2.txt
mynewfile-3.txt
How would I go about this using regular expressions?

Like this :
rename -n 's/^file/mynewfile-/' *.txt
or from comments :
rename -n 's/^file(\d+)/mynewfile-${1}-test/' *.txt
___ ____
^ ^
capturing group |
captured group
Drop -n switch when the output looks good to rename for real.
There are other tools with the same name which may or may not be able to do this, so be careful.
The rename command that is part of the util-linux package, won't.
If you run the following command (GNU)
$ rename
and you see perlexpr, then this seems to be the right tool.
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename
For archlinux:
pacman -S perl-rename
For RedHat-family distros:
yum install prename
The 'prename' package is in the EPEL repository.
For Gentoo:
emerge dev-perl/rename
For *BSD:
pkg install p5-File-Rename
For Mac users:
brew install rename
If you don't have this command with another distro, search your package manager to install it or do it manually
Or you can use perl CPAN:
cpan -i File::Rename
Old standalone version can be found here
man rename
This tool was originally written by Larry Wall, the Perl's dad.

Related

yum + how to identify the installed rpm with lower version

The goal is to compare the list of rpms under folder /tmp/list_of_rpms to installed rpm, and if the package is installed with a lower version then print this rpm.
The approach to check if rpm is installed or not can be easily verified with
rpm -qi gssproxy-0.7.0-30.el7_9.x86_64.rpm
package gssproxy-0.7.0-30.el7_9.x86_64.rpm is not installed
but actually this rpm installed with lower version as
rpm -qa | grep gssproxy
gssproxy-0.7.0-29.el7.x86_64
Can we identify also if rpm is installed with lower version?
The approach to take the installed rpm by rpm -qa | grep gssproxy
and then comparing it to the rpm file /tmp/gssproxy-0.7.0-30.el7_9.x86_64.rpm by regex is very complicated.
There's a dedicated tool called rpmdev-vercmp for comparing RPM versions. It's in the yum-utils package. apparently in a package named rpmdevtools.
Untested, but hopefully sufficient to get you started:
for rpm in /tmp/list_of_rpms/*.rpm; do
base=${rpm#/tmp/list_of_rpms/}
pkg=${base%-*-*}
if installed=$(rpm -q "$base"); then
rpmdev-vercmp "$pkg" "$installed" >/dev/null 2>&1
case $? in 12) echo "$installed is less than $pkg";; esac
fi
done
This doesn't do anything for packages which are not installed at all; perhaps you want to add an else clause and add a notification about those, too?
Some background from https://www.golinuxhub.com/2018/06/how-to-compare-rpm-version-bash-script-python-linux-algorithm/ but the actual code in that has problems. Also, https://unix.stackexchange.com/questions/163702/bash-script-to-verify-that-an-rpm-is-at-least-at-a-given-version

CentOS 7 sudo >> -E: command not found

I'm working on CentOS 7 and regular sudo commands (e.g. sudo yum update, etc.) are working fine. However, one of my sudo commands require to preserve the environment variables, so I used:
sudo -E ./build/unit-tests
and I get this error:
/var/tmp/sclyZMkcN: line 8: -E: command not found
It appears sudo is not recognizing the -E command on CentOS 7. What can I do in this case? Any alternatives or possible fix?
I've recently come across exactly the same problem. I tried to execute a script with sudo -E, which caused the above-mentioned -E: command not found error.
The reason turned out to be Red Hat Developer Toolset providing a broken sudo. A solution is to use the full sudo system path to make sure a good one is used, i.e.
/usr/bin/sudo -E ./some_script.sh
I you know which variables to preserve, you can use env to pass them through the command line.
sudo env foo="$foo" bar="$bar" ./build/unit-tests

How to list current package versions in OPAM?

opam list -a lists all packages currently available at OPAM, but does not display the version number for packages which are not currently installed, as per the opam list --help output:
(...) the output format displays one package per line, and each line contains the name of the package, the installed version or -- if the package is not installed, (...)
How can I list all packages including their version numbers?
Use opam info <packagename>.
I guess opam list does only prints the versions of already installed packages because of the package dependencies. Listing the latest versions of packages, for example, of not-yet-installed packages is not quite useful.
A refined (and quicker) version of anol's answer is to give the whole list of packages to opam show in one pass (asking opam to output both package and version field), and to process the result with sed, as apparently show outputs each field on its own line:
opam show -f package,version $(opam list -a -s) \
| sed -e '/ *package:/N; s/ *package: \([^\n]*\)\n *version: \([^\n]*\)/\1: \2/'
This is not an ideal solution, but using camlspotter's recommandation, I manually queried each package for its version field, using the following shell loop:
for p in $(opam list -a -s); do echo "$p $(opam show -f version $p)"; done
It works, but it takes ~85 seconds to complete on my machine (querying over 1000 packages currently available).

A diff which compares changes within lines for Mac OS X

I'm looking for software that would let me apply changes within lines – not necessarily automaticall – rather than the whole line. I read that wdiff can do this, however there is no Mac OS X version.
If you have Homebrew, you can use:
brew install wdiff
If you want to see the differences in color, try:
brew install colordiff
Then you can do:
wdiff file1.txt file2.txt | colordiff
or
wdiff file1.txt file2.txt | colordiff | less -R
There is (now) a version of wdiff available for Mac OS X. Just install MacPorts and run "sudo port install wdiff".
-Scott
There are probably lots of options for this. The first I came across with a quick Google search is KDiff3.
I use DeltaWalker but use AlternativeTo to see other options

How do you get Amazon SES working on Debian Squeeze?

All the Perl dependencies for it are met but I'm getting this error:
Can't locate object method "ssl_opts" via package "LWP::UserAgent" at SES.pm line 250.
I just wanted to document what I had to do to get this running on my Debian system. The solution for Ubuntu is probably the same.
First, to let Perl find SES.pm make the directory /usr/local/lib/site_perl and copy SES.pm there. I prefer this solution over what the README recommends.
Your system probably has a lot of the dependencies met already so instead of installing duplicate packages just check first which ones it needs. To do that run these commands. If it gives an error it's not met:
perl -e 'use Crypt::SSLeay'
perl -e 'use Digest::SHA'
perl -e 'use Bundle::LWP'
perl -e 'use LWP::Protocol::https'
perl -e 'use MIME::Base64'
perl -e 'use Crypt::SSLeay'
perl -e 'use XML::LibXML'
I had to remove the package libcrypt-ssleay-perl because it's not compatible with this Amazon script. With it the script produces the error in the question.
I installed these packages from Debian:
libxml-libxml-perl
libssl-dev (needed to compile dependencies)
To find out which package contains the Perl module you need use this page on the Debian site to search the contents of packages:
http://www.debian.org/distrib/packages
Replace the :: in the package with / and put .pm at the end. For example if you need XML::LibXML search for XML/LibXML.pm
I installed these packages from CPAN. It takes a little while though. There are a lot of dependencies.
perl -MCPAN -e 'install LWP::Protocol::https'
perl -MCPAN -e 'install Crypt::SSLeay'
Comment out line 250 in SES.pm, as follows:
# $browser->ssl_opts(verify_hostname => 1);
Then it will run. Probably less secure. But it runs.