Compare if the newest file in the directory is different from the previous oneFind latest filesMerge two...

Why were movies shot on film shot at 24 frames per second?

Justifying the use of directed energy weapons

How big would a Daddy Longlegs Spider need to be to kill an average Human?

What to say to a student who has failed?

How would a situation where rescue is impossible be handled by the crew?

Script that helps people make better choices

How is "sein" conjugated in this sub-sentence?

LeetCode: Pascal's Triangle C#

Is there a limit on how long the casting (speaking aloud part of the spell) of Wish can be?

Why is 日本 read as "nihon" but not "nitsuhon"?

Ask for a paid taxi in order to arrive as early as possible for an interview within the city

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner

If all stars rotate, why was there a theory developed, that requires non-rotating stars?

Sleeping solo in a double sleeping bag

Was Switzerland really impossible to invade during WW2?

In the MCU, why does Mjölnir retain its enchantments after Ragnarok?

Can my boyfriend, who lives in the UK and has a Polish passport, visit me in the USA?

Why is observed clock rate < 3MHz on Arduino Uno?

Why does my house heat up, even when it's cool outside?

System to validate run time complexity requirements

How to write triplets in 4/4 time without using a 3 on top of the notes all the time

Vacuum collapse -- why do strong metals implode but glass doesn't?

Factoring the square of this polynomial?



Compare if the newest file in the directory is different from the previous one


Find latest filesMerge two lists while removing duplicatesAutocomplete newest fileCopying the newest filesCompare two numbers read from a fileCopy newest file in directory over SSHDifferent MD5Sum for the same file in same directoryCopying newest files to a preexisting directoryLoad files from different directories one after the other






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have the following script:



#!/bin/bash
y=$(ls -t ./pics/0/*.png | head -n 2 | tail -n 1)
new=$(ls -t ./pics/0/*.png | head -n 1)
while true
do
if cmp --silent "$y" "$new" ; then
y=$(ls -t ./pics/0/*.png | head -n 1)
base64 $y | tr -d 'n' | sed '$ a '
new=$(ls -t ./pics/0/*.png | head -n 1)
fi
done


What am I doing wrong?
To be clear my goal is to compare whether the newest file is different from the previous newest file and only if it is generate a unique BASE64 to STDOUT (meaning it should be printed only ONCE).










share|improve this question









New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

    – guillermo chamorro
    2 days ago











  • @guillermochamorro Now it says that I've got too many arguments..

    – Sir Muffington
    2 days ago











  • @steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

    – Sir Muffington
    2 days ago













  • Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

    – Sir Muffington
    2 days ago






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    2 days ago


















0















I have the following script:



#!/bin/bash
y=$(ls -t ./pics/0/*.png | head -n 2 | tail -n 1)
new=$(ls -t ./pics/0/*.png | head -n 1)
while true
do
if cmp --silent "$y" "$new" ; then
y=$(ls -t ./pics/0/*.png | head -n 1)
base64 $y | tr -d 'n' | sed '$ a '
new=$(ls -t ./pics/0/*.png | head -n 1)
fi
done


What am I doing wrong?
To be clear my goal is to compare whether the newest file is different from the previous newest file and only if it is generate a unique BASE64 to STDOUT (meaning it should be printed only ONCE).










share|improve this question









New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

    – guillermo chamorro
    2 days ago











  • @guillermochamorro Now it says that I've got too many arguments..

    – Sir Muffington
    2 days ago











  • @steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

    – Sir Muffington
    2 days ago













  • Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

    – Sir Muffington
    2 days ago






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    2 days ago














0












0








0


1






I have the following script:



#!/bin/bash
y=$(ls -t ./pics/0/*.png | head -n 2 | tail -n 1)
new=$(ls -t ./pics/0/*.png | head -n 1)
while true
do
if cmp --silent "$y" "$new" ; then
y=$(ls -t ./pics/0/*.png | head -n 1)
base64 $y | tr -d 'n' | sed '$ a '
new=$(ls -t ./pics/0/*.png | head -n 1)
fi
done


What am I doing wrong?
To be clear my goal is to compare whether the newest file is different from the previous newest file and only if it is generate a unique BASE64 to STDOUT (meaning it should be printed only ONCE).










share|improve this question









New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have the following script:



#!/bin/bash
y=$(ls -t ./pics/0/*.png | head -n 2 | tail -n 1)
new=$(ls -t ./pics/0/*.png | head -n 1)
while true
do
if cmp --silent "$y" "$new" ; then
y=$(ls -t ./pics/0/*.png | head -n 1)
base64 $y | tr -d 'n' | sed '$ a '
new=$(ls -t ./pics/0/*.png | head -n 1)
fi
done


What am I doing wrong?
To be clear my goal is to compare whether the newest file is different from the previous newest file and only if it is generate a unique BASE64 to STDOUT (meaning it should be printed only ONCE).







bash






share|improve this question









New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited yesterday







Sir Muffington













New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 2 days ago









Sir MuffingtonSir Muffington

12 bronze badges




12 bronze badges




New contributor



Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Sir Muffington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

    – guillermo chamorro
    2 days ago











  • @guillermochamorro Now it says that I've got too many arguments..

    – Sir Muffington
    2 days ago











  • @steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

    – Sir Muffington
    2 days ago













  • Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

    – Sir Muffington
    2 days ago






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    2 days ago



















  • You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

    – guillermo chamorro
    2 days ago











  • @guillermochamorro Now it says that I've got too many arguments..

    – Sir Muffington
    2 days ago











  • @steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

    – Sir Muffington
    2 days ago













  • Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

    – Sir Muffington
    2 days ago






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    2 days ago

















You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

– guillermo chamorro
2 days ago





You are missing spaces in your if. It is if [ cmp --silent $y $new != null ].

– guillermo chamorro
2 days ago













@guillermochamorro Now it says that I've got too many arguments..

– Sir Muffington
2 days ago





@guillermochamorro Now it says that I've got too many arguments..

– Sir Muffington
2 days ago













@steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

– Sir Muffington
2 days ago







@steeldriver this code works, but the script prints out multiple duplicate base64 strings, I probably made a logical error in doing the script then? any clue how to fix it so that prints unique base64 every time a file is changed?

– Sir Muffington
2 days ago















Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

– Sir Muffington
2 days ago





Removing the --silent flag seems to reduce the number of occurrences of base64 strings to only 3-4 per second, when a new file is added every second. Is this a bug???

– Sir Muffington
2 days ago




1




1





Please take a look: shellcheck.net

– Cyrus
2 days ago





Please take a look: shellcheck.net

– Cyrus
2 days ago










1 Answer
1






active

oldest

votes


















0













Please note that the following snippet will NOT work with filenames containing spaces, tabs or newlines.



To find out more about the find-command used here, please reference https://unix.stackexchange.com/a/240424/364705



#!/bin/bash
lastrun=''
while true; do
read -r newer older <<<
$(find . -type f -exec stat -c '%Y %n' {} ;
| sort -nr
| awk 'NR==1,NR==2 {print $2}'
| xargs )

if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d 'n' | sed '$ a '
lastrun="$newer"
fi
fi
done


And a solution using inotify-wait:



#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done





share|improve this answer




























  • This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

    – Sir Muffington
    yesterday











  • Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

    – markgraf
    yesterday











  • If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

    – markgraf
    yesterday











  • It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

    – Sir Muffington
    yesterday











  • $(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

    – markgraf
    yesterday
















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
});


}
});






Sir Muffington is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536334%2fcompare-if-the-newest-file-in-the-directory-is-different-from-the-previous-one%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









0













Please note that the following snippet will NOT work with filenames containing spaces, tabs or newlines.



To find out more about the find-command used here, please reference https://unix.stackexchange.com/a/240424/364705



#!/bin/bash
lastrun=''
while true; do
read -r newer older <<<
$(find . -type f -exec stat -c '%Y %n' {} ;
| sort -nr
| awk 'NR==1,NR==2 {print $2}'
| xargs )

if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d 'n' | sed '$ a '
lastrun="$newer"
fi
fi
done


And a solution using inotify-wait:



#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done





share|improve this answer




























  • This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

    – Sir Muffington
    yesterday











  • Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

    – markgraf
    yesterday











  • If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

    – markgraf
    yesterday











  • It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

    – Sir Muffington
    yesterday











  • $(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

    – markgraf
    yesterday


















0













Please note that the following snippet will NOT work with filenames containing spaces, tabs or newlines.



To find out more about the find-command used here, please reference https://unix.stackexchange.com/a/240424/364705



#!/bin/bash
lastrun=''
while true; do
read -r newer older <<<
$(find . -type f -exec stat -c '%Y %n' {} ;
| sort -nr
| awk 'NR==1,NR==2 {print $2}'
| xargs )

if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d 'n' | sed '$ a '
lastrun="$newer"
fi
fi
done


And a solution using inotify-wait:



#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done





share|improve this answer




























  • This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

    – Sir Muffington
    yesterday











  • Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

    – markgraf
    yesterday











  • If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

    – markgraf
    yesterday











  • It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

    – Sir Muffington
    yesterday











  • $(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

    – markgraf
    yesterday
















0












0








0







Please note that the following snippet will NOT work with filenames containing spaces, tabs or newlines.



To find out more about the find-command used here, please reference https://unix.stackexchange.com/a/240424/364705



#!/bin/bash
lastrun=''
while true; do
read -r newer older <<<
$(find . -type f -exec stat -c '%Y %n' {} ;
| sort -nr
| awk 'NR==1,NR==2 {print $2}'
| xargs )

if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d 'n' | sed '$ a '
lastrun="$newer"
fi
fi
done


And a solution using inotify-wait:



#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done





share|improve this answer















Please note that the following snippet will NOT work with filenames containing spaces, tabs or newlines.



To find out more about the find-command used here, please reference https://unix.stackexchange.com/a/240424/364705



#!/bin/bash
lastrun=''
while true; do
read -r newer older <<<
$(find . -type f -exec stat -c '%Y %n' {} ;
| sort -nr
| awk 'NR==1,NR==2 {print $2}'
| xargs )

if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d 'n' | sed '$ a '
lastrun="$newer"
fi
fi
done


And a solution using inotify-wait:



#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









markgrafmarkgraf

3427 bronze badges




3427 bronze badges
















  • This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

    – Sir Muffington
    yesterday











  • Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

    – markgraf
    yesterday











  • If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

    – markgraf
    yesterday











  • It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

    – Sir Muffington
    yesterday











  • $(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

    – markgraf
    yesterday





















  • This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

    – Sir Muffington
    yesterday











  • Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

    – markgraf
    yesterday











  • If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

    – markgraf
    yesterday











  • It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

    – Sir Muffington
    yesterday











  • $(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

    – markgraf
    yesterday



















This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

– Sir Muffington
yesterday





This works. However, when the file amount in the folder goes around over 1000 it appears to skip 1-3 files per second. Is this a performance limitation of find?

– Sir Muffington
yesterday













Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

– markgraf
yesterday





Could be a shell-script isn't fast enough if the files are coming quickly. If you do not have any subdirectories in the directory where you are running this, then you can drop the find..-exec and just do stat -c '%Y %n' * instead. That should be faster.

– markgraf
yesterday













If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

– markgraf
yesterday





If that still isn't fast enough, maybe look into inotifywait to trigger an action whenever a file is created/moved_to your directory.

– markgraf
yesterday













It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

– Sir Muffington
yesterday





It doesn't appear to be working without the find part. I already wrote a prototype for inotifywait, but still need to format it properly.

– Sir Muffington
yesterday













$(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

– markgraf
yesterday







$(stat -c '%Y %n' * | sort -nr ... instead of $(find . -type f -exec stat -c '%Y %n' {} ; | sort -nr .... But only if there are no subdirs.

– markgraf
yesterday












Sir Muffington is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Sir Muffington is a new contributor. Be nice, and check out our Code of Conduct.













Sir Muffington is a new contributor. Be nice, and check out our Code of Conduct.












Sir Muffington is a new contributor. Be nice, and check out our Code of Conduct.
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536334%2fcompare-if-the-newest-file-in-the-directory-is-different-from-the-previous-one%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...