I am currently writing a small setup script for a Linux application that needs the user to edit a configuration file before the application is started. I've chosen to make the script simply open the configuration file in Nano, and resume the script afterwards. I do, however, need to detect whether the user saved the changes (to then continue starting the application), or whether he discarded them (which would indicate the user doesn't want to continue).

I have already checked whether this is possible with the returned exit code from Nano, and it apparently isn't - it always returns 0 even if the changes were discarded. Is there another way to figure out whether the file was changed and saved, or will I have to do this in an entirely different way?

  • 1
    Make a tmp copy of the file and have the user edit that instead of the original. When they exit nano, diff the tmp file with the default to see if it was changed. – jw013 May 4 '12 at 17:33
  • 2
    You shouldn't default to nano, it is better to use whatever $EDITOR is set to. – Emil May 4 '12 at 17:56
  • Do you want to check whether the user changed the file or whether he saved the file?  I don't know nano, but I know that, in vi, you can type :wq and it will save (i.e., rewrite) the file even if you made no changes.  The accepted answer will consider that to be a positive result (yes, the user saved the file).  The temp copy approach (suggested in a comment, above) recognizes only actual edits (changes) to the file.  If you're concerned about the space requirements of making a copy, just hash it before and after. – G-Man Says 'Reinstate Monica' Oct 25 '15 at 1:48

You can use the stat command to check the file's modification time before and after nano. Something like:

oldtime=`stat -c %Y "$filename"`
nano "$filename"

if [[ `stat -c %Y "$filename"` -gt $oldtime ]] ; then
  echo $filename has been modified

Of course, this won't detect whether nano modified the file, or some other program did, but that could be considered a feature. (You can use some other program to edit the file, and then exit nano without saving.)


I was recently trying to do something similar, I eventually used a modification of @cjm's approach.

First of all, under Darwin, I used stat -f %m to get the time (in seconds) since the file was modified.

This works well, but there is a small chance that the user will save and exit the editor in the same second as a modification made by your script. To solve this, I manually bump the modified time of the file by one second using touch.


echo "some text" > myfile.txt
touch -A -01 myfile.txt
oldtime=`stat -f %m myfile.txt`
vi myfile.txt
if [[ `stat -f %m myfile.txt` -gt $oldtime ]] ; then
    echo "file was edited"
  • The -A option to touch is not universally available.  A simpler way of getting a similar effect would be to sleep 1 before invoking the editor. – G-Man Says 'Reinstate Monica' Oct 25 '15 at 1:08
  • 1
    Granted simpler, but I cringe at the thought of making the user wait a second before popping the editor open. – Julian Oct 30 '15 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.