0

I am trying to list all the directories inside another directory and put it onto a file using sed.

Command Used:

ls -ld <directory path>/* | sed 's/^.*\(what.*\).*\//\1/' 

What it print onto terminal:

what_111 what_222 

When I put it onto a file, this is what I see: Un-visible characters getting put onto file.

what_111^[[0m^[[K what_222^[[0m^[[K 

How do I remove them from sed output while putting onto file ? Any comments would be helpful.

8
  • superuser.com/questions/380772/… Commented Jun 15, 2021 at 19:48
  • Yes it did work with the following command: ls -ld <directory path>/* | sed -u 's/^.*(test.*).*\//\1/' | sed -u 's/\x1b[[0-9;]*[a-zA-Z]//g' This works fine on the terminal but when I put it on the shell script the sed isn't working. Any issue ? Commented Jun 15, 2021 at 20:39
  • 1
    Do not parse ls. Why do you use ls and then remove colors? Just do not use ls.... You are asking XY question. Commented Jun 15, 2021 at 20:45
  • @KamilCuk Well I am trying to get the list of directories inside another directory and putting in the file.. While doing so faced saw this un-viewable symbols on the text file.. TO resolve that Used the sed. Commented Jun 15, 2021 at 20:48
  • 1
    on the text file No, on ls output. Does the filename itself contains unprintable characters or does ls output them? When I put it onto a file How do you "put it onto a file"? Un-visible characters getting put onto file. How do you inspect the file content? Commented Jun 15, 2021 at 20:49

3 Answers 3

1

You seem to be starting off on the wrong track (trying to parse the output of ls and, I'd bet, having ls aliased to ls --color) and then trying to add code to fix the problems that you caused by doing so. To list the names of directories under a directory just do this:

find /path/to/directory -maxdepth 1 -type d -printf '%P\n' 
Sign up to request clarification or add additional context in comments.

Comments

0

I am trying to list all the directories inside another directory and put it onto a file using sed.

That's not possible to do with sed - sed is a "Stream EDitor", it can't list directories.

Do not parse ls..

find the directories.

find another_directory/ -maxdepth 1 -mindepth 1 -type d > a_file 

3 Comments

But this above command prints the entire path of the directories. I just wanted to print the names of the directory alone inside the other directory [not the full hierarchical path] and also not all the sub-directories inside the other.
find ... -printf '%f\n' see man find. Or find ... | sed 's~.*/~~'. or cd another_directory; find . .... | cut -c3-
Tried.. Like.. find <dirpath>/ -maxdepth 1 -type d |sed "s/^.*(test.*).*\//\1/" But still the sed doesn't remove of all the details before test, but the same sed works with ls output.
0

Tried combing some of the response above and got it working. Below is the command I tried and its working.

find <directory path> -maxdepth 1 -type d | sed 's/^.*\(test.*\)/\1/g' |tail -n +2 > file_list.txt 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.