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,546
  • 5
  • 41
  • 62
Nir
  • 2,217
  • 8
  • 36
  • 59

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,019
  • 2
  • 23
  • 26
1

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

or

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

or

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

does what you want

Vampire
  • 32,892
  • 3
  • 65
  • 94
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,104
  • 13
  • 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
  • 71,025
  • 22
  • 98
  • 142