Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract filename from path in csh shell -- from list of files

Tags:

linux

shell

csh

How to extract filename from the path; i have a list of files. I'm using csh shell, and have awk, sed, perl installed.

/dfgfd/dfgdfg/filename

should give me

filename

I tried basename:

    find $PROJDIR -name '*.c' -o -name '*.cc' -o -name '*.h'
 | xargs grep -l pattern | xargs basename

and it gave me the following error:

basename: too few arguments Try `basename --help' for more information.

thx

like image 615
vehomzzz Avatar asked Sep 12 '25 08:09

vehomzzz


2 Answers

The standard program basename does what you want:

$ basename /dfgfd/dfgdfg/filename
filename
like image 176
Thomas Avatar answered Sep 13 '25 23:09

Thomas


This kind of workaround worked for me. You said you had perl so this should run. It replaces all nonspace text up to the last / with nothing (effectively deleting it).

find $PROJDIR -name '*.c' -o -name '*.cc' -o -name '*.h'
 | xargs grep -l pattern | perl -pi -e "s/\S+\///g"
like image 27
FModa3 Avatar answered Sep 13 '25 21:09

FModa3