1

I want to trim this string /tmp/files/ from a variable $FILES For example:

setenv FILES=/tmp/files/list
ONLY_L=`trim($FILES,'/tmp/files/')`
echo $ONLY_L
#should see only 'list'

I though of using sed for the job, but it look a little "ugly" because all the \ that came before the /.

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
Nir
  • 2,497
  • 9
  • 42
  • 71

4 Answers4

3

For sed, you don't have to use /

For instance, this works as well:

echo $FILES | sed 's#/tmp/files/##'
csiu
  • 3,159
  • 2
  • 24
  • 26
1

ONLY_L="${FILES##*/}"

or

ONLY_L="$(basename "$FILES")"

or

ONLY_L="$(echo "$FILES" | sed 's|.*/||')"

does what you want

Vampire
  • 35,631
  • 4
  • 76
  • 102
1

You should use the basename command for this. It automatically removes the path and leaves just the filename:

basename /tmp/files/list

Output:

list
Kevin
  • 2,112
  • 14
  • 15
0

You don't need sed or call tools. bash provides you this ability using string substitution.

$ FILES='/tmp/files/list'

# do this
$ echo "${FILES/\/tmp\/files\/}"
list

# or this
$ echo "${FILES##*/}"
list 
jaypal singh
  • 74,723
  • 23
  • 102
  • 147