Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:21:53+00:00 2026-06-12T02:21:53+00:00

I have to remove .. character from a file in Bash script. Example: I

  • 0

I have to remove .. character from a file in Bash script. Example:

I have some string like:

some/../path/to/file
some/ab/path/to/file

And after replace, it should look like

some/path/to/file
some/ab/path/to/file

I have used below code

DUMMY_STRING=/../
TEMP_FILE=./temp.txt
sed s%${DUMMY_STRING}%/%g ${SRC_FILE} > ${TEMP_FILE}
cp ${TEMP_FILE} ${SRC_FILE}

It is replacing the /../ in line 1; but it is also removing the line /ab/ from second line. This is not desired. I understand it is considering /../ as some regex and /ab/ matches this regex. But I want only those /../ to be replaced.

Please provide some help.

Thanks,

NN

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:21:54+00:00Added an answer on June 12, 2026 at 2:21 am

    The . is a metacharacter in sed meaning ‘any character’. To suppress its special meaning, escape it with a backslash:

    sed -e 's%/\.\./%/%g' $src_file > $temp_file
    

    Note that you are referring to different files after you eliminate the /../ like that. To refer to the same name as before (in the absence of symlinks, which complicate things), you would need to remove the directory component before the /../. Thus:

    some/../path/to/file
    path/to/file
    

    refer to the same file, assuming some is a directory and not a symlink somewhere else, but in general, some/path/to/file is a different file (though symlinks could be used to confound that assertion).


    $ x="some/../path/to/file
    > some/ab/path/to/file
    > /some/path/../to/another/../file"
    $ echo "$x"
    some/../path/to/file
    some/ab/path/to/file
    /some/path/../to/another/../file
    $ echo "$x" | sed -e 's%/\.\./%/%g'
    some/path/to/file
    some/ab/path/to/file
    /some/path/to/another/file
    $ echo "$x" | sed -e "s%/\.\./%/%g"
    some/path/to/file
    some/ab/path/to/file
    /some/path/to/another/file
    $ echo "$x" | sed -e s%/\.\./%/%g
    some/path/file
    some/path/file
    /some/path/to/another/file
    $ echo "$x" | sed -e s%/\\.\\./%/%g
    some/path/to/file
    some/ab/path/to/file
    /some/path/to/another/file
    $
    

    Note the careful use of double quotes around the variable "$x" in the echo commands. I could have used either single or double quotes in the assignment and would have gotten the same result.

    Test on Mac OS X 10.7.4 with the standard sed (and shell is /bin/sh, aka bash 3.2.x), but the results would be the same on any system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string. How do I remove all text after a certain character?
I need to remove the character : from a file. Ex: I have numbers
I have a set of characters I want to remove from a string :
I have a file with characters like thi: ABCD...HHCCCCHH..BC Now, I want to remove
I have remove some item from the list of the BxSlider carousel. But when
I have to remove quotes from a relatively large text file. I have looked
I am writing a script to read a file and replace some characters which
I have the following PHP code to remove special characters from a variable; <?php
I have read up on remove the character 'u' in a list but I
I have to remove a branch from svn history for good. Normally I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.