[wpseo_breadcrumb]

Piping commands after a piped xargs

Solutons:


You are almost there. In your last command, you can use -I to do the ls correctly

-I replace-str

    Replace occurrences of replace-str in the initial-arguments with names read from standard input.  Also, unquoted blanks do not terminate input items; instead the separator is the newline character.  Implies -x and -L 1.

So, with

find . -type d -name "*log*" | xargs -I {} sh -c "echo {}; ls -la {} | tail -2"

you will echo the dir found by find, then do the ls | tail on it.

Just in addition to fredtantini and as a general clarification (since the docs are a bit confusing):

The xargs -I {} will take the ‘{}’ characters from the standard input and replace them with whatever comes in from the pipe. This means you could actually replace {} with any character combination (maybe to better suite your preferred programming flavor). For example: xargs -I % sh -c "echo %". If you always use the xargs -I {} you can replace it with xargs -i as it is the shorthand. EDIT: The xargs -i option has been deprecated, so stick to the xargs -I{}.

The sh -c will tell your bash/shell to read the next command from a string and not from the standard input. So writing sh -c "echo something" is equivalent to echo something.

The xargs -I {} sh -c "echo {}" will read the input you created with sh -c which is echo {}. Since you told it to replace {} with the arguments you got from the pipe, that’s what will happen.

You can easily test this even without piping, just type the above command in a terminal. Whatever you write next will get outputted to the terminal (Ctrl-D to exit).

In the ls -la {} command the same thing happens again. The {} is replaced with the contents of the pre-pipe command.

GNU Parallel makes this kind of tasks easy:

find . -type d -name "*log*" | parallel --tag "ls -la {} | tail -2"

If you do not want to do a full install of GNU Parallel you can do a minimal installation: http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Related Solutions

What does __all__ mean in Python?

Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py...

Is the linux kernel ported to JavaScript yet?

Javascript is not a systems programming language, it is not appropriate for a kernel. Additionally, the kernel is a very large body of code, and "porting" it to another language is not something that can be done easily, and would likely take years. If the...

How to insert (file) data into a PostgreSQL bytea column?

as superuser: create or replace function bytea_import(p_path text, p_result out bytea) language plpgsql as $$ declare l_oid oid; begin select lo_import(p_path) into l_oid; select lo_get(l_oid) INTO p_result; perform lo_unlink(l_oid); end;$$; lo_get was...

What is the best color combination for on screen reading?

Legibility depends on high contrast between foreground and background, so black-and-white is the safest bet. See for example: Hall RH & Hanna H 2003. The Impact of Web Page Text-Background Color Combinations on Readability, Retention, Aesthetics, and...

MATCH FULL vs MATCH SIMPLE in foreign key constraints

Check the CREATE TABLE page of the manual: There are three match types: MATCH FULL, MATCH PARTIAL, and MATCH SIMPLE (which is the default). MATCH FULL will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null; if...

JavaScript set object key by variable

You need to make the object first, then use [] to set it. var key = "happyCount"; var obj = {}; obj[key] = someValueArray; myArray.push(obj); UPDATE 2021: Computed property names feature was introduced in ECMAScript 2015 (ES6) that allows you to dynamically...

What is the difference between const and readonly in C#?

Apart from the apparent difference of having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the constructor exits. After that it is frozen. const's are implicitly...

How should I index a UUID in Postgres?

Use PostgreSQL's built-in uuid data type, and create a regular b-tree index on it. There is no need to do anything special. This will result in an optimal index, and will also store the uuid field in as compact a form as is currently practical. (Hash indexes in...

Top level domain/domain suffix for private network?

Since the previous answers to this question were written, there have been a couple of RFCs that alter the guidance somewhat. RFC 6761 discusses special-use domain names without providing specific guidance for private networks. RFC 6762 still recommends not...

Getting last modification date of a PostgreSQL database table

There is no reliable, authorative record of the last modified time of a table. Using the relfilenode is wrong for a lot of reasons: Writes are initially recorded to the write-head log (WAL), then lazily to the heap (the table files). Once the record is in WAL,...

How do I make this sed script a “one liner”?

An ANSI C string -- with $'' -- can contain backslash escapes, like \n -- so you can have a newline in sed's arguments while still having the shell command invoking sed be only one line. sed -i $'/INTERPRETER_PYTHON_DISTRO_MAP/,/version_added/ {\n /default/a\\...

How to OCR a PDF file and get the text stored within the PDF?

ocrmypdf does a good job and can be used like this: ocrmypdf in.pdf out.pdf To install: pip install ocrmypdf or sudo apt install ocrmypdf # ubuntu sudo dnf -y install ocrmypdf # fedora After learning that Tesseract can now also produce searchable PDFs, I found...

If …Else If wont work [closed]

First of all, put the javascript code between a <script></script> tag because javascript code will not run in an html <div></div> tag. Then, instead of x == 0||9||2, use x == 0 || x == 9 || x == 2. Kindly indent your code for easier...

How to convert the object of character to string

Your object of characters is already almost an array. It has numeric indices, but is missing the .length property. If you add that it, it will be an "array like" object, which can then be passed to Array.from to get a proper array. Once you have a real array,...

How can I write the approximate value of PI?

Because your { and } is wrong. I think brackets will be as given below If the formula is PI = 4/1 - 4/3 + 4/5 - 4/7 + ... ( Leibniz's Series ) then you can formalate as given below #include <iostream> using namespace std; int main() { double n, i; //...