I have a set of files named like
20151016_174721.jpg
and I want to rename them like
2015-10-16 17.47.21.jpg
I tried using rename using the following:
rename -n "s/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2}).*$/$1-$2-$3 $4.$5.$6.jpg/" *.jpg
But it ends up telling me
20151016_174721.jpg renamed as -- ...jpg
And I cannot understand why.
You can use:
rename 's/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})(.*)$/$1-$2-$3 $4.$5.$6$7/' *.jpg
Make sure to use single quotes in your pattern to avoid shell attempting to expand $1, $2 etc.
Related
I've got many files on a linux server which have this format
text_text_mixturelettersnumbers.filefor example Hesperocyparis_goveniana_E00196073A.bam.baior Hesperocyparis_forbesii_RBGEH19_bwa_out.txt. I would like to change the first underscore to a hyphen and leave everything else so it looks like this text-text_mixturelettersnumbers.file.
I have tried rename -n 's/(\w+)_(\w+_.)/$1-$2/' * and many different versions thereof but nothing is happening. Could someone please point out what I've got wrong?
Thanks
Markus
The util-linux rename does not have an option to display the results only. It is very basic.
If you want to list the files that contain two underscores before an extension, use
for f in *_*_*.*; do
echo "$f => ${f/_/-}";
done
To actually rename, use mv:
for f in *_*_*.*; do
mv -- "$f" "${f/_/-}";
done
The "${f/_/-}" replaces the first _ with - in variable f.
So I have a bunch of files who go like this : Yu-Gi-Oh! Zexal - Magic (S01E40).mp4 and I try to rename all of my files like this : S01E40 , I tried this command :
rename 'y/(\((?>[^)(]+|\g<1>)*\))//' *.mp4
Nothing changed .
Anyone can help me ?
Use
rename 's/.*\((.*)\).*(\.mp4)$/$1$2/' *.mp4
It will only keep what is in last parentheses and the extension.
y/// is the same as tr///, i.e. transliteration, which exchanges characters one by one. s/// is for substitution.
I am trying to change the name of a number of files in a directory using rename, but the pattern that I'm using is not working with rename, even though it works in my text editor (BBEdit). I would like to know how to modify the rename command I'm using or the pattern so that I can get rid of the long prefix that each file has.
My directory listing looks like this:
bos012_attempt_2018-02-15-01-52-18_KIC Document 0001.pdf
gem512_attempt_2018-02-14-20-30-11_Geo HW 2.pdf
kgs252_attempt_2018-02-14-23-35-03_kgs252_hw2.pdf
nrs728_attempt_2018-02-15-10-04-42_mids.png
oko018_attempt_2018-02-15-23-57-57_Hw2.pdf
I want to change this to
KIC Document 0001.pdf
Geo HW 2.pdf
kgs252_hw2.pdf
mids.png
Hw2.pdf
Using rename -vs 's/\D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_/''/g' *
produces no changes in the names. Nevertheless, changing the pattern \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ to no character works just fine in my text editor. I've tried different things, e.g.
rename -vs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
and nothing.
mydir $ rename -nvs \D\D\D\d\d\d_attempt_2018-\d\d-\d\d-\d\d-\d\d-\d\d_ '' *
returns
Using expression: sub { use feature ':5.18';
s/\Q${\"DDDddd_attempt_2018\-dd\-dd\-dd\-dd\-dd_"}// }
'bos012_attempt_2018-02-15-01-52-18_KIC_Document_0001.pdf' unchanged
'gem512_attempt_2018-02-14-20-30-11_Geo_HW_2.pdf' unchanged
'nrs728_attempt_2018-02-15-10-04-42_mids.png' unchanged
'oko018_attempt_2018-02-15-23-57-57_Hw2.pdf' unchanged
Depending on your version of rename, you may use this rename command:
rename -n 's/.*_attempt_\d{4}(-\d{2}){5}_//' *.{pdf,png}
'bos012_attempt_2018-02-15-01-52-18_KIC Document 0001.pdf' would be renamed to 'KIC Document 0001.pdf'
'gem512_attempt_2018-02-14-20-30-11_Geo HW 2.pdf' would be renamed to 'Geo HW 2.pdf'
'kgs252_attempt_2018-02-14-23-35-03_kgs252_hw2.pdf' would be renamed to 'kgs252_hw2.pdf'
'oko018_attempt_2018-02-15-23-57-57_Hw2.pdf' would be renamed to 'Hw2.pdf'
'nrs728_attempt_2018-02-15-10-04-42_mids.png' would be renamed to 'mids.png'
If you are satisfied with the output remove -n argument of dry-run.
I have a bunch of files which are of this format:
blabla.log.YYYY.MM.DD
Where YYYY.MM.DD is something like (2016.01.18)
I have quite a few folders with about 1000 files in each, so I wanted to have a simple script to rename them. I want to rename them to
blabla.log
So basically, I'm just stripping the date at the end. Here is what I have:
for f in [a-zA-Z]*.log.[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]; do
mv -v $f ${f#[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]};
done
This script outputs this:
mv: `blabla.log.2016.01.18' and `blabla.log.2016.01.18' are the same file
For more information:
I'm on windows, but I run this script in gitbash
For some reason, my gitbash doesn't recognize the "rename" command
Some regex patterns (like [0-9]{4} don't seem to work)
I'm really at a lost. Thanks.
EDIT: I need to rename every single file that has a date at the end and that is of the from: *.log.2016.01.18. They all need to keep their original names. All that should change is the removal of the date.
You have to use % instead of #: you want to remove from the end, not the start of your string.
Also, you're missing a . in what has to be removed, you don't want to end up with blabla.log..
Quoting the variable names prevents surprises when file names contain special characters.
Together:
mv -v "$f" "${f%.[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]}"
I am trying to write a bash shell script to rename a bunch of photos to my own numbering system. All images filenames are like "IMG_0000.JPG" and I can get the script to match and rename(overwrite) all the photos with the following Perl-regex code:
#!/bin/bash
rename -f 's/\w{4}\d{4}.JPG/replacement.jpg/' *.JPG;
But when I try to use a variable as the name of the replacement, as I keep seeing on other posts here and elsewhere on the internet, nothing happens:
#!/bin/bash
$replacement = "000.jpg";
rename -f 's/\w{4}\d{4}.JPG/$replacement/' *.JPG;
How can I get such a variable to work correctly in my bash script? (NOTE: I am not looking to simply strip the "IMG_" from the filename)
Take the replacement out of single quotes:
#!/bin/bash
$replacement="000.jpg"
rename -f 's/\w{4}\d{4}.JPG/'$replacement'/' *.JPG
Bash does not inspect single quoted strings for interpolation.
Using double quotes and correct variable assignment:
#!/bin/bash
replacement="000.jpg"
rename -f "s/\w{4}\d{4}\.JPG/$replacement/" *.JPG
Note that this can cause trouble, e.g. when renaming two files with names like IMG_0001.JPG and FOO_9352.JPG: The first file will be renamed to 000.jpg, then the second file will also be renamed to 000.jpg, overwriting the first.