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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Vimo
  • 1,061
  • 3
  • 12
  • 22
  • https://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream – KamilCuk Jun 15 '21 at 19:48
  • Yes it did work with the following command: ls -ld /* | 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 ? – Vimo Jun 15 '21 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. – KamilCuk Jun 15 '21 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. – Vimo Jun 15 '21 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? – KamilCuk Jun 15 '21 at 20:49
  • When I execute in the terminal I dont see the characters. But when I put the output onto a file > file.txt, then I see those characters.. I am using the sed because I wanted to print only the end directory name and not the entire path of the directory. – Vimo Jun 15 '21 at 20:50
  • ls -ld /* | sed 's/^.*\(test.*\).*\//\1/' | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g ' > file_list.txt – Vimo Jun 15 '21 at 20:52
  • Why is the string `(test.*)` showing up in the sed scripts in your comments, e.g. `sed 's/^.*(test.*).*\//\1/'`? That seems completely unrelated to anything mentioned in your question. – Ed Morton Jun 16 '21 at 22:31

3 Answers3

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'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 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. – Vimo Jun 15 '21 at 20:54
  • `find ... -printf '%f\n'` see `man find`. Or `find ... | sed 's~.*/~~'`. or `cd another_directory; find . .... | cut -c3-` – KamilCuk Jun 15 '21 at 21:04
  • Tried.. Like.. find / -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. – Vimo Jun 15 '21 at 21:08
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
Vimo
  • 1,061
  • 3
  • 12
  • 22