-1

I'm newbie for a shell script. So I need to cut the directory name from my $file. e.g.

fullpath='/var/log/apache/access.log

Now I need to cutting "apache" to output. How can I do.

Thank in advance.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
dimsum
  • 19
  • 1
  • 6
  • You're probably looking for `basename`. Have you searched StackOverflow for similar questions? You may find your answer by just searching a little more. Here is one, for example: http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash/965072#965072 – vastlysuperiorman Mar 27 '15 at 06:53
  • If it is the directory you're after, it's probably `dirname` you want. – Matijs Mar 27 '15 at 06:55
  • Valid point. Though from the example, he may want just "apache", not /var/log/apache, which would be the output of dirname. In that case `basename $(dirname $FULLPATH)` would give the desired output. – vastlysuperiorman Mar 27 '15 at 06:58

2 Answers2

0

The combination of basename and dirname will give you the parent directory of the file you provided in $fullpath

For example:

FULLPATH=/var/log/apache/access.log; basename $(dirname $FULLPATH)

will print "apache"

vastlysuperiorman
  • 1,694
  • 19
  • 27
-1

vim Filename

fullpath='/var/log/apache/access.log

then save and exit

cut -d'/' -f4 Filename

Ravi Bhushan
  • 253
  • 3
  • 17
  • This solution isn't very helpful as the number of directories in a full path is not constant. Taking the fourth field will not always yield a filename, pathname, or anything at all. – vastlysuperiorman Mar 27 '15 at 06:59