{"id":302,"date":"2011-11-22T14:14:25","date_gmt":"2011-11-22T14:14:25","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2011\/11\/22\/checking-file-attributes-in-perl\/"},"modified":"2017-12-25T09:11:41","modified_gmt":"2017-12-25T09:11:41","slug":"checking-file-attributes-in-perl","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/checking-file-attributes-in-perl\/","title":{"rendered":"How to Check  File Attributes in\u00a0Perl ? Perl File Attributes explained"},"content":{"rendered":"<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3879 aligncenter\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2011\/11\/file-attributes-in-perl.png\" alt=\"perl-file-attributes\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2011\/11\/file-attributes-in-perl.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2011\/11\/file-attributes-in-perl-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/h2>\n<h2>Checking File Attributes in\u00a0Perl<\/h2>\n<p>I have been using perl for quite some time now. I have also been using the file handling logic in my scripts. However, what I did not know till now is that you can quickly check for certain file properties in perl. Here is an example:<br \/>\n#!\/usr\/bin\/perl<br \/>\nmy (@description,$size);<br \/>\nif (-e $file)<br \/>\n{<br \/>\npush @description, \u2018binary\u2019 if (-B _);<br \/>\npush @description, \u2018a socket\u2019 if (-S _);<br \/>\npush @description, \u2018a text file\u2019 if (-T _);<br \/>\npush @description, \u2018a block special file\u2019 if (-b _);<br \/>\npush @description, \u2018a character special file\u2019 if (-c _);<br \/>\npush @description, \u2018a directory\u2019 if (-d _);<br \/>\npush @description, \u2018executable\u2019 if (-x _);<br \/>\npush @description, (($size = -s _)) ? \u201c$size bytes\u201d : \u2018empty\u2019;<br \/>\nprint \u201c$file is \u201c, join(\u2018, \u2018,@description),\u201d\\n\u201d;<br \/>\n}<br \/>\nNice, isn\u2019t it?<br \/>\nHere is the complete list of features that you can check:<\/p>\n<table border=\"1\" width=\"445\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"155\"><strong>Operator <\/strong><\/td>\n<td valign=\"top\" width=\"435\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-A<\/td>\n<td valign=\"top\" width=\"435\">Age of file (at script startup) in days since modification.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-B<\/td>\n<td valign=\"top\" width=\"435\">Is it a binary file?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-C<\/td>\n<td valign=\"top\" width=\"435\">Age of file (at script startup) in days since modification.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-M<\/td>\n<td valign=\"top\" width=\"435\">Age of file (at script startup) in days since modification.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-O<\/td>\n<td valign=\"top\" width=\"435\">Is the file owned by the real user ID?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-R<\/td>\n<td valign=\"top\" width=\"435\">Is the file readable by the real user ID or real group?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-S<\/td>\n<td valign=\"top\" width=\"435\">Is the file a socket?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-T<\/td>\n<td valign=\"top\" width=\"435\">Is it a text file?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-W<\/td>\n<td valign=\"top\" width=\"435\">Is the file writable by the real user ID or real group?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-X<\/td>\n<td valign=\"top\" width=\"435\">Is the file executable by the real user ID or real group?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-b<\/td>\n<td valign=\"top\" width=\"435\">Is it a block special file?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-c<\/td>\n<td valign=\"top\" width=\"435\">Is it a character special file?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-d<\/td>\n<td valign=\"top\" width=\"435\">Is the file a directory?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-e<\/td>\n<td valign=\"top\" width=\"435\">Does the file exist?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-f<\/td>\n<td valign=\"top\" width=\"435\">Is it a plain file?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-g<\/td>\n<td valign=\"top\" width=\"435\">Does the file have the setgid bit set?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-k<\/td>\n<td valign=\"top\" width=\"435\">Does the file have the sticky bit set?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-l<\/td>\n<td valign=\"top\" width=\"435\">Is the file a symbolic link?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-o<\/td>\n<td valign=\"top\" width=\"435\">Is the file owned by the effective user ID?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-p<\/td>\n<td valign=\"top\" width=\"435\">Is the file a named pipe?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-r<\/td>\n<td valign=\"top\" width=\"435\">Is the file readable by the effective user or group ID?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-s<\/td>\n<td valign=\"top\" width=\"435\">Returns the size of the file, zero size = empty file.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-t<\/td>\n<td valign=\"top\" width=\"435\">Is the filehandle opened by a TTY (terminal)?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-u<\/td>\n<td valign=\"top\" width=\"435\">Does the file have the setuid bit set?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-w<\/td>\n<td valign=\"top\" width=\"435\">Is the file writable by the effective user or group ID?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-x<\/td>\n<td valign=\"top\" width=\"435\">Is the file executable by the effective user or group ID?<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"155\">-z<\/td>\n<td valign=\"top\" width=\"435\">Is the file size zero?<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Checking File Attributes in\u00a0Perl I have been using perl for quite some time now. I have also been using the file handling logic in my scripts. However, what I did&#8230; <\/p>\n","protected":false},"author":1,"featured_media":3879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[14],"tags":[2679,438,349,2680,2681,1122,545,175,2683,2684,2682,2685,2686,2687,2578,213],"class_list":["post-302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-perl","tag-attributes","tag-check","tag-file","tag-file-attributes","tag-file-attributes-in-perl","tag-guide","tag-how","tag-perl","tag-perl-attributes","tag-perl-attributes-explained","tag-perl-attributes-tutorial","tag-perl-file-attributes-explained","tag-perl-file-attributes-guide","tag-perl-file-attributes-reference","tag-perl-tutorial","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":3881,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions\/3881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3879"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}