how to write a script for running a stat command for a directories and sub directories and print only most recent file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












how to find most recent files in a directory? my script is giving some extra outputs with most recent file in the directory.



#!/bin/bash
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
(stat -c %y "$directory"/* | tail -n 1)
done
for D in "$entry"
do
(ls -ltr "$D" | tail -n 1)
done


current output :



2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
-rw-r--r-- 1 root root 0 Feb 19 12:19 test3.xml


my directory structure is as below



$ pwd
/nfs/test_library/myfolder/test

$ ls -ltr test
1.0 2.0 3.0

$ ls -ltr 1.0
test1.xml
$ ls -ltr 2.0
test2.xml
$ ls -ltr 3.0
test3.xml(which is the most recent file)


so I have to get the script working for printing only test3.xml







share|improve this question






















  • I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
    – Jesse_b
    Feb 26 at 18:26











  • I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
    – Jesse_b
    Feb 26 at 18:47














up vote
1
down vote

favorite












how to find most recent files in a directory? my script is giving some extra outputs with most recent file in the directory.



#!/bin/bash
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
(stat -c %y "$directory"/* | tail -n 1)
done
for D in "$entry"
do
(ls -ltr "$D" | tail -n 1)
done


current output :



2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
-rw-r--r-- 1 root root 0 Feb 19 12:19 test3.xml


my directory structure is as below



$ pwd
/nfs/test_library/myfolder/test

$ ls -ltr test
1.0 2.0 3.0

$ ls -ltr 1.0
test1.xml
$ ls -ltr 2.0
test2.xml
$ ls -ltr 3.0
test3.xml(which is the most recent file)


so I have to get the script working for printing only test3.xml







share|improve this question






















  • I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
    – Jesse_b
    Feb 26 at 18:26











  • I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
    – Jesse_b
    Feb 26 at 18:47












up vote
1
down vote

favorite









up vote
1
down vote

favorite











how to find most recent files in a directory? my script is giving some extra outputs with most recent file in the directory.



#!/bin/bash
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
(stat -c %y "$directory"/* | tail -n 1)
done
for D in "$entry"
do
(ls -ltr "$D" | tail -n 1)
done


current output :



2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
-rw-r--r-- 1 root root 0 Feb 19 12:19 test3.xml


my directory structure is as below



$ pwd
/nfs/test_library/myfolder/test

$ ls -ltr test
1.0 2.0 3.0

$ ls -ltr 1.0
test1.xml
$ ls -ltr 2.0
test2.xml
$ ls -ltr 3.0
test3.xml(which is the most recent file)


so I have to get the script working for printing only test3.xml







share|improve this question














how to find most recent files in a directory? my script is giving some extra outputs with most recent file in the directory.



#!/bin/bash
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
(stat -c %y "$directory"/* | tail -n 1)
done
for D in "$entry"
do
(ls -ltr "$D" | tail -n 1)
done


current output :



2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
-rw-r--r-- 1 root root 0 Feb 19 12:19 test3.xml


my directory structure is as below



$ pwd
/nfs/test_library/myfolder/test

$ ls -ltr test
1.0 2.0 3.0

$ ls -ltr 1.0
test1.xml
$ ls -ltr 2.0
test2.xml
$ ls -ltr 3.0
test3.xml(which is the most recent file)


so I have to get the script working for printing only test3.xml









share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 18:46









Jesse_b

10.4k22658




10.4k22658










asked Feb 26 at 18:20









user2576747

112




112











  • I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
    – Jesse_b
    Feb 26 at 18:26











  • I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
    – Jesse_b
    Feb 26 at 18:47
















  • I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
    – Jesse_b
    Feb 26 at 18:26











  • I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
    – Jesse_b
    Feb 26 at 18:47















I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
– Jesse_b
Feb 26 at 18:26





I think your second for loop is supposed to be nested in the first? the variable entry doesn't exist outside of it. However either way it wouldn't make sense because entry is going to be a single file so what should D be?
– Jesse_b
Feb 26 at 18:26













I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
– Jesse_b
Feb 26 at 18:47




I think your ls may be broken if that is what the output of ls -ltr looks like on your system.
– Jesse_b
Feb 26 at 18:47










1 Answer
1






active

oldest

votes

















up vote
1
down vote













I was able to accomplish what I believe you want with the following:



GNU stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat --printf='%Yt%nn' ; | sort -n -k1,1 | tail -1
else
echo "Error, please only specify a directory"
fi


BSD stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat -F -t '%s' ; | sort -n -k6,6 | tail -1
else
echo "Error, please only specify a directory"
fi


This will find all files in the specified directory recursively. Then it will stat them with their modify timestamp in a UNIX EPOCH timestamp format. Then it sorts them based on this timestamp field. Finally it prints only the last result (most recently updated file).






share|improve this answer






















  • please let me know how I can use this in the script
    – user2576747
    Feb 26 at 18:38










  • @user2576747: I have updated my answer to prompt for the directory to be searched.
    – Jesse_b
    Feb 26 at 18:40










  • getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
    – user2576747
    Feb 26 at 19:20










  • @user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
    – Jesse_b
    Feb 26 at 19:33










  • yay..it worked thank you so much .
    – user2576747
    Feb 26 at 19:42










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426764%2fhow-to-write-a-script-for-running-a-stat-command-for-a-directories-and-sub-direc%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













I was able to accomplish what I believe you want with the following:



GNU stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat --printf='%Yt%nn' ; | sort -n -k1,1 | tail -1
else
echo "Error, please only specify a directory"
fi


BSD stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat -F -t '%s' ; | sort -n -k6,6 | tail -1
else
echo "Error, please only specify a directory"
fi


This will find all files in the specified directory recursively. Then it will stat them with their modify timestamp in a UNIX EPOCH timestamp format. Then it sorts them based on this timestamp field. Finally it prints only the last result (most recently updated file).






share|improve this answer






















  • please let me know how I can use this in the script
    – user2576747
    Feb 26 at 18:38










  • @user2576747: I have updated my answer to prompt for the directory to be searched.
    – Jesse_b
    Feb 26 at 18:40










  • getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
    – user2576747
    Feb 26 at 19:20










  • @user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
    – Jesse_b
    Feb 26 at 19:33










  • yay..it worked thank you so much .
    – user2576747
    Feb 26 at 19:42














up vote
1
down vote













I was able to accomplish what I believe you want with the following:



GNU stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat --printf='%Yt%nn' ; | sort -n -k1,1 | tail -1
else
echo "Error, please only specify a directory"
fi


BSD stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat -F -t '%s' ; | sort -n -k6,6 | tail -1
else
echo "Error, please only specify a directory"
fi


This will find all files in the specified directory recursively. Then it will stat them with their modify timestamp in a UNIX EPOCH timestamp format. Then it sorts them based on this timestamp field. Finally it prints only the last result (most recently updated file).






share|improve this answer






















  • please let me know how I can use this in the script
    – user2576747
    Feb 26 at 18:38










  • @user2576747: I have updated my answer to prompt for the directory to be searched.
    – Jesse_b
    Feb 26 at 18:40










  • getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
    – user2576747
    Feb 26 at 19:20










  • @user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
    – Jesse_b
    Feb 26 at 19:33










  • yay..it worked thank you so much .
    – user2576747
    Feb 26 at 19:42












up vote
1
down vote










up vote
1
down vote









I was able to accomplish what I believe you want with the following:



GNU stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat --printf='%Yt%nn' ; | sort -n -k1,1 | tail -1
else
echo "Error, please only specify a directory"
fi


BSD stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat -F -t '%s' ; | sort -n -k6,6 | tail -1
else
echo "Error, please only specify a directory"
fi


This will find all files in the specified directory recursively. Then it will stat them with their modify timestamp in a UNIX EPOCH timestamp format. Then it sorts them based on this timestamp field. Finally it prints only the last result (most recently updated file).






share|improve this answer














I was able to accomplish what I believe you want with the following:



GNU stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat --printf='%Yt%nn' ; | sort -n -k1,1 | tail -1
else
echo "Error, please only specify a directory"
fi


BSD stat



read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
find "$directory" -type f -exec stat -F -t '%s' ; | sort -n -k6,6 | tail -1
else
echo "Error, please only specify a directory"
fi


This will find all files in the specified directory recursively. Then it will stat them with their modify timestamp in a UNIX EPOCH timestamp format. Then it sorts them based on this timestamp field. Finally it prints only the last result (most recently updated file).







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 26 at 19:40

























answered Feb 26 at 18:36









Jesse_b

10.4k22658




10.4k22658











  • please let me know how I can use this in the script
    – user2576747
    Feb 26 at 18:38










  • @user2576747: I have updated my answer to prompt for the directory to be searched.
    – Jesse_b
    Feb 26 at 18:40










  • getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
    – user2576747
    Feb 26 at 19:20










  • @user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
    – Jesse_b
    Feb 26 at 19:33










  • yay..it worked thank you so much .
    – user2576747
    Feb 26 at 19:42
















  • please let me know how I can use this in the script
    – user2576747
    Feb 26 at 18:38










  • @user2576747: I have updated my answer to prompt for the directory to be searched.
    – Jesse_b
    Feb 26 at 18:40










  • getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
    – user2576747
    Feb 26 at 19:20










  • @user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
    – Jesse_b
    Feb 26 at 19:33










  • yay..it worked thank you so much .
    – user2576747
    Feb 26 at 19:42















please let me know how I can use this in the script
– user2576747
Feb 26 at 18:38




please let me know how I can use this in the script
– user2576747
Feb 26 at 18:38












@user2576747: I have updated my answer to prompt for the directory to be searched.
– Jesse_b
Feb 26 at 18:40




@user2576747: I have updated my answer to prompt for the directory to be searched.
– Jesse_b
Feb 26 at 18:40












getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
– user2576747
Feb 26 at 19:20




getting below errorstat: invalid option -- 'F' Try 'stat --help' for more information
– user2576747
Feb 26 at 19:20












@user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
– Jesse_b
Feb 26 at 19:33




@user2576747: I have modified my answer to include a solution using GNU stat which you are likely using.
– Jesse_b
Feb 26 at 19:33












yay..it worked thank you so much .
– user2576747
Feb 26 at 19:42




yay..it worked thank you so much .
– user2576747
Feb 26 at 19:42












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426764%2fhow-to-write-a-script-for-running-a-stat-command-for-a-directories-and-sub-direc%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay