there is probably a simpler way to do this but I ran ls on the /media/user directory with no usb storage plugged in and saved the output to a file and I wanted to use diff to compare the file with a new usb plugged in and compare the file with the /media/user directory so that it detects the new directory in /media from the usb

I've used so far

ls > usbmedia.diff 
diff /home/user/desktop/scripts/usbmedia.diff /media/user/

the output I get is no /media/user/usbmedia.diff file so it is looking for a file of the same name but how do I get the diff command to not do that and compare the directory with the file

  • What exactly do you want? You want to detect if there is new USB devices plugged in? – c0rp Aug 14 '14 at 8:32
  • yeah basically I just want to detect the newly mounted media and know what it is exactly and I was kind of just practicing scripts and trying out new things there wasn't really a goal in mind – Eric Newman Aug 15 '14 at 3:32

You're nearly right.

/tmp is better practice:


while:                                            # Go forever.
    ls /media/<USERNAME>/ > /tmp/media.diff
    sleep 10
    ls /media/<USERNAME>/ > /tmp/mediacompare.diff
    diff /tmp/media.diff /tmp/mediacompare.diff
  • I just wrote the script for this out and my bash outputs: line 8: Syntax error: expected end of file im still fairly new to scripting but I'm assuming there has to be an end or something maybe like brackets? – Eric Newman Aug 15 '14 at 3:25
  • Sorry, added the done. – Tim Aug 15 '14 at 7:49

You could do this:

$ ls /media/user/ > /home/user/desktop/scripts/usbmedia.diff

Then plug in your device

$ diff /home/user/desktop/scripts/usbmedia.diff <(ls /media/user/)

<() this is called process substitution

You could read about process substitution <() here and here

  • This worked great just tested it out thank you very much i owe you an upvote as soon as I get the rep – Eric Newman Aug 15 '14 at 3:31
  • 1
    If this is that answer that worked, you can click the tick, below the down arrow. That gives them 15 and you 2. – Tim Aug 15 '14 at 7:49

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.