Hi,
I have a directory with the following file names
01 - abc hyn
02-def
03-ghi[www.Unix3333.com].dir
04 - jhu[www.Unix.com].dir
abc1 kil
def bil
The last two file names abc1 starts with one space and def starts with double space. I want these files in my directory to be renamed as
ABC HYN
DEF
GHI.dir
JHU.dir
ABC1 KIL
DEF BIL
Thanks
If you have a list of the files you want renamed in a file named "list", the following script should work:
#!/bin/ksh
IFS=""
while read old
do
new=${old##*([-0-9 ])}
new=${new/\[*\]/}
nds=${new%.dir}
if [ "${nds}.dir" == "$new" ]
then
suf=.dir
else
suf=
fi
new=$(printf "%s\n" "$nds"|tr "[:lower:]" "[:upper:]")
mv "$old" "$new$suf"
done < list
Note that this assumes that there is no more than one occurrence of [...] in a line.
This seems like a pretty strange set of conversions???
1 Like
Hi Don,
How can I run this script inside a folder?
Because all those are files inside a directory with those names.
Do you want to apply this naming convention to ALL files in the directory; or only to a selected list of files?
1 Like
All files in the directory
Install the following in your $HOME/bin directory as a filed named "namechange":
#!/bin/ksh
IFS=""
while read old
do
new=${old##*([-0-9 ])}
new=${new/\[*\]/}
nds=${new%.dir}
if [ "${nds}.dir" == "$new" ]
then
suf=.dir
else
suf=
fi
new=$(printf "%s\n" "$nds"|tr "[:lower:]" "[:upper:]")
mv "$old" "$new$suf"
done
and make it executable. Then issue the commands:
cd directory
ls | $HOME/bin/namechange
where "directory" is the name of the directory containing the files you want to rename.
1 Like