{"id":494,"date":"2009-10-23T14:49:02","date_gmt":"2009-10-23T19:49:02","guid":{"rendered":"http:\/\/www.gamescheat.ca\/?p=494"},"modified":"2019-09-04T19:57:40","modified_gmt":"2019-09-05T00:57:40","slug":"unix-bash-script-to-check-if-the-files-exists-in-the-directory","status":"publish","type":"post","link":"http:\/\/www.gamescheat.ca\/?p=494","title":{"rendered":"Unix bash script to check if the files exists in the directory"},"content":{"rendered":"<p>As a server administrator I seldom have to check to see if a file exists in the directory. But what if you have a long list of files that you need to check for?  Checking for the existence of many many files manually could prove to be tedious and repetitive task.  It would be so easy if you could automate this task, but fear not as I have created a script to automate this process.  It would have taken me hours and severe case of carpal tunnel syndrome to manually check for files, but with this handy script it only took a matter of minutes.<\/p>\n<p>Here is the script below, simply cut and paste it into your unix editor.<\/p>\n<blockquote><p>#!\/bin\/bash<br \/>\n# Andrew Lin, www.gamescheat.ca<br \/>\n# This script will get the input from a file<br \/>\n# and check to see if the files exists.<\/p>\n<p># files=\u201d\/facebook\/login\/craigslist \/facebook\/login\/freeones \/youtube\/myspace\/meghan_mccain\u201d      <\/p>\n<p>INPUTFILE=\u201d\/facebook\/login\/games\u201d<br \/>\nfiles=\u201d$(cat $INPUTFILE)\u201d                                 <\/p>\n<div style=\"float: left;\"><div style=\"margin: 15px 15px 15px 15px\";><script type=\"text\/javascript\"><!--\ngoogle_ad_client = \"pub-3319935785736004\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 336;\ngoogle_ad_height = 280;\ngoogle_ad_format = \"336x280_as\";\ngoogle_ad_type = \"text_image\";\ngoogle_ad_channel =\"\";\ngoogle_color_border = \"cccccc\";\ngoogle_color_link = \"cc0000\";\ngoogle_color_bg = \"ffffff\";\ngoogle_color_text = \"000000\";\ngoogle_color_url = \"008000\";\n\/\/--><\/script>\n<script type=\"text\/javascript\"\n  src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\n<\/script><\/div><\/div><p>for i in $files<br \/>\ndo                                               <\/p>\n<p>  if [ -e $i ];<br \/>\n  then<br \/>\n     echo \u201cFile Exists $i\u201d >> \/youtube\/myspace\/yahoo-result<br \/>\n  else<br \/>\n     echo \u201cFile does not exists $i\u201d >> \/youtube\/myspace\/yahoo-result<br \/>\n  fi                                                      <\/p>\n<p>done<\/p><\/blockquote>\n<p><em>#files=\u201d\/facebook\/login\/craigslist \/facebook\/login\/freeones \/youtube\/myspace\/meghan_mccain\u201d<\/em><br \/>\nThe # symbol at the beginning of the line means the line is a comment.  In this case if you wanted to check for a short list of files you can simply enter the file names and path like this.<\/p>\n<p><em>INPUTFILE=\u201d\/facebook\/login\/games\u201d<\/em><br \/>\nDefine the variable INPUTFILE with the name and path of the file games.  This file contains the names of files you wish to check for.<\/p>\n<p><em>files=\u201d$(cat $INPUTFILE)\u201d<\/em><br \/>\nThe variable file contains the contents of the file games.<\/p>\n<p><em>for i in $files<br \/>\ndo<\/em><br \/>\nThe for loop will make the variable i equal the first line of $files, then execute the command following do.  The counter is then incremented and i is then equal the second line of $files, and then the commands after do is executed again.<\/p>\n<p><em>if [ -e $i ];<br \/>\n  then<br \/>\n     echo \u201cFile Exists $i\u201d >> \/youtube\/myspace\/yahoo-result<\/em>-e means if the file name contained in the variable $1 exists, then write File Exists and the name and path of the file into the file \/youtube\/myspace\/yahoo-result.<\/p>\n<p><em>else<br \/>\n     echo \u201cFile does not exists $i\u201d >> \/youtube\/myspace\/yahoo-result<\/em><br \/>\nIf not then write file does not exists and the path of file into \/youtube\/myspace\/yahoo-result.<\/p>\n<p><em>done<\/em><br \/>\nAfter the for loop has reached the end of $files, then done exixts the loop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a server administrator I seldom have to check to see if a file exists in the directory. But what if you have a long list of files that you need to check for? Checking for the existence of many&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.gamescheat.ca\/?p=494\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[55,35],"_links":{"self":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/494"}],"collection":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":8,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":987,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/987"}],"wp:attachment":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}