Rename all the files within a folder with prefix "Unix_"
Suppose a folder has two files
a.txt
b.pdf
then they both should be renamed from a single command to
Unix_a.txt
Unix_b.pdf
Rename all the files within a folder with prefix "Unix_"
Suppose a folder has two files
a.txt
b.pdf
then they both should be renamed from a single command to
Unix_a.txt
Unix_b.pdf
Situation:
We have certificate.key certificate.crt inside /user/ssl/
We want to rename anything that starts with certificate to certificate_OLD
We are now located inside /user
First, you do a dry run with -n:
rename -n "s/certificate/certificate_old/" ./ssl/*
Which returns:
rename(./ssl/certificate.crt, ./ssl/certificate_OLD.crt)
rename(./ssl/certificate.key, ./ssl/certificate_OLD.key)
Your files will be unchanged this is just a test run.
Solution:
When your happy with the result of the test run it for real:
rename "s/certificate/certificate_OLD/" ./ssl/*
What it means:
`rename "s/ SOMETHING / SOMETING_ELSE " PATH/FILES
Tip:
If you are already on the path run it like this:
rename "s/certificate/certificate_OLD/" *
Or if you want to do this in any sub-directory starting with ss do:
rename -n "s/certificat/certificate_old/" ./ss*/*
You can also do:
rename -n "s/certi*/certificate_old/" ./ss*/*
Which renames anything starting with certi in any sub-directory starting with ss.
The sky is the limit.
Play around with regex and ALWAYS test this BEFORE with -n.
WATCH OUT THIS WILL EVEN RENAME FOLDER NAMES THAT MATCH.
Better cd into the directory and do it there.
USE AT OWN RISK.
find -execdir rename
This renames files and directories with a regular expression affecting only basenames.
So for a prefix you could do:
PATH=/usr/bin find . -depth -execdir rename 's/^/Unix_/' '{}' \;
or to affect files only:
PATH=/usr/bin find . -type f -execdir rename 's/^/Unix_/' '{}' \;
-execdir first cds into the directory before executing only on the basename.
I have explained it in more detail at: Find multiple files and rename them in Linux