Zipping the 10 most recent files

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.
linux files date zip
add a comment |
Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.
linux files date zip
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17
add a comment |
Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.
linux files date zip
Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.
linux files date zip
linux files date zip
edited Mar 14 at 15:25
Jeff Schaller♦
45k1164147
45k1164147
asked Mar 14 at 8:02
Prabhat JaiswalPrabhat Jaiswal
31
31
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17
add a comment |
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17
add a comment |
3 Answers
3
active
oldest
votes
First take backup
You should try:
ls -Lt | head -10 | xargs zip log.zip
Your command
zip log.zip *.log
is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.
If your directory has mixed files, i.e. files other than log files, then you can try:
ls -Lt *.log | head -10 | xargs zip log.zip
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like/home/username/...../log.zip.
– Prvt_Yadv
Mar 14 at 10:04
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
add a comment |
To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:
zsh -c 'zip log.zip *.log(.om[1,10])'
This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:
*.log( ... )-- this starts the wildcard off with*.log, which will select every1 file in the current directory that ends with.log, filtered by the following criteria.-- this filters the resulting list to include only plain filesom-- this sorts ("orders") the resulting list by modification time, most recent first[1,10]-- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)
Once zsh has generated the 10 most recent plain files, it hands those to the zip command.
Footnotes:
- by default, zsh will not select hidden (dot) files, such as
.foo.log; if you have such files and wish to select them, you can include theDglob qualifier (*.log(.Dom[1,10])) or set theGLOB_DOTSoption (withsetopt globdots).
add a comment |
Tried with below command
ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f506230%2fzipping-the-10-most-recent-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
First take backup
You should try:
ls -Lt | head -10 | xargs zip log.zip
Your command
zip log.zip *.log
is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.
If your directory has mixed files, i.e. files other than log files, then you can try:
ls -Lt *.log | head -10 | xargs zip log.zip
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like/home/username/...../log.zip.
– Prvt_Yadv
Mar 14 at 10:04
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
add a comment |
First take backup
You should try:
ls -Lt | head -10 | xargs zip log.zip
Your command
zip log.zip *.log
is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.
If your directory has mixed files, i.e. files other than log files, then you can try:
ls -Lt *.log | head -10 | xargs zip log.zip
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like/home/username/...../log.zip.
– Prvt_Yadv
Mar 14 at 10:04
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
add a comment |
First take backup
You should try:
ls -Lt | head -10 | xargs zip log.zip
Your command
zip log.zip *.log
is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.
If your directory has mixed files, i.e. files other than log files, then you can try:
ls -Lt *.log | head -10 | xargs zip log.zip
First take backup
You should try:
ls -Lt | head -10 | xargs zip log.zip
Your command
zip log.zip *.log
is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.
If your directory has mixed files, i.e. files other than log files, then you can try:
ls -Lt *.log | head -10 | xargs zip log.zip
answered Mar 14 at 8:28
Prvt_YadvPrvt_Yadv
3,30631430
3,30631430
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like/home/username/...../log.zip.
– Prvt_Yadv
Mar 14 at 10:04
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
add a comment |
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like/home/username/...../log.zip.
– Prvt_Yadv
Mar 14 at 10:04
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]
– Prabhat Jaiswal
Mar 14 at 9:51
Instead of log.zip use the absolute path like
/home/username/...../log.zip.– Prvt_Yadv
Mar 14 at 10:04
Instead of log.zip use the absolute path like
/home/username/...../log.zip.– Prvt_Yadv
Mar 14 at 10:04
1
1
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.
– Jeff Schaller♦
Mar 14 at 19:13
add a comment |
To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:
zsh -c 'zip log.zip *.log(.om[1,10])'
This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:
*.log( ... )-- this starts the wildcard off with*.log, which will select every1 file in the current directory that ends with.log, filtered by the following criteria.-- this filters the resulting list to include only plain filesom-- this sorts ("orders") the resulting list by modification time, most recent first[1,10]-- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)
Once zsh has generated the 10 most recent plain files, it hands those to the zip command.
Footnotes:
- by default, zsh will not select hidden (dot) files, such as
.foo.log; if you have such files and wish to select them, you can include theDglob qualifier (*.log(.Dom[1,10])) or set theGLOB_DOTSoption (withsetopt globdots).
add a comment |
To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:
zsh -c 'zip log.zip *.log(.om[1,10])'
This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:
*.log( ... )-- this starts the wildcard off with*.log, which will select every1 file in the current directory that ends with.log, filtered by the following criteria.-- this filters the resulting list to include only plain filesom-- this sorts ("orders") the resulting list by modification time, most recent first[1,10]-- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)
Once zsh has generated the 10 most recent plain files, it hands those to the zip command.
Footnotes:
- by default, zsh will not select hidden (dot) files, such as
.foo.log; if you have such files and wish to select them, you can include theDglob qualifier (*.log(.Dom[1,10])) or set theGLOB_DOTSoption (withsetopt globdots).
add a comment |
To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:
zsh -c 'zip log.zip *.log(.om[1,10])'
This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:
*.log( ... )-- this starts the wildcard off with*.log, which will select every1 file in the current directory that ends with.log, filtered by the following criteria.-- this filters the resulting list to include only plain filesom-- this sorts ("orders") the resulting list by modification time, most recent first[1,10]-- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)
Once zsh has generated the 10 most recent plain files, it hands those to the zip command.
Footnotes:
- by default, zsh will not select hidden (dot) files, such as
.foo.log; if you have such files and wish to select them, you can include theDglob qualifier (*.log(.Dom[1,10])) or set theGLOB_DOTSoption (withsetopt globdots).
To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:
zsh -c 'zip log.zip *.log(.om[1,10])'
This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:
*.log( ... )-- this starts the wildcard off with*.log, which will select every1 file in the current directory that ends with.log, filtered by the following criteria.-- this filters the resulting list to include only plain filesom-- this sorts ("orders") the resulting list by modification time, most recent first[1,10]-- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)
Once zsh has generated the 10 most recent plain files, it hands those to the zip command.
Footnotes:
- by default, zsh will not select hidden (dot) files, such as
.foo.log; if you have such files and wish to select them, you can include theDglob qualifier (*.log(.Dom[1,10])) or set theGLOB_DOTSoption (withsetopt globdots).
answered Mar 14 at 12:54
Jeff Schaller♦Jeff Schaller
45k1164147
45k1164147
add a comment |
add a comment |
Tried with below command
ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
add a comment |
Tried with below command
ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
add a comment |
Tried with below command
ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh
Tried with below command
ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh
answered Mar 14 at 18:36
Praveen Kumar BSPraveen Kumar BS
1,7621311
1,7621311
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
add a comment |
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?
– Jeff Schaller♦
Mar 14 at 19:05
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f506230%2fzipping-the-10-most-recent-files%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
Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.
– gerhard d.
Mar 14 at 8:17