How to get a new line at the end of pipe command

15,530

Solution 1

What about:

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

Solution 2

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.

Solution 3

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' ' ')

Solution 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.

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
%

Solution 5

Why ls, grep, cut, ...?

Use awk and find:

find /usr/share -maxdepth 1 -type d | awk -F/ '{printf "%s ",$NF} END {print ""}'
Share:
15,530

Related videos on Youtube

user2953423
Author by

user2953423

Updated on September 18, 2022

Comments

  • user2953423
    user2953423 over 1 year

    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]$
    
  • user2953423
    user2953423 over 8 years
    I can't use printf. (These were the instructions)
  • user2953423
    user2953423 over 8 years
    Thanks, but these were the instructions.. :)
  • Byte Commander
    Byte Commander over 8 years
    +1 for the simplest command. Great idea, did not know that myself. ;-)
  • heemayl
    heemayl over 8 years
    @user2953423 check my edits
  • A.B.
    A.B. over 8 years
    The instructions? We make your homework?
  • Byte Commander
    Byte Commander over 8 years
    It's not necessary to pass any arguments to echo. It will print an empty new line without arguments.
  • A.B.
    A.B. over 8 years
    -printf '%f\n' is great. But I thought OP needs all in one line? =)