How can I list (ls) the 5 last modified files in a directory?
I know ls -t
will list all files by modified time. But how can I limit these results to only the last n files?
linux list terminal limit ls
add a comment |
I know ls -t
will list all files by modified time. But how can I limit these results to only the last n files?
linux list terminal limit ls
7
Reverse the orderls -lrt
might help somebody.
– PJ Brunet
Jan 12 '15 at 14:51
Love it @PJBrunet. I've formed the habit of always ls'ing withtharl
as it tends to have everything I need.
– Adam Grant
Feb 15 '18 at 16:57
add a comment |
I know ls -t
will list all files by modified time. But how can I limit these results to only the last n files?
linux list terminal limit ls
I know ls -t
will list all files by modified time. But how can I limit these results to only the last n files?
linux list terminal limit ls
linux list terminal limit ls
asked Mar 28 '13 at 20:17
RyanRyan
8,0632275138
8,0632275138
7
Reverse the orderls -lrt
might help somebody.
– PJ Brunet
Jan 12 '15 at 14:51
Love it @PJBrunet. I've formed the habit of always ls'ing withtharl
as it tends to have everything I need.
– Adam Grant
Feb 15 '18 at 16:57
add a comment |
7
Reverse the orderls -lrt
might help somebody.
– PJ Brunet
Jan 12 '15 at 14:51
Love it @PJBrunet. I've formed the habit of always ls'ing withtharl
as it tends to have everything I need.
– Adam Grant
Feb 15 '18 at 16:57
7
7
Reverse the order
ls -lrt
might help somebody.– PJ Brunet
Jan 12 '15 at 14:51
Reverse the order
ls -lrt
might help somebody.– PJ Brunet
Jan 12 '15 at 14:51
Love it @PJBrunet. I've formed the habit of always ls'ing with
tharl
as it tends to have everything I need.– Adam Grant
Feb 15 '18 at 16:57
Love it @PJBrunet. I've formed the habit of always ls'ing with
tharl
as it tends to have everything I need.– Adam Grant
Feb 15 '18 at 16:57
add a comment |
4 Answers
4
active
oldest
votes
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
2
If I'm not mistaken, the shell represents the output ofls
in multiple columns, but the output ofls
is piped to following command with 1 file/dir at a time
– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
This ignores the Total at the top:ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
add a comment |
Use tail
command:
ls -t | tail -n 5
add a comment |
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l
outputs in a list format
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t
sorts output by placing most recently modified file first
head -6
will show 5 files because ls
prints the block size in the first line of output.
I think this is a slightly more elegant and possibly more useful approach.
Example output:
total 26960312
-rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py
-rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf
-rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf
-rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf
-rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
add a comment |
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
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%2f15691359%2fhow-can-i-list-ls-the-5-last-modified-files-in-a-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
2
If I'm not mistaken, the shell represents the output ofls
in multiple columns, but the output ofls
is piped to following command with 1 file/dir at a time
– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
This ignores the Total at the top:ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
add a comment |
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
2
If I'm not mistaken, the shell represents the output ofls
in multiple columns, but the output ofls
is piped to following command with 1 file/dir at a time
– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
This ignores the Total at the top:ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
add a comment |
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
answered Mar 28 '13 at 20:19
Paul RubelPaul Rubel
21.9k74673
21.9k74673
2
If I'm not mistaken, the shell represents the output ofls
in multiple columns, but the output ofls
is piped to following command with 1 file/dir at a time
– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
This ignores the Total at the top:ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
add a comment |
2
If I'm not mistaken, the shell represents the output ofls
in multiple columns, but the output ofls
is piped to following command with 1 file/dir at a time
– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
This ignores the Total at the top:ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
2
2
If I'm not mistaken, the shell represents the output of
ls
in multiple columns, but the output of ls
is piped to following command with 1 file/dir at a time– Alex
Mar 28 '13 at 20:22
If I'm not mistaken, the shell represents the output of
ls
in multiple columns, but the output of ls
is piped to following command with 1 file/dir at a time– Alex
Mar 28 '13 at 20:22
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
that does seem to be the case. Nice simplification.
– Paul Rubel
Mar 28 '13 at 20:34
3
3
This ignores the Total at the top:
ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
This ignores the Total at the top:
ls -1t | head -n 6 | tail -n 5
– Ryan
Mar 28 '13 at 21:50
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
I don't see a total, but if there is one that would certainly work.
– Paul Rubel
Mar 29 '13 at 13:31
add a comment |
Use tail
command:
ls -t | tail -n 5
add a comment |
Use tail
command:
ls -t | tail -n 5
add a comment |
Use tail
command:
ls -t | tail -n 5
Use tail
command:
ls -t | tail -n 5
answered Mar 28 '13 at 20:19
AlexAlex
6,62363152
6,62363152
add a comment |
add a comment |
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l
outputs in a list format
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t
sorts output by placing most recently modified file first
head -6
will show 5 files because ls
prints the block size in the first line of output.
I think this is a slightly more elegant and possibly more useful approach.
Example output:
total 26960312
-rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py
-rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf
-rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf
-rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf
-rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
add a comment |
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l
outputs in a list format
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t
sorts output by placing most recently modified file first
head -6
will show 5 files because ls
prints the block size in the first line of output.
I think this is a slightly more elegant and possibly more useful approach.
Example output:
total 26960312
-rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py
-rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf
-rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf
-rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf
-rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
add a comment |
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l
outputs in a list format
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t
sorts output by placing most recently modified file first
head -6
will show 5 files because ls
prints the block size in the first line of output.
I think this is a slightly more elegant and possibly more useful approach.
Example output:
total 26960312
-rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py
-rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf
-rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf
-rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf
-rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
The accepted answer lists only the filenames, but to get the top 5 files one can also use:
ls -lht | head -6
where:
-l
outputs in a list format
-h
makes output human readable (i.e. file sizes appear in kb, mb, etc.)
-t
sorts output by placing most recently modified file first
head -6
will show 5 files because ls
prints the block size in the first line of output.
I think this is a slightly more elegant and possibly more useful approach.
Example output:
total 26960312
-rw-r--r--@ 1 user staff 1.2K 11 Jan 11:22 phone2.7.py
-rw-r--r--@ 1 user staff 2.7M 10 Jan 15:26 03-cookies-1.pdf
-rw-r--r--@ 1 user staff 9.2M 9 Jan 16:21 Wk1_sem.pdf
-rw-r--r--@ 1 user staff 502K 8 Jan 10:20 lab-01.pdf
-rw-rw-rw-@ 1 user staff 2.0M 5 Jan 22:06 0410-1.wmv
answered Jan 11 '18 at 11:37
samisnotinsanesamisnotinsane
14519
14519
add a comment |
add a comment |
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
add a comment |
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
add a comment |
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
ls -t
list files by creation time not last modified time. Use ls -ltc
if you want to list files by last modified time from last to first(top to bottom). Thus to list the last n: ls -ltc | head ${n}
answered Aug 25 '16 at 23:13
victrrvictrr
292
292
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
add a comment |
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
1
1
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:
-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
Which platform/version do you refer to? Can you provide a link? Looking at the linux man-page yields the opposite of what you state:
-t sort by modification time, newest first
– Joma
Sep 8 '16 at 21:35
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%2f15691359%2fhow-can-i-list-ls-the-5-last-modified-files-in-a-directory%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
7
Reverse the order
ls -lrt
might help somebody.– PJ Brunet
Jan 12 '15 at 14:51
Love it @PJBrunet. I've formed the habit of always ls'ing with
tharl
as it tends to have everything I need.– Adam Grant
Feb 15 '18 at 16:57