khanna
khanna

Reputation: 765

remove the forward slash from ls in bash

I really tried it a lot, I know how sed works,

But I have a file, say ls -t -d */ which gives me

15072301/
15072300/
15072223/
15072222/
15072221/

Basically I want to get rid of this / in the end of each line. Using sed itself? I know I can handle it otherwise too, like taking a substring but there has to be an sed answer to this?

Upvotes: 0

Views: 1650

Answers (1)

tripleee
tripleee

Reputation: 189958

The sed way to remove a trailing slash from each line would be

sed 's:/$::' file

Notice that ls is actually not providing any value here, assuming the directory names correspond to when they were last modified. The shell will expand the glob in sorted order. So you might as well use

printf '%s\n' */ | sort -r | sed 's:/$::'

You should generally not use ls in scripts. See http://mywiki.wooledge.org/ParsingLs for a discussion.

Upvotes: 6

Related Questions