What is the meaning of : `ls -lt | grep - | head -1 | awk 'print $9' | xargs rm`2019 Community Moderator ElectionWhat is a simple explanation for how pipes work in Bash?Shell command to sum integers, one per line?What do 'real', 'user' and 'sys' mean in the output of time(1)?Is PowerShell ready to replace my Cygwin shell on Windows?In the shell, what does “ 2>&1 ” mean?How do I split a string on a delimiter in Bash?What is the meaning of “POSIX”?Difference between sh and bashawk print column $3 if $2==a specific value?What does set -e mean in a bash script?awk after grep: print value when grep returns nothing
Do people actually use the word "kaputt" in conversation?
Why didn’t Eve recognize the little cockroach as a living organism?
If I cast the Enlarge/Reduce spell on an arrow, what weapon could it count as?
How can I create URL shortcuts/redirects for task/diff IDs in Phabricator?
Print a physical multiplication table
Is there any common country to visit for uk and schengen visa?
Why does Surtur say that Thor is Asgard's doom?
What are the rules for concealing thieves' tools (or items in general)?
Emojional cryptic crossword
How much propellant is used up until liftoff?
Why is participating in the European Parliamentary elections used as a threat?
Can other pieces capture a threatening piece and prevent a checkmate?
Print last inputted byte
Homology of the fiber
Why is this tree refusing to shed its dead leaves?
Hackerrank All Women's Codesprint 2019: Name the Product
Why do I have a large white artefact on the rendered image?
Is xar preinstalled on macOS?
How to remove space in section title at KOMA-Script
PTIJ: Which Dr. Seuss books should one obtain?
Do I need to convey a moral for each of my blog post?
Help with identifying unique aircraft over NE Pennsylvania
How to test the sharpness of a knife?
Did Nintendo change its mind about 68000 SNES?
What is the meaning of : `ls -lt | grep - | head -1 | awk 'print $9' | xargs rm`
2019 Community Moderator ElectionWhat is a simple explanation for how pipes work in Bash?Shell command to sum integers, one per line?What do 'real', 'user' and 'sys' mean in the output of time(1)?Is PowerShell ready to replace my Cygwin shell on Windows?In the shell, what does “ 2>&1 ” mean?How do I split a string on a delimiter in Bash?What is the meaning of “POSIX”?Difference between sh and bashawk print column $3 if $2==a specific value?What does set -e mean in a bash script?awk after grep: print value when grep returns nothing
What is the meaning of this command: ls -lt | grep - | head -1 | awk 'print $9' | xargs rm.
I know the individual meaning of these commands but what happens when we join them by pipe?
linux shell unix pipeline
add a comment |
What is the meaning of this command: ls -lt | grep - | head -1 | awk 'print $9' | xargs rm.
I know the individual meaning of these commands but what happens when we join them by pipe?
linux shell unix pipeline
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21
add a comment |
What is the meaning of this command: ls -lt | grep - | head -1 | awk 'print $9' | xargs rm.
I know the individual meaning of these commands but what happens when we join them by pipe?
linux shell unix pipeline
What is the meaning of this command: ls -lt | grep - | head -1 | awk 'print $9' | xargs rm.
I know the individual meaning of these commands but what happens when we join them by pipe?
linux shell unix pipeline
linux shell unix pipeline
edited Mar 6 at 23:37
John Kugelman
246k54406459
246k54406459
asked Mar 6 at 23:17
devashish-pateldevashish-patel
287
287
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21
add a comment |
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21
add a comment |
1 Answer
1
active
oldest
votes
A simple way to see what long pipelines do is run them one piece at a time. Run ls -lt, then ls -lt | grep -, then ls -lt | grep - | head -1, etc. See what the intermediate output of each command is so you know what's being fed into the next.
$ cd /usr
$ ls -lt
total 156
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
File listing with each entry on its own line along with permissions, size, and other information. Looking at man ls I see that the -t flag means the files are sorted by modification time, newest to oldest.
$ ls -lt | grep -
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
Appears to remove the "total 156" line. (If that's the goal it's a rather poor way to do it. There's no guarantee that every line has a dash in it, which is what grep - is looking for. A file with full rwxrwxrwx permissions wouldn't have a dash in the line.)
$ ls -lt | grep - | head -1
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
Gets the first line of the previous grep results.
$ ls -lt | grep - | head -1 | awk 'print $9'
src/
Prints the ninth column, the file name.
Careful: At this point you could tack on | xargs rm, but that rm looks dangerous. Let's not, eh? Instead of running it to see what happens, I'd try man xargs to see what xargs rm does. Or google "xargs rm": aha, that tells me that it'll delete the files being passed in. Good to know—I think I'll pass.
Put all the pieces together and you might describe the overall result as "list files in reverse chronological order, find the first, and delete it". Or in other words, delete the newest file. That's what the whole thing does.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033709%2fwhat-is-the-meaning-of-ls-lt-grep-head-1-awk-print-9-xargs-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A simple way to see what long pipelines do is run them one piece at a time. Run ls -lt, then ls -lt | grep -, then ls -lt | grep - | head -1, etc. See what the intermediate output of each command is so you know what's being fed into the next.
$ cd /usr
$ ls -lt
total 156
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
File listing with each entry on its own line along with permissions, size, and other information. Looking at man ls I see that the -t flag means the files are sorted by modification time, newest to oldest.
$ ls -lt | grep -
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
Appears to remove the "total 156" line. (If that's the goal it's a rather poor way to do it. There's no guarantee that every line has a dash in it, which is what grep - is looking for. A file with full rwxrwxrwx permissions wouldn't have a dash in the line.)
$ ls -lt | grep - | head -1
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
Gets the first line of the previous grep results.
$ ls -lt | grep - | head -1 | awk 'print $9'
src/
Prints the ninth column, the file name.
Careful: At this point you could tack on | xargs rm, but that rm looks dangerous. Let's not, eh? Instead of running it to see what happens, I'd try man xargs to see what xargs rm does. Or google "xargs rm": aha, that tells me that it'll delete the files being passed in. Good to know—I think I'll pass.
Put all the pieces together and you might describe the overall result as "list files in reverse chronological order, find the first, and delete it". Or in other words, delete the newest file. That's what the whole thing does.
add a comment |
A simple way to see what long pipelines do is run them one piece at a time. Run ls -lt, then ls -lt | grep -, then ls -lt | grep - | head -1, etc. See what the intermediate output of each command is so you know what's being fed into the next.
$ cd /usr
$ ls -lt
total 156
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
File listing with each entry on its own line along with permissions, size, and other information. Looking at man ls I see that the -t flag means the files are sorted by modification time, newest to oldest.
$ ls -lt | grep -
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
Appears to remove the "total 156" line. (If that's the goal it's a rather poor way to do it. There's no guarantee that every line has a dash in it, which is what grep - is looking for. A file with full rwxrwxrwx permissions wouldn't have a dash in the line.)
$ ls -lt | grep - | head -1
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
Gets the first line of the previous grep results.
$ ls -lt | grep - | head -1 | awk 'print $9'
src/
Prints the ninth column, the file name.
Careful: At this point you could tack on | xargs rm, but that rm looks dangerous. Let's not, eh? Instead of running it to see what happens, I'd try man xargs to see what xargs rm does. Or google "xargs rm": aha, that tells me that it'll delete the files being passed in. Good to know—I think I'll pass.
Put all the pieces together and you might describe the overall result as "list files in reverse chronological order, find the first, and delete it". Or in other words, delete the newest file. That's what the whole thing does.
add a comment |
A simple way to see what long pipelines do is run them one piece at a time. Run ls -lt, then ls -lt | grep -, then ls -lt | grep - | head -1, etc. See what the intermediate output of each command is so you know what's being fed into the next.
$ cd /usr
$ ls -lt
total 156
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
File listing with each entry on its own line along with permissions, size, and other information. Looking at man ls I see that the -t flag means the files are sorted by modification time, newest to oldest.
$ ls -lt | grep -
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
Appears to remove the "total 156" line. (If that's the goal it's a rather poor way to do it. There's no guarantee that every line has a dash in it, which is what grep - is looking for. A file with full rwxrwxrwx permissions wouldn't have a dash in the line.)
$ ls -lt | grep - | head -1
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
Gets the first line of the previous grep results.
$ ls -lt | grep - | head -1 | awk 'print $9'
src/
Prints the ninth column, the file name.
Careful: At this point you could tack on | xargs rm, but that rm looks dangerous. Let's not, eh? Instead of running it to see what happens, I'd try man xargs to see what xargs rm does. Or google "xargs rm": aha, that tells me that it'll delete the files being passed in. Good to know—I think I'll pass.
Put all the pieces together and you might describe the overall result as "list files in reverse chronological order, find the first, and delete it". Or in other words, delete the newest file. That's what the whole thing does.
A simple way to see what long pipelines do is run them one piece at a time. Run ls -lt, then ls -lt | grep -, then ls -lt | grep - | head -1, etc. See what the intermediate output of each command is so you know what's being fed into the next.
$ cd /usr
$ ls -lt
total 156
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
File listing with each entry on its own line along with permissions, size, and other information. Looking at man ls I see that the -t flag means the files are sorted by modification time, newest to oldest.
$ ls -lt | grep -
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
drwxr-xr-x 2 root root 90112 Mar 5 06:27 bin/
drwxr-xr-x 2 root root 12288 Mar 5 06:27 sbin/
drwxr-xr-x 155 root root 4096 Mar 5 06:27 lib/
drwxr-xr-x 50 root root 20480 Feb 20 18:11 include/
drwxr-xr-x 330 root root 12288 Feb 18 16:58 share/
drwxr-xr-x 2 root root 4096 Oct 19 13:51 games/
drwxr-xr-x 3 root root 4096 Jul 19 2016 locale/
drwxr-xr-x 10 root root 4096 Jul 19 2016 local/
Appears to remove the "total 156" line. (If that's the goal it's a rather poor way to do it. There's no guarantee that every line has a dash in it, which is what grep - is looking for. A file with full rwxrwxrwx permissions wouldn't have a dash in the line.)
$ ls -lt | grep - | head -1
drwxr-xr-x 11 root root 4096 Mar 6 06:35 src/
Gets the first line of the previous grep results.
$ ls -lt | grep - | head -1 | awk 'print $9'
src/
Prints the ninth column, the file name.
Careful: At this point you could tack on | xargs rm, but that rm looks dangerous. Let's not, eh? Instead of running it to see what happens, I'd try man xargs to see what xargs rm does. Or google "xargs rm": aha, that tells me that it'll delete the files being passed in. Good to know—I think I'll pass.
Put all the pieces together and you might describe the overall result as "list files in reverse chronological order, find the first, and delete it". Or in other words, delete the newest file. That's what the whole thing does.
answered Mar 6 at 23:31
John KugelmanJohn Kugelman
246k54406459
246k54406459
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033709%2fwhat-is-the-meaning-of-ls-lt-grep-head-1-awk-print-9-xargs-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Duplicate - stackoverflow.com/questions/9834086/…
– battlmonstr
Mar 6 at 23:21