How to write a script to rename the files? - linux

I have files
263_V01_C07_R000_THx_BH_4096H.dat,263_V01_C07_R000_THY_BH_4096H.dat
and so on
I would like to change all R000 into R011.I have tried like this:
#!/bin/bash
for file in *.dat; do
if [[ "$file" =~ _THx_ ]]; then
mv $file $file2
fi
done
But how to define file2?

You can substitute characters in a variable like this
file2=${file/R000/R011}

You can use rename
rename 's/R000/R011/' *.dat

You can use rename:
rename R000 R011 *
It looks like there are different versions floating around; an older, which is part of the util-linux project using the syntax above, and a newer by Larry Wall, using a Perl expression for the renaming:
rename 's/R000/R011/' *
Check the man page of your rename to see which one you have.

Related

Linux renaming now.jpg.1 to spy_1.html

I want to use linux to rename 10 files.
now.jpg to spy_.html
And the other 9 should be
now.jpg.1 to spy_html.1
now.jpg.2 to spy_html.2
And so forth.
So far I have come up with this:
for f in *.jpg
do
mv “$f” “(“%s”%p”%i”%o”%n”%_).html”
done
But it doesn't work. Any tips appreciated.
You could use regular expression to capture the optional numeric extension and add it to the target file name. This should work with bash:
for f in *.jpg*
do
if [[ $f =~ now\.jpg(\.[0-9])? ]]; then
mv "$f" "spy.html${BASH_REMATCH[1]}"
fi
done
assuming you want spy.html[.#]. If you want an underscore for the files with the numerical extension, i.e. spy_html.1, it would be easier to issue two commands, one for now.jpg and one loop for the now.jpg.*.

rename all files in folder through regular expression

I have a folder with lots of files which name has the following structure:
01.artist_name - song_name.mp3
I want to go through all of them and rename them using the regexp:
/^d+\./
so i get only :
artist_name - song_name.mp3
How can i do this in bash?
You can do this in BASH:
for f in [0-9]*.mp3; do
mv "$f" "${f#*.}"
done
Use the Perl rename utility utility. It might be installed on your version of Linux or easy to find.
rename 's/^\d+\.//' -n *.mp3
With the -n flag, it will be a dry run, printing what would be renamed, without actually renaming. If the output looks good, drop the -n flag.
Use 'sed' bash command to do so:
for f in *.mp3;
do
new_name="$(echo $f | sed 's/[^.]*.//')"
mv $f $new_name
done
...in this case, regular expression [^.].* matches everything before first period of a string.

linux rename files in bulk using bash script or command line one liner

I have a list of for example 100 files with the naming convention
<date>_<Time>_XYZ.xml.abc
<date>_<Time>_XYZ.xml
<date>_<Time>_XYZ.csv
for example
20140730_025373_XYZ.xml
20140730_015233_XYZ.xml.ab
20140730_015233_XYZ.csv
Now I want to write script which will remove anything between two underscores. for example in the above case
remove 015233 and change 20140730_015233_XYZ.xml.ab to 20140730_XYZ.xml.ab
remove 015233 and change 20140730_015233_XYZ.csv to 20140730_XYZ.csv
I have tried number of various options using rename, cut, mv but I am getting varied results, not the one which I expect.
You could use rename command if you want to rename files present inside the current directory,
rename 's/^([^_]*)_[^_]*(_.*)$/$1$2/g' *
You can use sed:
sed 's/\([^_]*\)_.*_\(.*\)/\1_\2/' files.list
You can also use cut command
cut -d'_' -f1,3 filename
for FILE in *; do mv "$FILE" "${FILE/_*_/_}"; done
And more specific is
for FILE in *.xml *.xml.ab *.csv; do mv "$FILE" "${FILE/_*_/_}"; done
Further:
for FILE in *_*_*.xml *_*_*.xml.ab *_*_*.csv; do mv "$FILE" "${FILE/_*_/_}"; done

Partial File Rename with different file types

Sorry if this is very simple compared to usual questions but I am just starting out. I have some files all with the same start name but of different file types, e.g:
1234.x
1234.y
1234.z
1234_V2.x
1234_V2.y
1234_V2.z
I want to rename the first part of these whilst keeping any ending and file type, e.g:
4321.x
4321.y
4321.z
4321_V2.x etc
I have tried using
mv 1234* 4321*
and
rename 1234* 4321*
But no luck! I have also been through all the other SO articles and although I could use a loop, most depend on the file type being the same.
Thanks in advance
You can use bash substitution:
for file in 1234*
do mv "$file" "4321${file#1234}"
done
OR, replace the do mv with the following
do mv "$file" "${file/1234/4321}"
See more in man bash under EXPANSION section, sub-section Parameter Expansion
Assuming your filenames for 1234 and 4321 i.e constant for all files, you can try this
for fn in `find . -name 1234*`
do
newf=`echo $fn | sed s/1234/4321/`
mv $fn $newfn
done
You can use a shell script, but it's kind of ugly because it will fork a lot, and thus, if you have a lot of files to rename, it will take time.
for f in 1234*; do echo mv $f $(echo $f | sed -e 's/1234/4321/'); done
Otherwize, rename is a good way to do it:
rename 's/1234/4321/' 1234*
Rename expects a regular expression as first parameter, see online documentation
See if it works:
rename "s/1234/4321/" 1234*
command means substitute(because of s) occurances of "1234" with "4321" in files that has name of pattern 1234*
You can also look at here. It is slightly more complicated than your case.

Bash scripting : How do I rename files to remove numeric characters at the beginning?

mv command doesnt accept pattern matching like grep !
Whats the good way to handle this and similar kind of operations ?
There's the rename tool, but if that's not what you want, you can do:
for file in *; do
new_file="${file##[0-9]}" # Strip all leading numbers
mv "$file" "$newfile"
done

Resources