how to write a script for running a stat command for a directories and sub directories and print only most recent file
Clash 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
bash timestamps
add a comment |Â
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
bash timestamps
I think your secondfor
loop is supposed to be nested in the first? the variableentry
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 shouldD
be?
â Jesse_b
Feb 26 at 18:26
I think yourls
may be broken if that is what the output ofls -ltr
looks like on your system.
â Jesse_b
Feb 26 at 18:47
add a comment |Â
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
bash timestamps
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
bash timestamps
edited Feb 26 at 18:46
Jesse_b
10.4k22658
10.4k22658
asked Feb 26 at 18:20
user2576747
112
112
I think your secondfor
loop is supposed to be nested in the first? the variableentry
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 shouldD
be?
â Jesse_b
Feb 26 at 18:26
I think yourls
may be broken if that is what the output ofls -ltr
looks like on your system.
â Jesse_b
Feb 26 at 18:47
add a comment |Â
I think your secondfor
loop is supposed to be nested in the first? the variableentry
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 shouldD
be?
â Jesse_b
Feb 26 at 18:26
I think yourls
may be broken if that is what the output ofls -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
add a comment |Â
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).
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 GNUstat
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
 |Â
show 3 more comments
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).
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 GNUstat
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
 |Â
show 3 more comments
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).
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 GNUstat
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
 |Â
show 3 more comments
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).
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).
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 GNUstat
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
 |Â
show 3 more comments
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 GNUstat
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
 |Â
show 3 more comments
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
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
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
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
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
I think your second
for
loop is supposed to be nested in the first? the variableentry
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 shouldD
be?â Jesse_b
Feb 26 at 18:26
I think your
ls
may be broken if that is what the output ofls -ltr
looks like on your system.â Jesse_b
Feb 26 at 18:47