Discussion:
combine two commands via pipe
(too old to reply)
c***@free.fr
2024-07-25 23:10:01 UTC
Permalink
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
c***@free.fr
2024-07-25 23:30:01 UTC
Permalink
Post by c***@free.fr
Hello gurus,
$ 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.
$ 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
Greg Wooledge
2024-07-25 23:50:01 UTC
Permalink
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.
c***@free.fr
2024-07-26 00:10:01 UTC
Permalink
Post by Greg Wooledge
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
Greg Wooledge
2024-07-26 00:20:01 UTC
Permalink
Post by c***@free.fr
Post by Greg Wooledge
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.
c***@free.fr
2024-07-26 00:30:01 UTC
Permalink
There is only one subdir exists with chars ‘apache’ included in /tmp.

Regards
Post by Greg Wooledge
Post by c***@free.fr
Post by Greg Wooledge
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?
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
Max Nikulin
2024-07-26 01:50:01 UTC
Permalink
Post by c***@free.fr
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"
c***@free.fr
2024-07-26 02:10:01 UTC
Permalink
I found this works though it's ugly.

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

Thanks for all help.
Post by Max Nikulin
Post by c***@free.fr
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
Greg Wooledge
2024-07-26 02:10:01 UTC
Permalink
Post by c***@free.fr
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*
Andy Smith
2024-07-26 02:30:01 UTC
Permalink
Hi,
Post by c***@free.fr
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?
Post by c***@free.fr
Post by Max Nikulin
Use glob if it is acceptable
sudo ls -ltr /tmp/*-apache2.service-*
--
https://bitfolk.com/ -- No-nonsense VPS hosting
Max Nikulin
2024-07-26 02:50:01 UTC
Permalink
Post by Andy Smith
Post by c***@free.fr
$ sudo ls -ltr "/tmp/$(ls /tmp |grep apache)"
[...]
Post by Andy Smith
So what is wrong with just using a glob as suggested?
Not all people are realizing how many pitfalls they may face using
shell. (I admit my example with findmnt likely has a shortcoming as well.)

I am realizing that the following pages describe dealing with lists
while single directory is expected in the current case, but still

https://mywiki.wooledge.org/ParsingLs
Why you shouldn't parse the output of ls(1)

https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29
for f in $(ls *.mp3)
No 1 in Bash Pitfalls
Greg Wooledge
2024-07-26 03:10:02 UTC
Permalink
Post by Max Nikulin
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.
c***@free.fr
2024-07-26 03:40:03 UTC
Permalink
Post by Greg Wooledge
* 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
Alain D D Williams
2024-07-25 23:50:01 UTC
Permalink
Post by c***@free.fr
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>
mick.crane
2024-07-26 00:00:02 UTC
Permalink
Post by Alain D D Williams
Post by c***@free.fr
this could work indeed. but it requires me to input a long path. so I am
asking for a easier way.
$ sudo find /tmp -user apache2
I've recently been using catfish to search for things.
I don't know if it can be configured to list by user.
mick
Loading...