The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Renaming file names in a shell script rudoraj Shell Programming and Scripting 1 01-26-2009 08:05 PM
Dealing with spaces in file names in a shell script same1290 Shell Programming and Scripting 5 08-13-2008 12:01 AM
How do manipulate file path and names siegfried Shell Programming and Scripting 2 09-28-2007 12:20 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM
How can I delete files using a file that containt path and names? AlvaroD UNIX for Dummies Questions & Answers 2 09-25-2001 01:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-10-2009
crackthehit007 crackthehit007 is offline
Registered User
  
 

Join Date: Feb 2009
Location: Pune/Banagalore
Posts: 71
want only file names (not whole path) in shell script

hi i wrote following script,
#!/usr/bin/sh
for index in `ls /tmp/common/*.txt`
do
echo "$index"
done

here index is giving full path but in my program i want only file names (not along with whole path)

Eg. if in /tmp/common files are a.txt and b.txt den out should be a.txt b.txt

and not like ==> /tmp/common/a.txt /tmp/common/b.txt

any knows how to do it?
  #2 (permalink)  
Old 02-10-2009
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
use basename command.

man basename

Code:
BASENAME(1)                      User Commands                     BASENAME(1)

NAME
       basename - strip directory and suffix from filenames

SYNOPSIS
       basename NAME [SUFFIX]
       basename OPTION

DESCRIPTION
       Print  NAME with any leading directory components removed.  If specified, also remove
       a trailing SUFFIX.

       --help display this help and exit

       --version
              output version information and exit

EXAMPLES
       basename /usr/bin/sort
              Output "sort".

       basename include/stdio.h .h
              Output "stdio".

  #3 (permalink)  
Old 02-10-2009
need_help need_help is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
echo `basename $index`
  #4 (permalink)  
Old 02-10-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Another approach


Code:
echo "$index" | cut -d"/" -f4

But this will only work for the example you provided - that has that specific number of / characters in the fully qualified path.
  #5 (permalink)  
Old 02-10-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 388
use ksh variable substitution:


Code:
echo ${file##*/}

  #6 (permalink)  
Old 02-10-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
#!/bin/ksh

a="/a/b/c"

file=${a##*/}
dir=${a%/*}

echo "a->[${a}] dir->[$dir] file->[${file}]"

  #7 (permalink)  
Old 02-11-2009
crackthehit007 crackthehit007 is offline
Registered User
  
 

Join Date: Feb 2009
Location: Pune/Banagalore
Posts: 71
thanks a lot !
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0