Extracting filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting filenames
# 1  
Old 05-07-2010
Extracting filenames

Hi

I need to pull out the name of the file from the path.

See, here is my loop that gets the files:

Code:
dsxdir="/var/local/dsx/import"
 
for dsxfile in $dsxdir/*.dsx;
do

dsxlog $reverb --info --module="$module" "$dsxfile"
$dsximp $norule $oprange --dsn=$dsn --dbname=$dbname --user=datasafe --password=datasafe --dsxfile=$dsxfile

done

When I try to process only the filename, I get the entire path:

Code:
 
Now processing /var/local/dsx/import/gen_cell_orig_001.dsx

I only want "gen_cell_orig_001.dsx"

Can you please help.
# 2  
Old 05-07-2010
Have you tried /usr/bin/basename ?

Code:
# which basename
/usr/bin/basename
# basename /var/adm/messages
messages

HTH
# 3  
Old 05-07-2010
Not necessary to use an external command:

Code:
file="/var/local/dsx/import/gen_cell_orig_001.dsx"

Now processing ${file##*/}

# 4  
Old 05-07-2010
Thanks Franklin52 .... it works like a charm .....
Can you please explain to me exactly what happens in that call ... sorry but I'm not too good with scripting
# 5  
Old 05-07-2010
Quote:
Originally Posted by ladyAnne
Thanks Franklin52 .... it works like a charm .....
Can you please explain to me exactly what happens in that call ... sorry but I'm not too good with scripting
Have a read of:

Learning the Korn Shell, 2nd Edition: Chapter 4: Basic Shell Programming

or:

Manipulating Strings
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 05-07-2010
$ echo "/var/local/dsx/import/gen_cell_orig_001.dsx"| cut -d '/' -f6
gen_cell_orig_001.dsx
This User Gave Thanks to mr_harish80 For This Post:
# 7  
Old 05-07-2010
Another well known approach

Code:
$> file="/var/local/dsx/import/gen_cell_orig_001.dsx"; echo $(basename "$file")
gen_cell_orig_001.dsx

This User Gave Thanks to kchinnam For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort the filenames

Hello Unix experts: I have dir where few files are there, i want to sort these files and write the output to some other file but i need filenames with filepath too eg: i have filenames like 010020001_S-FOR-Sort-SYEXC_20171218_094256_0004.txt so i want to sort my files on first 5 fields of... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

2. Shell Programming and Scripting

Rename Filenames

Hi there I have thousands files like: SG1130113000247.CAPNFXS SG1130113001247.CAPNFXT SG1130113002247.CAPNFXU . . . I want to remove SG1 and .CAP* from file name, and rename it to: 130113000247 130113001247 130113002247 (9 Replies)
Discussion started by: ali.seifaddini
9 Replies

3. Slackware

cp does not like filenames with accents?

Hi: mkisofs -graft-points -rational-rock -joliet -joliet-long -full-iso9660-filenames -iso-level 2 -o /tmp/image.iso STORE1/=/almacen/strauss In /almacen/strauss there are filenames containing not only spaces but accented characters as well. I burned the image to DVD, with the result that all... (2 Replies)
Discussion started by: stf92
2 Replies

4. Shell Programming and Scripting

Manipulating Filenames

Hi Folks, I'm looking for some ideas on how to change some file names. I'm pretty sure I need to use sed or awk but they still escape me. The files I have are like: VOD0615 NEW Blades R77307.pdf or VOD0615_NEW_Blades_R77307.pdf and what I want after processing is: R77307 NEW Blades.pdf ... (5 Replies)
Discussion started by: imonkey
5 Replies

5. UNIX for Dummies Questions & Answers

renaming filenames

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (0 Replies)
Discussion started by: vpv0002
0 Replies

6. UNIX for Dummies Questions & Answers

parsing filenames

How can I loose a part of the filename I want to drop the “_<Number>.sql” Below I have a listing of file names in a file Eg : CREDIT_DEL_033333.sql I want it to be CREDIT_DEL ATM_DEBIT_CARD_0999999.sql I want it to be ... (3 Replies)
Discussion started by: jville
3 Replies

7. UNIX for Dummies Questions & Answers

Backslashes in Filenames

Using a small script, I automatically generated some text logs. The files ended being undownloadable, unopenable and undeletable. Upon further investigation, the files ended up looking like this: log\r log2\r log3\r I've tried a few different things, including double slashing before the... (6 Replies)
Discussion started by: shepherdsflock
6 Replies

8. UNIX for Dummies Questions & Answers

extracting and using date from filenames in a loop

HIya, Having a dumb day whilst writing an archive process in Shell want to extract from the filename the date and archive into tar files based on this, I don't want to use mtime as it may not be the actual file date. The files are -rw-rw---- 1 user admin 100 Aug 29 11:10... (2 Replies)
Discussion started by: badg3r
2 Replies

9. Shell Programming and Scripting

Patterns in Filenames

Hi, To start, I am using a bash shell on a G4 powerbook running Leopard. I am attempting to write a shell script that will automate the processing of satellite imagery. All the filenames are of the following construction: A2008196000500.L2 where A indicates the sensor, the next four... (6 Replies)
Discussion started by: msb65
6 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question