0

From the line

/mnt/sampleserver/test_1/myfile.pdf

in my text file I want to remove

/mnt/sampleserver/test_1/

Please suggest me how to do it using linux commands.

1
  • Use basename Commented Jun 12, 2013 at 8:42

4 Answers 4

3

Use the 'basename' command. Something like this: basename /mnt/sampleserver/test_1/myfile.pdf

Sign up to request clarification or add additional context in comments.

4 Comments

basename will give myfile.pdf
@fedorqui yeah, the OP says “remove”, not “extract”.
Yes - he wanted to remove /mnt/sampleserver/test_1/
Oooops my fault, sorry I understood it the other way round.
0

Several ways:

file="/mnt/sampleserver/test_1/myfile.pdf"

$ echo ${file##*/}
myfile.pdf

$ echo $file | cut -d/ -f5
myfile.pdf

$ echo $file | awk -F/ '{print $5}'
myfile.pdf

Comments

0

You can use any text processing tool, for example, sed:

% echo "/mnt/sampleserver/test_1/myfile.pdf" | sed -e 's|/mnt/sampleserver/test_1/||'
myfile.pdf

Comments

0

Following should get you going. (Note the "\" for escape character)

/bin/sed -i "s//mnt/sampleserver/test_1///"

1 Comment

/bin/sed -i "s/\/mnt\/sampleserver\/test_1\////" file_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.