Changing name to files in bash respecting the number associated to each file - bash

I have a bunch of files in bash named myfile1.sh, myfile2.sh, myfile3.sh... and so on. I would like to remove the sh part in each one of them, that is: rename to myfile1, myfile2, myfile3, ... Do you have a suggestion to do it all at once?
Thanks in advance.

for i in *.sh; do mv "$i" "${i%.sh}"; done

If you have the rename command, you can use it:
rename 's/\.sh$//' *.sh
A bash one-liner can be:
for f in *.sh; do mv "$f" "${f%.sh}"; done

Related

How to rename folders that contain consistent sub-string at the end using mv command?

I have many folders with the following pattern:
$ ls
a-master 123-master abc123-master
I want them to be:
$ ls
a 123 abc123
During my research, I found this answer. But when I ran mv "$f" "${f/-master/}", I got mv: rename to : No such file or directory error and I'm not sure why.
I found many answers recommending rename package but I don't prefer it. I think it's possible to be done just by using mv command but I'm not entirely sure how.
Does is possible? If possible, what is the correct command for this case?
I'm looking for one liner command, a short and sweet one.
I am fetching details from the shared link-
for f in opencv_*; do mv "$f" "${f/.so/}"; done
The mv command is working on a loop where f is defined as a temporary variable, whereas the query posted does not have a variable. (f == "")
Example,
mkdir abc123-master
export F=abc123-master
mv $F ${F/-master/}
ls -rlt
abc123

Rename file in a directory using shell

Suppose I have some files in a dir called test_dir, like a.sh, b.sh, c.sh, d.bash, and so on. I need to change all the file *.sh to *.bash. How can I achieve it using shell?
Can be done in a single one-liner:
ls *.sh | while read i; do mv "$i" "${i%.sh}.bash"; 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 {} +

script for changing prefix of filename in bash

From
abcd_if_vb.c
abcd_if_av.c
to
edfg_if_vb.c
edfg_if_av.c
The problem is to catch what file have abcd prefix.
Not really.
for file in abcd*
do
mv "$file" "edfg${file#abcd}"
done
If you have rename, you can rename those files using
rename abcd_ edfg_ abcd*
ls -1 abcd*|awk '{f=$0;gsub(f,"abcd","efgh");system("mv "$0" "f);}'

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

Resources