sivakarthik sivakarthik - 2 years ago 64
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

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
echo $entry

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

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

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

And maybe, you can simplify it:

for i in my/path/*.py.x
    mv "$i" "${i%.*}";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download