• combine two commands via pipe

    From coreyh@free.fr@21:1/5 to All on Fri Jul 26 01:10:01 2024
    Hello gurus,

    I have the following commands:

    $ ls /tmp/|grep apache2 systemd-private-653536fdd8d04538ab68da7469570d0c-apache2.service-UiHjaL

    $ sudo ls -ltr /tmp/systemd-private-653536fdd8d04538ab68da7469570d0c-apache2.service-UiHjaL total 4


    When I tried to run them in one line as follows,

    $ ls /tmp/|grep apache2|sudo ls -ltr

    It doesn't work as I expected.

    How should I do that correctly?

    Thank you.

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to coreyh@free.fr on Fri Jul 26 01:50:01 2024
    On Fri, Jul 26, 2024 at 07:29:10 +0800, coreyh@free.fr wrote:
    On 2024-07-26 07:14, Alain D D Williams wrote:
    Neither do you say what you are trying to achieve. Looking for files
    owned by
    apache in a directory ?

    yes.

    Does "owned by apache" mean literally the user "apache"? Or is it really
    the user "www-data"?

    I'm going to assume you mean www-data.

    This will give you the file and directory names:

    find /tmp -user www-data

    If you want more detail, add -ls to the end:

    find /tmp -user www-data -ls

    If you want the output to be sorted by the real ls(1), you have to
    get a bit fancier. Here's one way, but it relies on the number of
    pathnames being relatively small:

    find /tmp -user www-data -print0 | xargs -0 ls -ldtr

    If there are too many results, ls will be executed multiple times, and
    then you'll get a series of separately sorted chunks, all concatenated together.

    I won't go any fancier than this until I know it's actually needed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From coreyh@free.fr@21:1/5 to Alain D D Williams on Fri Jul 26 01:30:01 2024
    On 2024-07-26 07:14, Alain D D Williams wrote:
    On Fri, Jul 26, 2024 at 07:04:37AM +0800, coreyh@free.fr wrote:
    Hello gurus,

    I have the following commands:

    $ ls /tmp/|grep apache2
    systemd-private-653536fdd8d04538ab68da7469570d0c-apache2.service-UiHjaL

    $ sudo ls -ltr
    /tmp/systemd-private-653536fdd8d04538ab68da7469570d0c-apache2.service-UiHjaL >> total 4


    When I tried to run them in one line as follows,

    $ ls /tmp/|grep apache2|sudo ls -ltr

    It doesn't work as I expected.

    You do not tell us what you expect; however it seems that you not
    understand
    what you are trying to do. The 'ls' command does not read from stdin,
    so
    putting it at the end of a pipeline will mean that data in the pipe is ignored.

    Neither do you say what you are trying to achieve. Looking for files
    owned by
    apache in a directory ?

    yes.


    Maybe the following will do what you want:

    $ sudo ls -ltr /tmp/systemd-private-653536fdd8d04538ab68da7469570d0c-apache2.service-UiHjaL | grep apache2



    this could work indeed. but it requires me to input a long path. so I am
    asking for a easier way.

    Thanks.

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alain D D Williams@21:1/5 to coreyh@free.fr on Fri Jul 26 01:50:01 2024
    On Fri, Jul 26, 2024 at 07:29:10AM +0800, coreyh@free.fr wrote:

    this could work indeed. but it requires me to input a long path. so I am asking for a easier way.

    Try this:

    $ sudo find /tmp -user apache2

    --
    Alain Williams
    Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
    +44 (0) 787 668 0256 https://www.phcomp.co.uk/
    Parliament Hill Computers. Registration Information: https://www.phcomp.co.uk/Contact.html
    #include <std_disclaimer.h>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From coreyh@free.fr@21:1/5 to All on Fri Jul 26 02:10:01 2024

    I won't go any fancier than this until I know it's actually needed.

    My actual requirement is that I want to 'ls -ltr' into a subdir in /tmp.
    that subdir is apache's tmp dir. but the name of the subdir is too long
    (hard to copy&paste), so I am looking for a easier way.

    Thank you.

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From coreyh@free.fr@21:1/5 to Greg Wooledge on Fri Jul 26 02:30:01 2024
    There is only one subdir exists with chars ‘apache’ included in /tmp.

    Regards

    On 2024-07-26 08:14, Greg Wooledge wrote:
    On Fri, Jul 26, 2024 at 07:59:42 +0800, coreyh@free.fr wrote:


    I won't go any fancier than this until I know it's actually needed.

    My actual requirement is that I want to 'ls -ltr' into a subdir in
    /tmp.
    that subdir is apache's tmp dir. but the name of the subdir is too
    long
    (hard to copy&paste), so I am looking for a easier way.

    Then how do you KNOW which subdirectory to use?

    Is it the only one with "-apache" in its name? If so:

    ls -ltr /tmp/*-apache*

    Otherwise, please describe how you (as a human with a mind) know which directory it is. Then we can try to duplicate that reasoning feat
    with commands.

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to coreyh@free.fr on Fri Jul 26 02:20:01 2024
    On Fri, Jul 26, 2024 at 07:59:42 +0800, coreyh@free.fr wrote:


    I won't go any fancier than this until I know it's actually needed.

    My actual requirement is that I want to 'ls -ltr' into a subdir in /tmp.
    that subdir is apache's tmp dir. but the name of the subdir is too long
    (hard to copy&paste), so I am looking for a easier way.

    Then how do you KNOW which subdirectory to use?

    Is it the only one with "-apache" in its name? If so:

    ls -ltr /tmp/*-apache*

    Otherwise, please describe how you (as a human with a mind) know which directory it is. Then we can try to duplicate that reasoning feat
    with commands.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to coreyh@free.fr on Fri Jul 26 04:10:01 2024
    On Fri, Jul 26, 2024 at 10:00:48 +0800, coreyh@free.fr wrote:
    I found this works though it's ugly.

    $ sudo ls -ltr "/tmp/$(ls /tmp |grep apache)"
    total 4

    Just use a glob.

    sudo ls -ltr /tmp/*apache*

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From coreyh@free.fr@21:1/5 to Max Nikulin on Fri Jul 26 04:10:01 2024
    I found this works though it's ugly.

    $ sudo ls -ltr "/tmp/$(ls /tmp |grep apache)"
    total 4

    Thanks for all help.

    On 2024-07-26 09:42, Max Nikulin wrote:
    On 26/07/2024 06:59, coreyh@free.fr wrote:

    My actual requirement is that I want to 'ls -ltr' into a subdir in
    /tmp. that subdir is apache's tmp dir. but the name of the subdir is
    too long (hard to copy&paste), so I am looking for a easier way.

    Use glob if it is acceptable

    sudo ls -ltr /tmp/*-apache2.service-*

    If you need a private tmp directory of a specific systemd service then
    try to find proper tools to query it

    service="bluetooth.service"
    pid="$(systemctl show --property MainPID --value "$service")"
    tmp="$(findmnt --task "$pid" --target /tmp --noheading --output FSROOT --raw)"
    ls -ltr "$tmp"

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andy Smith@21:1/5 to coreyh@free.fr on Fri Jul 26 04:30:01 2024
    Hi,

    On Fri, Jul 26, 2024 at 10:00:48AM +0800, coreyh@free.fr wrote:
    I found this works though it's ugly.

    $ sudo ls -ltr "/tmp/$(ls /tmp |grep apache)"
    total 4

    Thanks for all help.

    If you appreciate help then engage with it. Two people now have
    suggested that you just use a glob, and you're replying directly to
    one of them without explaining why you aren't following their
    advice. So what is wrong with just using a glob as suggested?

    On 2024-07-26 09:42, Max Nikulin wrote:
    Use glob if it is acceptable

    sudo ls -ltr /tmp/*-apache2.service-*

    --
    https://bitfolk.com/ -- No-nonsense VPS hosting

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Max Nikulin on Fri Jul 26 05:10:02 2024
    On Fri, Jul 26, 2024 at 09:44:52 +0700, Max Nikulin wrote:
    https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29
    for f in $(ls *.mp3)
    No 1 in Bash Pitfalls

    I added nicer anchors, which you can use if you prefer:

    https://mywiki.wooledge.org/BashPitfalls#pf1

    The auto-generated anchor also works, of course, but is far less pleasing
    to read.

    This particular pitfall isn't an exact match for the OP's situation,
    but it's tangentially related. The OP is explicitly insisting that
    the /tmp directory will always and forever have exactly one entry
    containing "apache" in its name. This is a poor assumption; *anyone*
    can make another file or subdirectory in /tmp, and they could put
    the word apache in its name.

    Therefore, it's possible that $(ls /tmp | grep apache) might return
    more than one result. This will break the OP's "/tmp/$(ls ...)"
    solution. The glob alternative will still work, however.

    So, in summary, the glob solution:

    * Is shorter.
    * Is easier to read and understand.
    * Is more efficient.
    * Doesn't break if someone creates /tmp/apache420.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From coreyh@free.fr@21:1/5 to All on Fri Jul 26 05:40:03 2024

    So, in summary, the glob solution:

    * Is shorter.
    * Is easier to read and understand.
    * Is more efficient.
    * Doesn't break if someone creates /tmp/apache420.

    I know few about glob. But after checking the man page I think it is a
    good idea.

    Many thanks!

    --
    corey hickman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)