Glob renaming in bash - bash

I'm fairly new to bash so sorry if this is kind of a basic question. I was trying to rename a bunch of mp3 files to prepend 1- to their filenames, and mv *.mp3 1-*.mp3 didn't work unfortunately. So I tried to script it, first with echo to test the commands:
for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done
Which seems to output the commands that I like, so I removed the echo, changing the command to
for f in *.mp3 ; do mv \'$f\' \'1-$f\'; done
Which failed. Next I tried piping the commands onward like so
for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done | /bin/sh
Which worked, but if anyone could enlighten me as to why the middle command doesn't work I would be interested to know. Or if there is an more elegant one-liner that would do what I wanted, I would be interested to see that too.

I think you have to change the command to
for f in *.mp3 ; do mv "$f" "1-$f"; done
Otherwise you would pass something like 'file1.mp3' and '1-file1.mp3' to mv (including the single quotes).

Dry run:
rename -n 's/^/1-/' *.mp3
Remove the -n if it looks correct to run the command. man rename for details.

Related

Bash : how to rename files that have two extentions?

I call a small program to convert .tap files (retrogaming) into .wav audio files. The program output the new file and just add the .wav extention
example : "file.tap" becomes "file.tap.wav".
I wrote a tiny script to batch the conversion. It works but I cannot manage to rename the file at the end so to remove the ".tap" in the filename.
Note that sometime the original file has lowercase or uppercase.
here is my code. Any help is welcome.
My idea was to 'erase' the ".tap" or ".TAP" in the final filename.
nb : I must use $file.wav like that in the instruction, otherwise it doesn't work.
thank you for your help
#!/bin/bash
for file in `ls`;
do
./tape_converter -11 -a $file $file.wav ;
mv $file ${file/\.tap\.wav/\.wav/};
done
you can modify your script to something like
for f in *.tap; do
<your command> "$f" "${f%.*}.wav"
done
this will name directly the output files with same basename and extension ".wav".
Just remove .tap, then add .wav; there's no need for trying to do a replacement.
for f in *; do
./tape_converter -11 -a "$f" "$f.wav"
mv -- "$f" "${f%.tap}.wav"
done
Here is my final code. Thanks to
thanasisp. It works fine. Happy.
for i in *;
do mv "$i" "$(echo $i|tr A-Z a-z)";
done
mkdir output
for f in *.tap;
do
./tape_converter -11 -a $f ${f%.*}.wav ;
mv *.wav output/
done

Renaming files in bash

My directory has many files named as "20130101_temp.txt", "20130102_temp.txt", etc.
How do I remove the "_temp" in the names of all these files. i.e., rename 20130101_temp.txt to 20130101.txt.
Using bash:
for x in *_temp.txt
do
mv $x ${x%%_temp.txt}.txt
done
There's also a utility that comes with Perl (at least on Ubuntu) called rename which takes a regular expression, so you could accomplish the same thing with:
rename -n 's/_temp\.txt$/.txt/' *_temp.txt
The -n option initiates a "dry run" that will only show you what is going to be renamed. Remove it to actually perform the rename.
Using a for-loop with a glob to find the files and a parameter substitution to remove the _temp for moving:
for t in ????????_temp.txt; do
echo mv ${t} ${t/_temp/}
done
Remove the echo when you've tested that the output looks right on your system.
Try something like this:
for FILENAME in *_temp.txt; do
mv $FILENAME `echo $FILENAME | sed -e 's/_temp//'`
done
It is usually a good idea to try it out first with the mv replaced with an echo.
It's not a bash solution but since I encounter renaming tasks frequently while being way to lazy to think about a reasonable bash solution, I just got pyRenamer, a GUI tool that does things like that quite well. It's usually installable from the standard repositories.
dlundquists solution works quite well though.
This worked for me:
find . -depth -name '*_temp*' -execdir bash -c 'for f; do mv -i "$f" "${f//_temp/ }"; done' bash {} +

Rename all files in directory from $filename_h to $filename_half?

Dead simple.
How do I rename
05_h.png
06_h.png
to
05_half.png
06_half.png
At least, I think it's simple, but it's hard to Google for this kind of thing unless you already know.
Thanks....
Just use bash, no need to call external commands.
for file in *_h.png
do
mv "$file" "${file/_h.png/_half.png}"
done
Do not add #!/bin/sh
For those that need that one-liner:
for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done
Try rename command:
rename 's/_h.png/_half.png/' *.png
Update:
example usage:
create some content
$ mkdir /tmp/foo
$ cd /tmp/foo
$ touch one_h.png two_h.png three_h.png
$ ls
one_h.png three_h.png two_h.png
test solution:
$ rename 's/_h.png/_half.png/' *.png
$ ls
one_half.png three_half.png two_half.png
for f in *.png; do
fnew=`echo $f | sed 's/_h.png/_half.png/'`
mv $f $fnew
done
Or in one-liner:
for f in *.png; do mv "$f" "$(echo $f | sed 's/_h.png$/_half.png/g')"; done
Are you looking for a pure bash solution? There are many approaches, but here's one.
for file in *_h.png ; do mv "$file" "${file%%_h.png}_half.png" ; done
This presumes that the only files in the current directory that end in _h.png are the ones you want to rename.
Much more specifically
for file in 0{5..6}_h.png ; do mv "$file" "${file/_h./_half.}" ; done
Presuming those two examples are your only. files.
For the general case, file renaming in has
been covered
before.
Use the rename utility written in perl.
Might be that it is not available by default though...
$ touch 0{5..6}_h.png
$ ls
05_h.png 06_h.png
$ rename 's/h/half/' *.png
$ ls
05_half.png 06_half.png
for i in *_h.png ; do
mv $i `echo "$i"|awk -F'.' '{print $1"alf."$2}'`
done
I had a similar question:
In the manual, it describes rename as
rename [option] expression replacement file
so you can use it in this way
rename _h _half *.png
In the code:
'_h' is the expression that you are looking for.
'_half' is the pattern that you want to replace with.
'*.png' is the range of files that you are looking for your possible target files.
Hope this can help c:
Another approach can be manually using batch rename option
Right click on the file -> File Custom Commands -> Batch Rename
and you can replace h. with half.
This will work for linux based gui using WinSCP etc
One liner:
for file in *.php ; do mv "$file" "_$file" ; done
Although the answer set is complete, I need to add another missing one.
for i in *_h.png;
do name=`echo "$i" | cut -d'_' -f1`
echo "Executing of name $name"
mv "$i" "${name}_half.png"
done
I had to rename the prefix of files and I found this answer with a solution like this:
for i in h_*; do mv ${i/#h_/half_}; done
If pattern begins with #, it must match at the beginning of the
expanded value of parameter. If pattern begins with %, it must match
at the end of the expanded value of parameter.
from man bash
Use the rename utility:
rc#bvm3:/tmp/foo $ touch 05_h.png 06_h.png
rc#bvm3:/tmp/foo $ rename 's/_h/_half/' *
rc#bvm3:/tmp/foo $ ls -l
total 0
-rw-r--r-- 1 rc rc 0 2011-09-17 00:15 05_half.png
-rw-r--r-- 1 rc rc 0 2011-09-17 00:15 06_half.png

batch rename files with ids intact

i have a directory listing like
seascaperecovered0088crop.jpg
seascaperecovered0096crop.jpg
seascaperecovered0098crop.jpg
seascaperecovered0101crop.jpg
seascaperecovered0103crop.jpg
seascaperecovered0105crop.jpg
seascaperecovered0107crop.jpg
seascaperecovered0112crop.jpg
seascaperecovered0119crop.jpg
seascaperecovered0122crop.jpg
and i want to rename all files as seen here:
seascape_0122.jpg
i have tried something like this:
for f in `ls | egrep 'seascaperecovered.*\.jpg'`;
do mv $f ${f/seascaperecovered/seascape};
done
i have read that you can do this with mv, rename, sed, awk, etc.
can someone point me to the easiest (and clearest, hopefully) way of accomplishing this in UNIX?
FWIW, I am ssh'd into a Linux machine and running a bash shell.
thanks,
jml
Very straightforward:
for i in seascaperecovered*.jpg; do A=${i/crop/}; mv $i ${A/recovered/_}; done
(Put echo before the mv first for a dry run.)
With bash regular expressions
for file in *; do
[[ "$file" =~ [0-9]+ ]] && mv "$file" seascape_${BASH_REMATCH[0]}.jpg
done

Bash command to remove leading zeros from all file names

I have a directory with a bunch of files with names like:
001234.jpg
001235.jpg
004729342.jpg
I want to remove the leading zeros from all file names, so I'd be left with:
1234.jpg
1235.jpg
4729342.jpg
I've been trying different configurations of sed, but I can't find the proper syntax. Is there an easy way to list all files in the directory, pipe it through sed, and either move or copy them to the new file name without the leading zeros?
for FILE in `ls`; do mv $FILE `echo $FILE | sed -e 's:^0*::'`; done
sed by itself is the wrong tool for this: you need to use some shell scripting as well.
Check Rename multiple files with Linux page for some ideas. One of the ideas suggested is to use the rename perl script:
rename 's/^0*//' *.jpg
In Bash, which is likely to be your default login shell, no external commands are necessary.
shopt -s extglob
for i in 0*[^0]; do mv "$i" "${i##*(0)}"; done
Maybe not the most elegant but it will work.
for i in 0*
do
mv "${i}" "`expr "${i}" : '0*\(.*\)'`"
done
Try using sed, e.g.:
sed -e 's:^0*::'
Complete loop:
for f in `ls`; do
mv $f $(echo $f | sed -e 's:^0*::')
done
I dont know sed at all but you can get a listing by using find:
find -type f -name *.jpg
so with the other answer it might look like
find . -type f -name *.jpg | sed -e 's:^0*::'
but i dont know if that sed command holds up or not.
Here's one that doesn't require sed:
for x in *.jpg ; do let num="10#${x%%.jpg}"; mv $x ${num}.jpg ; done
Note that this ONLY works when the filenames are all numbers. You could also remove the leading zeros using the shell:
for a in *.jpg ; do dest=${a/*(0)/} ; mv $a $dest ; done
In Bash shell you can do:
shopt -s nullglob
for file in 0*.jpg
do
echo mv "$file" "${file##*0}"
done

Resources