How to change date format and position in find output
Say i have the following command. It's set to display a list of the last 20 files modified on my drive with the paths included.
find -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -r | head -20
The command works fine however in the format of the date and time there is too much information as it also displays the milliseconds. My questions were how would I remove this. In addition, how could I change the position it prints in so that the path would show up on the left side and the date on the right?
scripting find
add a comment |
Say i have the following command. It's set to display a list of the last 20 files modified on my drive with the paths included.
find -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -r | head -20
The command works fine however in the format of the date and time there is too much information as it also displays the milliseconds. My questions were how would I remove this. In addition, how could I change the position it prints in so that the path would show up on the left side and the date on the right?
scripting find
add a comment |
Say i have the following command. It's set to display a list of the last 20 files modified on my drive with the paths included.
find -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -r | head -20
The command works fine however in the format of the date and time there is too much information as it also displays the milliseconds. My questions were how would I remove this. In addition, how could I change the position it prints in so that the path would show up on the left side and the date on the right?
scripting find
Say i have the following command. It's set to display a list of the last 20 files modified on my drive with the paths included.
find -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -r | head -20
The command works fine however in the format of the date and time there is too much information as it also displays the milliseconds. My questions were how would I remove this. In addition, how could I change the position it prints in so that the path would show up on the left side and the date on the right?
scripting find
scripting find
edited Dec 16 at 4:00
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Dec 21 '14 at 1:46
flowoftruth
173
173
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could do this:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/2 1/' | head -20
Putting .8
between %
and TT
modifies that field to limit it to 8 characters (hh:mm:ss
). The sed
regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's necessary for the time to be at the beginning of the line for the sort
command.
Warning: Putting the date after the file name could potentially cause confusion if filenames are displayed that contain spaces and numbers. However, for viewing as a human rather that for feeding into another script or program, this output should be fine.
Otherwise, to leave the times in front of the filenames:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | head -20
EDIT: sed
is unnecessary for trimming fractional second per @steeldriver's comment. This may be a GNU extension, but -printf
doesn't appear to be POSIX anyway. Also, corrected sorting bug.
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
At least with the GNU version offind
, you can specify a precision for the time field e.g. to print exactly 8 charactershh:mm:ss
(omitting the fractional seconds), replace%TT
by%.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
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%2f175214%2fhow-to-change-date-format-and-position-in-find-output%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
You could do this:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/2 1/' | head -20
Putting .8
between %
and TT
modifies that field to limit it to 8 characters (hh:mm:ss
). The sed
regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's necessary for the time to be at the beginning of the line for the sort
command.
Warning: Putting the date after the file name could potentially cause confusion if filenames are displayed that contain spaces and numbers. However, for viewing as a human rather that for feeding into another script or program, this output should be fine.
Otherwise, to leave the times in front of the filenames:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | head -20
EDIT: sed
is unnecessary for trimming fractional second per @steeldriver's comment. This may be a GNU extension, but -printf
doesn't appear to be POSIX anyway. Also, corrected sorting bug.
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
At least with the GNU version offind
, you can specify a precision for the time field e.g. to print exactly 8 charactershh:mm:ss
(omitting the fractional seconds), replace%TT
by%.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
add a comment |
You could do this:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/2 1/' | head -20
Putting .8
between %
and TT
modifies that field to limit it to 8 characters (hh:mm:ss
). The sed
regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's necessary for the time to be at the beginning of the line for the sort
command.
Warning: Putting the date after the file name could potentially cause confusion if filenames are displayed that contain spaces and numbers. However, for viewing as a human rather that for feeding into another script or program, this output should be fine.
Otherwise, to leave the times in front of the filenames:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | head -20
EDIT: sed
is unnecessary for trimming fractional second per @steeldriver's comment. This may be a GNU extension, but -printf
doesn't appear to be POSIX anyway. Also, corrected sorting bug.
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
At least with the GNU version offind
, you can specify a precision for the time field e.g. to print exactly 8 charactershh:mm:ss
(omitting the fractional seconds), replace%TT
by%.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
add a comment |
You could do this:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/2 1/' | head -20
Putting .8
between %
and TT
modifies that field to limit it to 8 characters (hh:mm:ss
). The sed
regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's necessary for the time to be at the beginning of the line for the sort
command.
Warning: Putting the date after the file name could potentially cause confusion if filenames are displayed that contain spaces and numbers. However, for viewing as a human rather that for feeding into another script or program, this output should be fine.
Otherwise, to leave the times in front of the filenames:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | head -20
EDIT: sed
is unnecessary for trimming fractional second per @steeldriver's comment. This may be a GNU extension, but -printf
doesn't appear to be POSIX anyway. Also, corrected sorting bug.
You could do this:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | sed -r 's/(^[^ ]+ [^ ]+) (.+)/2 1/' | head -20
Putting .8
between %
and TT
modifies that field to limit it to 8 characters (hh:mm:ss
). The sed
regex moves the first part of the line (to sets of non-space characters with one space between) to the end of the line. It's necessary for the time to be at the beginning of the line for the sort
command.
Warning: Putting the date after the file name could potentially cause confusion if filenames are displayed that contain spaces and numbers. However, for viewing as a human rather that for feeding into another script or program, this output should be fine.
Otherwise, to leave the times in front of the filenames:
find -type f -printf '%TY-%Tm-%Td %.8TT %pn' | sort -r | head -20
EDIT: sed
is unnecessary for trimming fractional second per @steeldriver's comment. This may be a GNU extension, but -printf
doesn't appear to be POSIX anyway. Also, corrected sorting bug.
edited Dec 21 '14 at 3:19
answered Dec 21 '14 at 2:01
depquid
2,77011330
2,77011330
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
At least with the GNU version offind
, you can specify a precision for the time field e.g. to print exactly 8 charactershh:mm:ss
(omitting the fractional seconds), replace%TT
by%.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
add a comment |
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
At least with the GNU version offind
, you can specify a precision for the time field e.g. to print exactly 8 charactershh:mm:ss
(omitting the fractional seconds), replace%TT
by%.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
Hi. Thank you so much for your help. The layout is as i wanted however putting the text before the date seems to cause issues so i think i may leave that part out. However, when i keep the command as in my original while ONLY adding the sed regex it doesn't seem to remove the additional numerals (milliseconds). Why could this be ?
– flowoftruth
Dec 21 '14 at 2:29
2
2
At least with the GNU version of
find
, you can specify a precision for the time field e.g. to print exactly 8 characters hh:mm:ss
(omitting the fractional seconds), replace %TT
by %.8TT
– steeldriver
Dec 21 '14 at 2:44
At least with the GNU version of
find
, you can specify a precision for the time field e.g. to print exactly 8 characters hh:mm:ss
(omitting the fractional seconds), replace %TT
by %.8TT
– steeldriver
Dec 21 '14 at 2:44
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
@flowoftruth Fixed.
– depquid
Dec 21 '14 at 3:20
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f175214%2fhow-to-change-date-format-and-position-in-find-output%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