Solved

How do you pull out the file name from a full path in Unix ksh?

Posted on 2006-11-30
2
458 Views
Last Modified: 2013-12-26
How do you pull out the file name from a full path in Unix ksh? I only want the file name and cannot remember how I have do this in the past. Below is a sample of my code snippet. I want to preserve the original file name.

-----------------------------------
for fn in `ls -1p $source_dir/*`
do

  IFS=":"
  outFile=$fn.san

  print "Sanitizing File:$fn"

  cat $fn | while read record

  ## Do stuff

  mv $fn to $fn.san
done

0
Comment
Question by:rayskelton
2 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 125 total points
ID: 18045151
basename $fullpath


e.g.

$ mkdir -p /tmp/a/long/path/with/some/files
$ touch /tmp/a/long/path/with/some/files/filename.txt
$ basename /tmp/a/long/path/with/some/files/filename.txt
filename.txt


( "dirname" does the opposite - i.e. just get the directory of a given file...)

i.e.

$ dirname /tmp/a/long/path/with/some/files/filename.txt
/tmp/a/long/path/with/some/files
0
 

Author Comment

by:rayskelton
ID: 18045626
This works great, thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question