4

I want to write a piped sequence of commands which prints out a list of all the subdirectories of the given directory, and printed in one line, following a new line.

I'm having trouble to make the output of the following command to be all at the same line, and then have a new line at the end.

This is my pipe command:

ls -l /share/ |grep ^d |tr -s ' ' |cut -f9 -d' '| tr '\n' ' ' 

And I would like the output to be as:

file1 file3
[user@linux]$

And not (what I actually get:

file1 file3[user@linux]$
2
  • 2
    What is $/share/?
    – A.B.
    Nov 16, 2015 at 8:13
  • I want to write a piped sequence of commands which prints out a list of all the subdirectories of the given directory, and printed in one line, following a new line. Nov 16, 2015 at 8:29

5 Answers 5

7

What about:

ls -l $/share/ |grep ^d |tr -s ' ' |cut -f9 -d' '| tr '\n' ' ' && echo " "
1
  • 4
    It's not necessary to pass any arguments to echo. It will print an empty new line without arguments.
    – Byte Commander
    Nov 16, 2015 at 8:40
4

It seems you want to get only directory names, if so you can use find:

find /share -maxdepth 1 -type d -printf '%f\n'

Example:

% find /usr -maxdepth 1 -type d -printf '%f\n'
usr
include
lib
share
sbin
local
bin
games
lib32
src

Original answer:

Use an intelligent shell like zsh or use printf:

printf '%s\n' "$(ls -l /share/ |grep ^d |tr -s ' ' |cut -f9 -d' '| tr '\n' ' ')"

Take a look at help printf.

3
  • I can't use printf. (These were the instructions) Nov 16, 2015 at 8:30
  • @user2953423 check my edits
    – heemayl
    Nov 16, 2015 at 8:37
  • 1
    -printf '%f\n' is great. But I thought OP needs all in one line? =)
    – A.B.
    Nov 16, 2015 at 9:01
2

The simplest solution would just be to add a plain echo command without any arguments to the end of the command:

ls -l $/share/ |grep ^d |tr -s ' ' |cut -f9 -d' '| tr '\n' ' ' ; echo

If you don't like the commands to be linked with ;, you could also use:

echo $(ls -l $/share/ |grep ^d |tr -s ' ' |cut -f9 -d' '| tr '\n' ' ')
0
1

Why ls, grep, cut, ...?

Use awk and find:

find /usr/share -maxdepth 1 -type d | awk -F/ '{printf "%s ",$NF} END {print ""}'
2
  • Thanks, but these were the instructions.. :) Nov 16, 2015 at 8:34
  • 4
    The instructions? We make your homework?
    – A.B.
    Nov 16, 2015 at 8:39
1

I want to write a piped sequence of commands which prints out a list of all the subdirectories of the given directory, and printed in one line, following a new line.

You don't need a piped sequence of command for that; you can use Bash's filename expansion Incidentally you'll need a piped sequence of commands for that, since apparently you don't want to print the trailing "/":

echo */ | tr -d '/'
  • echo */: prints */, which is a globbing pattern which is expanded to any folder / file in the current working directory ending with / (thus only to folders);
  • tr -d '/': deletes the trailing "/". There's no concern that "/" could appear in the file name since "/" is not a valid character for a filename.
% mkdir dir{1..3}
% touch file{1..3}
% tree
.
├── dir1
├── dir2
├── dir3
├── file1
├── file2
└── file3

3 directories, 3 files
% echo */ | tr -d '/'
dir1 dir2 dir3
%
1
  • 1
    +1 for the simplest command. Great idea, did not know that myself. ;-)
    – Byte Commander
    Nov 16, 2015 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.