top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to separate out the directory and file name using shell script?

+1 vote
564 views

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

posted Oct 16, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer
#!/bin/bash
echo "Enter any file name or file path"
read fspec
filename="${fspec##*/}"
echo $filename
dirname="${fspec%/*}"
echo $dirname

Try this.

answer Oct 17, 2013 by Satyabrata Mahapatra
Thanks, let me try...
Its working perfectly without any issue, thanks for the help.
Similar Questions
+2 votes

I am using below script to validate IPV6 address but this script also pass invalid IP such as empty string, name etc.

if [[ "$IPv6ADDR"=~"^:?([a-fA-F0-9]{1,4}(:|.)?){0,8}(:|::)?([a-fA-F0-9]{1,4}(:|.)?){0,8}$" ]]; then
  echo "valid ipv6 addr"
  break;
else
  echo "Invalid IP Address"
  break;
fi

Can someone identify what's wrong in the regex, Please?

0 votes
...