{"id":23109,"date":"2016-07-15T03:01:47","date_gmt":"2016-07-14T17:01:47","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=23109"},"modified":"2016-06-28T16:54:49","modified_gmt":"2016-06-28T06:54:49","slug":"linux-file-recovery-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-file-recovery-primer-tutorial\/","title":{"rendered":"Linux File Recovery Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/copy_depending.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux File Recovery Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/copy_depending.jpg\" title=\"Linux File Recovery Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux File Recovery Primer Tutorial<\/p><\/div>\n<p>We&#8217;re creating a <a target=_blank title='Korn Shell information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Korn_shell'>Korn Shell<\/a> program today for use with the Mac OS X command line, or for use with Linux or unix, in posing the following individual file restore or recovery <b>scenario<\/b> &#8230;<\/p>\n<blockquote><p>\n<font color=olive>#! \/bin\/ksh<\/font><br \/>\n# copy_depending.ksh<br \/>\n# If filesize too small and the filesize of something else okay and filesize of first is<br \/>\n#   less than minbytes copy second onto first ksh script tutorial<br \/>\n<b># Possible usage is that parameter two file is a backup of parameter 1 file and in the<br \/>\n#   meantime a bad change happened to file 1 causing it to become smaller than<br \/>\n#   parameter 3 minimum bytes setting &#8230; this script gets it back to the way it was<br \/>\n#   as the backup happened<\/b>\n<\/p><\/blockquote>\n<p><code><br \/>\n<b><\/b><br \/>\nif [ <font color=green>! -f<\/font> \"<font color=brown>$1<\/font>\" -o ! -f \"$2\" -o -z \"$3\" ]; then<br \/>\n  echo \"Usage: copy_depending.ksh [file1] [file2] [minbytes]  # if filesize too small and\\<br \/>\n the filesize of something else okay and filesize of first is less than minbytes\\<br \/>\n copy second onto first\";<br \/>\nelse<br \/>\n<b><\/b><br \/>\n  size1=\"`<font color=red>stat<\/font> $1 | cut -d' ' -f 8`\";<br \/>\n  size2=\"`stat $2 | cut -d' ' -f 8`\";<br \/>\n b=\"`basename $1`\"<br \/>\n  if [ $size1 -lt $3 ]; then<br \/>\n   if [ $size1 -lt $size2 ]; then<br \/>\n    if [ $1 <font color=purple>-nt<\/font> $2 ]; then<br \/>\n     msg=\"New $1 from $2 (backup, perhaps) and will email contents of the clobbered $1\";<br \/>\n     cat \"$1\" | <font color=blue>uuencode \"$b\" | mailx -s \"$msg\" rmetcalfe15@gmail.com;<\/font><br \/>\n     <font color=orange>cp -f $2 $1<\/font><br \/>\n    fi<br \/>\n   fi<br \/>\n  fi<br \/>\nfi<br \/>\n<b><\/b><br \/>\nexit<br \/>\n<\/code><\/p>\n<p> &#8230; oops &#8230; that&#8217;s all of it (<a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/copy_depending.ksh_GETME\" title=\"copy_depending.ksh\">copy_depending.ksh<\/a>) today &#8230; <font size=1>better be off then<\/font> &#8230; no &#8230; want to explain a bit in color coded terms.<\/p>\n<ul>\n<li><font color=olive>Today&#8217;s <a target=_blank title='Shebang information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Shebang_(Unix)'>Shebang<\/a> points at Korn Shell usage and makes <i>.\/copy_depending.ksh<\/i> work but please read the <i>chmod<\/i> bit further below to complete this knowledge<\/font><\/li>\n<li><font color=brown>Command line parameters usually get arranged so that $0 points at the script itself, $1 is first parameter, $2 is second etcetera<\/font><\/li>\n<li><font color=green>This is checking for the non-existance of what follows<\/font><\/li>\n<li><font color=blue><a title='Linux mailx Primer Tutorial' href='#lmpt'>Linux mailx Primer Tutorial<\/a> explains this below<\/font><\/li>\n<li><font color=red>The <a title='man stat' href='http:\/\/linux.die.net\/man\/2\/stat'><i>stat<\/i><\/a> command is what we use to extract the filesize information from the eighth column<\/font><\/li>\n<li><font color=orange>The <a title='man cp' href='http:\/\/linux.die.net\/man\/1\/cp'><i>cp<\/i><\/a> command is what we use to restore the file, should it come to that<\/font><\/li>\n<li>This <font color=purple>is checking if $1 file is newer than the $2 file<\/font><\/li>\n<\/ul>\n<p>Today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/copy_depending.jpg\" title='Click picture'>tutorial picture<\/a> shows you a simulation of this Korn Shell script&#8217;s usage.  In order to be able to go &#8230;<\/p>\n<p><code><br \/>\n.\/copy_depending.ksh you_only_read_once.html you_only_read_once.html_backup 1500<br \/>\n<\/code><\/p>\n<p> &#8230; during this session we had to, ahead of time, make sure the execute bit was set for the relevant user and we did this via &#8230;<\/p>\n<p><code><br \/>\nchmod 755 copy_depending.ksh<br \/>\n<\/code><\/p>\n<p>Perhaps this could be the (basis for the) backup to the backup you need.<\/p>\n<hr>\n<p id='lmpt'>Previous relevant <a target=_blank title='Linux mailx Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/Linux mailx Primer Tutorial\/'>Linux mailx Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux mailx Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg\" title=\"Linux mailx Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux mailx Primer Tutorial<\/p><\/div>\n<p>Think Linux and you may not associate it with email, but Linux can be excellent for many email requirements, especially using mailx and uuencode.   The use of uuencode allows you to give your email an attachment, and the mailx -s switch for the subject of the email can be used for both the purpose it was meant for, that is to establish an email subject, and with a stretch of imagination of usage, the body text, because of the quirk whereby anything after the first line of the subject will spill over into the body text of the email.   So, am sure there would be limits to the length of this body text, but you can often be brief when there is a good attachment to provide your email recipient.   Naturally, in the day and age of worry about computer viruses, your attachment will most likely be vetted by the receiving email client software for viability, so be aware of this.   Other than all that, this method of sending emails is potentially very powerful.  Notice that great way Linux and Unix can use `cat body.txt` type of syntax to embed one command within another &#8230; sometimes Linux is so simple, powerful, brilliant!   You may have noticed how short all the commands in Linux and Unix tend to be, and that is because it was intended to be short and powerful, and this is brilliant.<\/p>\n<p><b>Background reading for tutorial:<\/p>\n<ul>\n<li><a target=_blank title='mailx' href='http:\/\/linux.die.net\/man\/1\/mailx'>mailx<\/a><\/li>\n<li><a target=_blank title='uuencode' href='http:\/\/linux.die.net\/man\/1\/uuencode'>uuencode<\/a><\/li>\n<\/ul>\n<p><\/b>\n<\/p>\n<p>Here is a tutorial that introduces you to some email with attachment, subject and body text from the Linux <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/Linux\/mailx\/Linux_mailx_uuencode.jpg'>command line<\/a>.<\/p>\n<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d4673' onclick='var dv=document.getElementById(\"d4673\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d4673' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d23109' onclick='var dv=document.getElementById(\"d23109\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d23109' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re creating a Korn Shell program today for use with the Mac OS X command line, or for use with Linux or unix, in posing the following individual file restore or recovery scenario &#8230; #! \/bin\/ksh # copy_depending.ksh # If &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-file-recovery-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1,29,37],"tags":[128,205,234,265,707,725,997,1926,1927,1319,1339],"class_list":["post-23109","post","type-post","status-publish","format-standard","hentry","category-elearning","category-uncategorised","category-operating-system","category-tutorials","tag-backup","tag-chmod","tag-command-line","tag-copy","tag-linux","tag-mac-os-x","tag-programming","tag-restore","tag-shebang","tag-tutorial","tag-unix"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/23109"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=23109"}],"version-history":[{"count":8,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/23109\/revisions"}],"predecessor-version":[{"id":23117,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/23109\/revisions\/23117"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=23109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=23109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=23109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}