want only file names (not whole path) in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want only file names (not whole path) in shell script
# 1  
Old 02-10-2009
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  
Old 02-10-2009
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  
Old 02-10-2009
echo `basename $index`
# 4  
Old 02-10-2009
Tools 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  
Old 02-10-2009
use ksh variable substitution:

Code:
echo ${file##*/}

# 6  
Old 02-10-2009
Code:
#!/bin/ksh

a="/a/b/c"

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

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

# 7  
Old 02-11-2009
thanks a lot ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Long file names within shell script

I am downloading a zip file that contain files that are very long. I am trying to process them, but cannot. I can move the files from one directory to another at the shell prompt, but not within a shell script, I get a stat error. The files look somewhat like this; ... (5 Replies)
Discussion started by: trolley
5 Replies

2. UNIX for Dummies Questions & Answers

Download .xls file from mail and copy to another path using shell script

Hi I have a mail attachment coming from a mail id and evreytime with the same name in .xls format.I have to download the .xls file into a location and convert it itno .csv format and copy the .csv file to another location. (1 Reply)
Discussion started by: bikky6
1 Replies

3. UNIX for Dummies Questions & Answers

Removing path name from list of file names

I have this piece of code printf '%s\n' $pth*.msf | tr ' ' '\n' | sort -t '-' -k7 -k6r \ | awk -F- '{c=($6$7!=p&&FNR!=1)?ORS:"";p=$6$7}{printf("%c%s\n",c,$0)}' When I run it I get /home/chrisd/tatsh/branches/terr0.50/darwin/n02-z30-dsr65-terr0.50-dc0.002-8x6drw-csq.msf... (8 Replies)
Discussion started by: kristinu
8 Replies

4. Shell Programming and Scripting

A shell script for create a a file in the respective path

Hello forum members, I have to create a out file in the current path./aaa/bbb/ccc/hhh. i am writing script below. ###script Begins##### #!/bin/ksh echo "Weclome" if then echo "Hello" rm -rf $aaa/bbb/ccc/hhh #clean the exsisting o/p file echo "no... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

5. Shell Programming and Scripting

Shell script to loop for a particular file in predefined path

Hi , i need a shell script to poll a particular file in a predefined directory , once that file is found , i need to launch another shell script . Eg i need to poll for A.txt in /home/username/Desktop once A.txt is created by Desktop(Created by another Script) , i need to launch another... (2 Replies)
Discussion started by: Vinod H C
2 Replies

6. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies

7. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

8. Shell Programming and Scripting

How do manipulate file path and names

In emacs elisp, there is a handy function called file-name-nondirectory which accepts a path and file name and returns just a file name and extension. There is also a function called file-name-directory which just returns the dire ctory name without the file. How can I implement these same... (2 Replies)
Discussion started by: siegfried
2 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. UNIX for Dummies Questions & Answers

How can I delete files using a file that containt path and names?

Recentily i receive virus ninda and my network was files *.eml. I find all *.eml with: find / -name *.eml -print > virus Virus has the path and name of the file,so, How can i delete all *.eml? Thanks (2 Replies)
Discussion started by: AlvaroD
2 Replies
Login or Register to Ask a Question