Operating System - HP-UX
1834051 Members
2425 Online
110063 Solutions
New Discussion

Unix Command needed or Procedure

 
SOLVED
Go to solution
Engo Armand-Blaise
Frequent Advisor

Unix Command needed or Procedure

Is there any Unix command or procedure to check the last character of a file-name? If it is "." then rename this file-name without this "."

example :

file-name = "S.O."

the last character is ".", so to rename S.O. by S.O

=============================================

Thank you for your assistance
9 REPLIES 9
Armin Feller
Honored Contributor

Re: Unix Command needed or Procedure

You can use the find command:

root /tmp >ll
total 108520
drwx------ 2 root root 1024 Feb 6 16:44 .AgentSockets
-rw-r--r-- 1 root root 23 Feb 6 16:44 96.sh
-rw-r----- 1 root sys 55511040 Feb 18 13:00 Ignite-UX-11-00.depot

prw-rw-rw- 1 root root 0 Feb 6 16:44 dhcpfifo.any
prw-rw-rw- 1 root root 0 Feb 6 16:44 dhcpfifo.root
-rw-rw-rw- 1 root root 16 Feb 6 16:45 last_uuid
-r--r--r-- 1 root root 0 Feb 6 16:44 llbdbase.dat
drwxrwxrwx 2 afeller users 1024 Nov 15 11:26 modemlog
root /tmp >find . -name '*t'
./dhcpfifo.root
./llbdbase.dat
./Ignite-UX-11-00.depot
root /tmp >

Regards ...
Armin
James R. Ferguson
Acclaimed Contributor

Re: Unix Command needed or Procedure

Hi:

# echo "S.O."|sed -e 's/\.$//'

# echo $filename|sed -e 's/\.$//'

Regards!

...JRF...
Dietmar Konermann
Honored Contributor

Re: Unix Command needed or Procedure

filename=S.O.

if [[ -n ${filename##*[!.]} ]]; then
echo dot at the end
fi

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Engo Armand-Blaise
Frequent Advisor

Re: Unix Command needed or Procedure

Thank you for your help. would make sure that I've made a mistake :

cyb_hpux [ /consult/6/runs ] # export filename="S.O."
cyb_hpux [ /consult/6/runs ] # echo $filename
S.O.
cyb_hpux [ /consult/6/runs ] # echo $filename|sed -e 's^.$//'
sed: Function s^.$// cannot be parsed.


Thanks.
john korterman
Honored Contributor

Re: Unix Command needed or Procedure

Hi Engo,
you can also list file names ending in dot with grep, e.g.:

# ls armand_blaise
file1 file1. file2 file3

# ls armand_blaise/ | grep "\.$"
file1.

but before renaming you should perhaps check if the filename without the dot already exists.

regards,
John K.
it would be nice if you always got a second chance
Dietmar Konermann
Honored Contributor

Re: Unix Command needed or Procedure

Ah... you wanted to rename.

# mv "$filename" "${filename%.}"

"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Robin Wakefield
Honored Contributor

Re: Unix Command needed or Procedure

hi,

for file in `ls * | grep '\.$'`;do
echo mv $file ${file%.}
done


rgds, Robin
James R. Ferguson
Acclaimed Contributor
Solution

Re: Unix Command needed or Procedure

Hi (again):

You typed:

# echo $filename|sed -e 's^.$//'

...but you should have:

# echo "$filename"|sed -e 's/\.$//'

I choose to use 'sed' but Dietmar's use of the shell's builtins is faster.

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: Unix Command needed or Procedure

Hi,
As I can see you have got a solution already but perhaps you should have a look at the "basename" command which can be used for removing suffixes.
Examole:
# >S.O.
# ls S.O*
S.O.
# basename S.O. .
S.O
# mv S.O. `basename S.O. .`
# ls S.O*
S.O