Rename everything before brackets for files - regex

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.

Related

Batch rename files with regex not working

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.

Mass rename in shell script

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]}"

Rename files using regular expression in linux

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.

bulk file renaming in bash, to remove name with spaces, leaving trailing digits

Can a bash/shell expert help me in this? Each time I use PDF to split large pdf file (say its name is X.pdf) into separate pages, where each page is one pdf file, it creates files with this pattern
"X 1.pdf"
"X 2.pdf"
"X 3.pdf" etc...
The file name "X" above is the original file name, which can be anything. It then adds one space after the name, then the page number. Page numbers always start from 1 and up to how many pages. There is no option in adobe PDF to change this.
I need to run a shell command to simply remove/strip out all the "X " part, and just leave the digits, like this
1.pdf
2.pdf
3.pdf
....
100.pdf ...etc..
Not being good in pattern matching, not sure what regular expression I need.
I know I need something like
for i in *.pdf; do mv "$i$" ........; done
And it is the ....... part I do not know how to do.
This only needs to run on Linux/Unix system.
Use sed..
for i in *.pdf; do mv "$i" $(sed 's/.*[[:blank:]]//' <<< "$i"); done
And it would be simple through rename
rename 's/.*\s//' *.pdf
You can remove everything up to (including) the last space in the variable with this:
${i##* }
That's "star space" after the double hash, meaning "anything followed by space". ${i#* } would remove up to the first space.
So run this to check:
for i in *.pdf; do echo mv -i -- "$i" "${i##* }" ; done
and remove the echo if it looks good. The -i suggested by Gordon Davisson will prompt you before overwriting, and -- signifies end of options, which prevents things from blowing up if you ever have filenames starting with -.
If you just want to do bulk renaming of files (or directories) and don't mind using external tools, then here's mine: rnm
The command to do what you want would be:
rnm -rs '/.*\s//' *.pdf
.*\s selects the part before (and with) the last white space and replaces it with empty string.
Note:
It doesn't overwrite any existing files (throws warning if it finds an existing file with the target name).
And this operation is failsafe. You can get back the changes made by last rnm command with rnm -u.
Here's a list of documents for rnm.

Optimal procedure to rename files in a linux directory

This question pertains to renaming files in a directory on a Linux system wherein the affected files appear in this general format:
index.html?p=155
index.html?page_id=10
index.html?author=2&paged=5
index.html?feed=rss2&tag=search-engine
index.html?tag=social-media
Might there be a shell level "rename" command that I can use to replace the question marks (?) with an underscore (_) in each file within a directory?
Thank you in advance for any advice or information!
I would prefer the rename command myself, though sometimes a roll-your-own for loop can be more targeted.
Note: rename takes a sed expression as the argument to change and filenames as the last argument. The proper call to use would be:
rename 's/\?/_/' index*
because the ? indicates 0 or 1 of a preceding character when not \ escaped.
This is also easier to toss into a find command which can operate recursively, etc:
find . -name index.html* -exec rename 's/\?/_/' {} +
for file in index.html\?*; do
new=${file/\?/_} # Substitute underscore for ?
mv "$file" "$new" # Rename the file
done
See the Parameter Expansion section of the bash man page for information on the substitution syntax used.
You can use the rename command.
rename '?' '_' *
The first parameter is the expression you want to replace, the second argument is the string to replace the first parameter with, and the final option is the selection of files to apply it to ( all in the current directory, in this case )
See the man page for more details.http://ss64.com/bash/rename.html