sivakarthik sivakarthik - 7 months ago 12
Bash Question

shell script to list file names alone in a directory & rename it

I'm new to scripting concept.. I have a requirement to rename multiple files in a directory like

filename.sh.x
into
filename.sh


First I tried to get the file names in a particular directory.. so i followed the below scripting code

for entry in PathToThedirectory/*sh.x
do
echo $entry
done


& the above code listed down all the file names with full path..

But my expected o/p is : to get file names alone like
abc.sh.x
,
so that I can proceed with the split string mechanism to perform rename
operation easily...
help me to solve this ... Thanks in advance

Answer
for i in my/path/*.py.x
do
    basename=$(basename "$i")
    mv my/path/"$basename" my/path/"${basename%.*}"
done

And maybe, you can simplify it:

for i in my/path/*.py.x
do
    mv "$i" "${i%.*}";
done