// API callback
related_results_labels_thumbs({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340"},"updated":{"$t":"2023-10-27T23:44:13.497+05:30"},"category":[{"term":"awk one liners"},{"term":"perl one liners"},{"term":"different methods"},{"term":"sed one liners"},{"term":"Different ways of"},{"term":"Shell Scripting"},{"term":"commands"},{"term":"Perl"},{"term":"shells"},{"term":"awk"},{"term":"Basics"},{"term":"sed"},{"term":"Administration"},{"term":"Filesystem"},{"term":"bash"},{"term":"utilities"},{"term":"Oracle"},{"term":"find"},{"term":"VI editor"},{"term":"awk tutorial"},{"term":"join"},{"term":"sed tutorial"},{"term":"sqlplus"},{"term":"shell script"},{"term":"vim"},{"term":"Productivity"},{"term":"awk parse CSV"},{"term":"file permissions"},{"term":"grep command"},{"term":"inodes"},{"term":"ksh"},{"term":"python"},{"term":"Inodes FAQ"},{"term":"Linux Interview Questions and answers"},{"term":"date"},{"term":"env variable"},{"term":"how to"},{"term":"parse CSV file"},{"term":"perl arrays"},{"term":"perl hash"},{"term":"python3"},{"term":"sed regular expression"},{"term":"typeset"},{"term":"C programming"},{"term":"FTP"},{"term":"GNU date"},{"term":"PATH variable"},{"term":"Soft Skill"},{"term":"associative array"},{"term":"awk date functions"},{"term":"awk sed perl grep substring"},{"term":"awk sum column values"},{"term":"awk time"},{"term":"convert uppercase"},{"term":"date formatting"},{"term":"line count"},{"term":"lines below pattern"},{"term":"linux"},{"term":"mail"},{"term":"mathematical"},{"term":"paste command"},{"term":"perl foreach"},{"term":"perl function"},{"term":"perl split string"},{"term":"profile"},{"term":"ps command"},{"term":"sed insert line"},{"term":"sed replace"},{"term":"shell script duplicates"},{"term":"soft link"},{"term":"sort remove duplicates"},{"term":"source"},{"term":"special variable IFS"},{"term":"split file"},{"term":"timestamps"},{"term":"unix linux process"},{"term":"zero padding"},{"term":"CDPATH"},{"term":"CGI"},{"term":"CPAN module"},{"term":"CPU utilization"},{"term":"IFS"},{"term":"Linux shell scripting interview questions"},{"term":"NSE"},{"term":"Oracle table csv"},{"term":"SUID"},{"term":"XML"},{"term":"__DATA__"},{"term":"__END__"},{"term":"alpha vantage"},{"term":"array"},{"term":"awk concatenate values"},{"term":"awk delimiter square brackets"},{"term":"awk first record group"},{"term":"awk insert column"},{"term":"awk insert line"},{"term":"awk join with delimiter"},{"term":"awk join without delimiter"},{"term":"awk match exact word"},{"term":"awk match pattern specific column"},{"term":"awk max value"},{"term":"awk min value"},{"term":"awk mktime"},{"term":"awk pass parameters"},{"term":"awk pattern matching"},{"term":"awk perl sed change delimiter"},{"term":"awk perl sed print trailer record"},{"term":"awk print lines before pattern"},{"term":"awk read files alternatively"},{"term":"awk sed perl cut extract characters"},{"term":"awk sed perl insert line before pattern"},{"term":"awk sed perl print lines below pattern"},{"term":"awk sed perl reverse file order"},{"term":"awk sed perl reverse string"},{"term":"awk sed perl right padding"},{"term":"awk sed print first few characters"},{"term":"awk sed swap lines in file"},{"term":"awk special variables"},{"term":"awk split file and insert header"},{"term":"awk split files every n line"},{"term":"awk split files on pattern"},{"term":"awk strftime"},{"term":"awk sum group"},{"term":"awk systime"},{"term":"awk unique values"},{"term":"awk varying length delimiter"},{"term":"awk word delimiter"},{"term":"bash arithmetic"},{"term":"bash substring"},{"term":"bc"},{"term":"bc command examples"},{"term":"best practice"},{"term":"case insensitive search"},{"term":"char"},{"term":"comm"},{"term":"comm command examples"},{"term":"command line arguments"},{"term":"compare files"},{"term":"concatenate lines"},{"term":"concatenate variables"},{"term":"convert ascii to hex"},{"term":"convert csv file to query"},{"term":"convert lowercase"},{"term":"convert text to query"},{"term":"cp"},{"term":"cron"},{"term":"ctags"},{"term":"cut"},{"term":"database"},{"term":"date command"},{"term":"date conversion"},{"term":"date functions"},{"term":"delete"},{"term":"delete character"},{"term":"delete control-M"},{"term":"delete line"},{"term":"df command"},{"term":"du command"},{"term":"environment"},{"term":"epoch time"},{"term":"eulers project"},{"term":"exclude directory"},{"term":"expr command"},{"term":"extern"},{"term":"filename"},{"term":"find files only current directory"},{"term":"find files specific directory"},{"term":"find module location"},{"term":"find xargs"},{"term":"find2perl"},{"term":"fold"},{"term":"foreach"},{"term":"gawk"},{"term":"getopts"},{"term":"group count"},{"term":"heredoc"},{"term":"inode changes"},{"term":"insert line beginning"},{"term":"insert line end"},{"term":"iterators"},{"term":"join method"},{"term":"ksh arrays examples"},{"term":"ksh substring"},{"term":"leave directory"},{"term":"length"},{"term":"line number"},{"term":"lines above pattern"},{"term":"link count"},{"term":"links"},{"term":"linux command line options"},{"term":"linux date command"},{"term":"ln command"},{"term":"map"},{"term":"multiple delimiters"},{"term":"multiple pattern search"},{"term":"multiple queries"},{"term":"negative pattern search"},{"term":"omit directory"},{"term":"ord"},{"term":"pandas"},{"term":"parse text file"},{"term":"pass awk variables to shell"},{"term":"paste command join lines"},{"term":"paste command merge files"},{"term":"paste command multiple files"},{"term":"paste command standard input"},{"term":"paste read file alternatively"},{"term":"pattern match"},{"term":"pattern matching"},{"term":"performance"},{"term":"perl DATA handler"},{"term":"perl alias"},{"term":"perl append string"},{"term":"perl builtin function"},{"term":"perl date"},{"term":"perl delete string"},{"term":"perl dumper"},{"term":"perl each"},{"term":"perl eval"},{"term":"perl file find"},{"term":"perl file handling"},{"term":"perl find files"},{"term":"perl hash examples"},{"term":"perl insert line"},{"term":"perl insert string"},{"term":"perl join"},{"term":"perl qr"},{"term":"perl regular expression"},{"term":"perl replace string"},{"term":"perl sed print lines before pattern"},{"term":"perl subtring"},{"term":"perl sum digits"},{"term":"preserve timestamp"},{"term":"prime numbers"},{"term":"print hash"},{"term":"print header"},{"term":"print nth line"},{"term":"print range of lines"},{"term":"print trailer record"},{"term":"ps TIME"},{"term":"push"},{"term":"python file writing"},{"term":"python iterator"},{"term":"python list"},{"term":"python read file"},{"term":"remove duplicates"},{"term":"remove leading zeros"},{"term":"replace characters"},{"term":"replace line"},{"term":"replace string"},{"term":"replace word"},{"term":"retrieve filename"},{"term":"reverse file"},{"term":"review"},{"term":"rlogin"},{"term":"sed delete blank lines"},{"term":"sed delete header trailer"},{"term":"sed delete line before after pattern"},{"term":"sed delete line ranges"},{"term":"sed delete lines ending with"},{"term":"sed delete not containing pattern"},{"term":"sed hold space"},{"term":"sed pattern space"},{"term":"sed print line"},{"term":"sed sum"},{"term":"set options"},{"term":"setuid"},{"term":"shell script UUC"},{"term":"shell script change delimiter"},{"term":"shell script read files alternatively"},{"term":"shell script reverse string"},{"term":"shell script sum"},{"term":"shell script swap lines in file"},{"term":"shell script to insert line before pattern"},{"term":"shell variables"},{"term":"shift"},{"term":"shift command"},{"term":"sort"},{"term":"sort and merge"},{"term":"sort file numerically"},{"term":"sort find duplicates"},{"term":"sort multiple fields"},{"term":"sort reverse"},{"term":"special variables"},{"term":"split"},{"term":"split command"},{"term":"split line"},{"term":"split multiple files"},{"term":"ssh"},{"term":"string length"},{"term":"string reverse"},{"term":"substitution"},{"term":"sum file contents"},{"term":"swap columns"},{"term":"swap lines"},{"term":"sys module"},{"term":"sys.path"},{"term":"table to csv"},{"term":"tac command"},{"term":"tags"},{"term":"time command"},{"term":"time difference"},{"term":"tr"},{"term":"umask"},{"term":"unix"},{"term":"unix find"},{"term":"unix shell scripting interview questions"},{"term":"unix sort examples"},{"term":"unix time"},{"term":"update"},{"term":"values"},{"term":"wantarray example"},{"term":"while loop"},{"term":"while loop IFS"},{"term":"while read multiple delimiters"},{"term":"word count"},{"term":"write method"},{"term":"writelines method"},{"term":"xargs"},{"term":"year 2038"},{"term":"yes command"}],"title":{"type":"text","$t":"The UNIX School"},"subtitle":{"type":"html","$t":"Instructions\/Tutorials on Unix\/Linux commands, C Programming, Unix Administration , Oracle, PL SQL, Perl, Productivity tips soft skills for newbies and professionals."},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/-\/shells?alt=json-in-script\u0026max-results=5"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/search\/label\/shells"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/-\/shells\/-\/shells?alt=json-in-script\u0026start-index=6\u0026max-results=5"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"12"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"5"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-6010857107841778455"},"published":{"$t":"2012-09-13T16:18:00.000+05:30"},"updated":{"$t":"2012-09-13T16:18:19.698+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"bash"},{"scheme":"http://www.blogger.com/atom/ns#","term":"env variable"},{"scheme":"http://www.blogger.com/atom/ns#","term":"ksh"},{"scheme":"http://www.blogger.com/atom/ns#","term":"profile"},{"scheme":"http://www.blogger.com/atom/ns#","term":"set options"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shells"}],"title":{"type":"text","$t":"$IGNOREEOF vs ignoreeof"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: x-large;\"\u003EH\u003C\/span\u003Eow to prevent your terminal from getting closed OR prevent from getting the user logged out on pressing Control-D?\u003Cbr \/\u003E\n\u0026nbsp; The answer to this is \u003Cb\u003EIGNOREEOF \/ ignoreeof\u003C\/b\u003E. Let us see what is this IGNOREEOF and the difference between IGNOREEOF and ignoreeof.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EControl-D\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;Many of the times we might have pressed some key only to realize that the terminal got closed or he got logged out from his account. The user gets logged out whenever an EOF character is pressed which is Control-D. \u0026nbsp;So, if the user happens to be in his login shell, then logically the Ctrl-D ends up in getting the user's terminal closed as well.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EBash\/Bourne shell\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; In Bash\/Bourne shell, the log-out from the user account can be prevented by using the environment variable IGNOREEOF.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cpre class=\"gpr1\"\u003Eexport IGNOREEOF=2\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; The \u003Ca href=\"http:\/\/www.theunixschool.com\/2010\/04\/what-is-difference-between-export-set.html\" target=\"_blank\"\u003Eenv variable\u003C\/a\u003E IGNOREEOF is set to 2. This means, the shell will neglect 'Ctrl-D' key 2 times. A warning message will be displayed to the user on pressing Control-D for 2 times, however on the 3rd time, the user will be logged out. So, the user can set a value of his choice. In this way, the user can prevent himself from getting logged out of the shell. It is ideal to put this setting in the \u003Ca href=\"http:\/\/www.theunixschool.com\/2011\/07\/what-is-profile-file.html\" target=\"_blank\"\u003Eprofile file\u003C\/a\u003E to make it permanent.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003Eexport IGNOREEOF\u003C\/pre\u003E\n\u0026nbsp; By just declaring the environment variable without any value, the shell will neglect Ctrl-D 10 times since 10 is the default value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; To unset the variable IGNOREEOF:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003Eunset IGNOREEOF\u003C\/pre\u003E\n\u003Cb\u003EKsh\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; The environment variable IGNOREEOF does not work for k-shell. Instead, it is done using one of the k-shell set command options, \"ignoreeof\". This is the difference between IGNOREEOF and ignoreeof.\u003Cbr \/\u003E\nTo set the 'ignoreeof' option in ksh:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003Eset -o ignoreeof\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;This means ignoreeof is set. Once set, the shell will neglect Ctrl-D for 20 times in case of a ksh93, 11 times in case of older ksh shells which are the default values. A user defined value cannot be set in ksh unlike the IGNOREEOF in bash shell.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; To unset the ignoreeof in ksh:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003Eset +o ignoreeof\n\u003C\/pre\u003E\nNote: ignoreeof works in bash and bourne shells as well."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/6010857107841778455\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/09\/ignoreeof-prevent-terminal-close-user-logout.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6010857107841778455"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6010857107841778455"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/09\/ignoreeof-prevent-terminal-close-user-logout.html","title":"$IGNOREEOF vs ignoreeof"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-703284045445428724"},"published":{"$t":"2012-05-28T16:33:00.000+05:30"},"updated":{"$t":"2020-07-11T10:16:17.058+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"commands"},{"scheme":"http://www.blogger.com/atom/ns#","term":"IFS"},{"scheme":"http://www.blogger.com/atom/ns#","term":"parse CSV file"},{"scheme":"http://www.blogger.com/atom/ns#","term":"parse text file"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shell script change delimiter"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shells"},{"scheme":"http://www.blogger.com/atom/ns#","term":"special variable IFS"},{"scheme":"http://www.blogger.com/atom/ns#","term":"while loop"},{"scheme":"http://www.blogger.com/atom/ns#","term":"while loop IFS"},{"scheme":"http://www.blogger.com/atom/ns#","term":"while read multiple delimiters"}],"title":{"type":"text","$t":"Shell - Read a text or CSV file and extract data"},"content":{"type":"html","$t":"\u0026nbsp; \u0026nbsp;In one of our earlier articles on awk, we saw how easily \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2011\/05\/awk-read-file-and-split-contents.html\" target=\"_blank\"\u003Eawk can parse a file and extract data from\u003C\/a\u003E it.\u0026nbsp;Shell also has properties with which we can handle text files: files with fields separated by white spaces or CSV files in which the fields are separated by a comma delimiter. Let us see in this article how to read and parse a file field by field or column by column and extract data from it using the \u003Ci\u003Ewhile \u003C\/i\u003Eloop of shell. This article will also explain the usage of shell while loop.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us consider a file with sample contents as shown below. The file contains 3 fields: OS, the company and a random value.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\nSolaris Sun 25\nLinux RedHat 30\n\u003C\/pre\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E1\u003C\/b\u003E. \u003Cb\u003Ewhile \u003C\/b\u003Ecommand, in addition to running a loop till the condition becomes false, can also \u003Cb\u003Eread a file and parse it\u003C\/b\u003E line by line. This we discussed once in the \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2012\/03\/15-different-ways-to-display-contents.html\" target=\"_blank\"\u003E15 different ways to display the file contents\u003C\/a\u003E. Below shown is a simple example demonstrating it:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\nwhile read line\ndo\n        echo \"Line is : $line\"\ndone \u0026lt; file\n\u003C\/pre\u003E\n\u0026nbsp; On running the above script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nLine is : Solaris Sun 25\nLine is : Linux RedHat 30\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;The script does: Read the file named \"file\"(input re-direction \u0026lt; ). Every line read is present in the variable \u003Ci\u003Eline.\u003C\/i\u003E\u0026nbsp;Inside the while loop, the line is printed which contains the entire line. The read condition becomes false when there are no lines to read at which point the while loop is quit.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNote: The \u003Ci\u003Eread\u003C\/i\u003E\u0026nbsp;statement reads till a newline character is found. And hence while loop is able to parse the file line by line.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2\u003C\/b\u003E. \u0026nbsp;\u003Cb\u003ERead every line into individual fields\u003C\/b\u003E using the while loop:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\nwhile read f1 f2 f3\ndo\n        echo \"OS is     : $f1\"\n        echo \"Company is: $f2\"\n        echo \"Value is  : $f3\"\ndone \u0026lt; file\n\u003C\/pre\u003E\n\u0026nbsp;On running the above script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nOS is     : Solaris\nCompany is: Sun\nValue is  : 25\nOS is     : Linux\nCompany is: RedHat\nValue is  : 30\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;\u003Ci\u003Eread\u003C\/i\u003E command can take multiple variable names as shown here in the form of f1, f2 and f3. The first field gets read into f1, second into f2 and so on.\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;How does the shell do it? How does it manage to split the fields properly and allocate it to variables? It is because of the shell special variable \u003Cb\u003EIFS \u003C\/b\u003Ewhich stands for \u003Cb\u003EI\u003C\/b\u003Enternal \u003Cb\u003EF\u003C\/b\u003Eield \u003Cb\u003ES\u003C\/b\u003Eeparator. IFS in shell is same as FS, the field separator, in awk. By default, IFS is white space which could be single space, series of single space, tab space or a new line. Hence, the shell is able to parse the fields approriately.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3\u003C\/b\u003E. What if there are fewer variable compared to fields? i.e, If we have lesser fields in read with more fields present in file?\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\n\nwhile read f1 f2\ndo\n\n        echo \"OS is       : $f1\"\n        echo \"Remaining is: $f2\"\ndone \u0026lt; file\n\u003C\/pre\u003E\n\u0026nbsp;On running the above script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nOS is       : Solaris\nRemaining is: Sun 25\nOS is       : Linux\nRemaining is: RedHat 30\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; What happened? The last variable in the \u003Ci\u003Eread \u003C\/i\u003Ecommand will consume all the remaining fields. Similarly, try and see what happens in the vice-versa case when there are more variables in the \u003Ci\u003Eread\u003C\/i\u003E\u0026nbsp;command than the number of fields present in the file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4\u003C\/b\u003E. \u003Cb\u003EChange the delimiter of a file\u003C\/b\u003E from a single space to a colon using the while loop:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E#!\/bin\/bash\n\nwhile read f1 f2 f3\ndo\n        echo $f1:$f2:$f3\ndone \u0026lt; file \u0026gt; file1\n\u003C\/pre\u003E\n\u0026nbsp; This script also shows how to re-direct the output of a while script to a file. Here, the file is read in the while loop, and the read variables are printed using the echo statement with a delimiter.\u003Cbr \/\u003E\n\u0026nbsp; Also, note this: \u0026nbsp;\"\u0026lt; file \u0026gt; file1\". The while reads from \u003Ci\u003Efile\u003C\/i\u003E\u0026nbsp;and writes to \u003Ci\u003Efile1\u003C\/i\u003E. The \u0026lt; indicates reading, \u0026gt; indicates writing to the output.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe output file contents will look as below:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file1\nSolaris:Sun:25\nLinux:RedHat:30\n\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5\u003C\/b\u003E.\u0026nbsp;\u003Cb\u003ERead every line from a CSV file into individual fields\u003C\/b\u003E\u0026nbsp;using the while loop. The CSV files are separated by a comma delimiter:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;Let us assume the sample file to contain data as below:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file.csv\nSolaris,Sun,25\nLinux,RedHat,30\n\u003C\/pre\u003E\nThe script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\nIFS=\",\"\nwhile read f1 f2 f3\ndo\n        echo \"OS is        : $f1\"\n        echo \"Company  is  : $f2\"\n        echo \"Value   is   : $f3\"\ndone \u0026lt; file.csv\n\u003C\/pre\u003E\nOn running the above script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nOS is        : Solaris\nCompany  is  : Sun\nValue   is   : 25\nOS is        : Linux\nCompany  is  : RedHat\nValue   is   : 30\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;You got it correct!! \u0026nbsp;Since IFS is the one which tells the read command how to split the fields, by setting the IFS to \",\", \u003Ci\u003Eread \u003C\/i\u003E\u0026nbsp;will now read the fields by separating them when a comma is encountered. In this way, we get the individual fields in the corresponding variables.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6\u003C\/b\u003E. \u003Cb\u003EDo not change the IFS permanently\u003C\/b\u003E. \u0026nbsp;In the above example, once the IFS is changed, it remains so till the end of the script. This may lead to a problem since the code below might have been written with the default behavior of IFS in mind. It is always considered a good practice to set the IFS to the old value once the purpose is solved.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\nOLDIFS=$IFS\nIFS=\",\"\nwhile read f1 f2 f3\ndo\n        echo \"OS is        : $f1\"\n        echo \"Company  is  : $f2\"\n        echo \"Value   is   : $f3\"\ndone \u0026lt; file\nIFS=$OLDIFS\n\u003C\/pre\u003E\nOR\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E#!\/bin\/bash\nwhile IFS=\",\" read f1 f2 f3\ndo\n        echo \"OS is        : $f1\"\n        echo \"Company  is  : $f2\"\n        echo \"Value   is   : $f3\"\ndone \u0026lt; file\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;In the first way, IFS is temporarily saved into a variable OLDIFS and once the operation is done, IFS is restored back to its old value.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;In the second way, IFS is being set to a new value with-in the while loop. Because of this, the scope of the IFS remains so only within the while loop. Once outside the while, IFS will retain its old value. You can write some echo statements before and after the while loop printing the IFS value to confirm the same.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7\u003C\/b\u003E. \u003Cb\u003ERead a file with dates\u003C\/b\u003E using the while loop and extract the date components year, month and date.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAssuming the file contents as shown below. The file contains the dates separated by a slash.:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\n2012\/05\/21\n2012\/05\/22\n\u003C\/pre\u003E\nThe script to parse the dates. Same as the earlier ones, except the IFS is set to slash.:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\nOLDIFS=$IFS\n\nIFS=\"\/\"\nwhile read f1 f2 f3\ndo\n        echo \"Year  is : $f1\"\n        echo \"Month is : $f2\"\n        echo \"Date  is : $f3\"\n\ndone \u0026lt; file\nIFS=$OLDIFS\n\u003C\/pre\u003E\nOn running the above script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nYear  is : 2012\nMonth is : 05\nDate  is : 21\nYear  is : 2012\nMonth is : 05\nDate  is : 22\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; This is now very easy to understand. Once the IFS is set to slash, we got the year, month and date components into f1, f2 and f3 respectively. Inside the while, we can use the variables for any use.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E8\u003C\/b\u003E. \u003Cb\u003EReading a file with multiple delimiters\u003C\/b\u003E in the shell:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAssuming the sample file contents as shown below. The difference here is the 1st and 2nd fields are separated by colon, whereas the 2nd and 3rd are separated by a slash.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\nSolaris:Sun\/25\nLinux:RedHat\/30\n\u003C\/pre\u003E\nThe script to parse the above file:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat test.sh\n#!\/bin\/bash\n\nOLDIFS=$IFS\nIFS=\":\/\"\nwhile read f1 f2 f3\ndo\n        echo \"OS is     : $f1\"\n        echo \"Company is: $f2\"\n        echo \"Value is  : $f3\"\ndone \u0026lt; file\nIFS=$OLDIFS\n\u003C\/pre\u003E\nOn running the script:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ .\/test.sh\nOS is     : Solaris\nCompany is: Sun\nValue is  : 25\nOS is     : Linux\nCompany is: RedHat\nValue is  : 30\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;Check out the IFS setting. IFS can be set to multiple values as well as shown. On setting it to colon and a slash, it starts splitting fields whenever it encounters either a colon or a slash. So, on encountering the colon first, it read the 1st column, and on encountering the slash, the 2nd column is read, and the last got in by default."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/703284045445428724\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/05\/shell-read-text-or-csv-file-and-extract.html#comment-form","title":"10 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/703284045445428724"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/703284045445428724"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/05\/shell-read-text-or-csv-file-and-extract.html","title":"Shell - Read a text or CSV file and extract data"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"10"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-3119531682628928194"},"published":{"$t":"2012-04-25T21:51:00.000+05:30"},"updated":{"$t":"2020-07-11T10:05:46.384+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"file permissions"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shells"},{"scheme":"http://www.blogger.com/atom/ns#","term":"umask"}],"title":{"type":"text","$t":"What is umask?"},"content":{"type":"html","$t":"\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; umask, as the man page says, stands for\u0026nbsp;\u003Cb\u003EU\u003C\/b\u003Eser file creation \u003Cb\u003Emask\u003C\/b\u003E\u0026nbsp;which is used for determining the default permission for a new file creation. The new file creation could either be a file creation through a normal process or a file copy. umask command is a shell built-in meaning it is an \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2012\/03\/internal-vs-external-commands.html\" target=\"_blank\"\u003E\u003Cb\u003Einternal command\u003C\/b\u003E\u003C\/a\u003E.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; The three file permission attributes are \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2010\/05\/file-permissions-vs-directory.html\" target=\"_blank\"\u003E\u003Cb\u003Eread, write and execute\u003C\/b\u003E\u003C\/a\u003E. These 3 are mapped to octal values as shown below:\u003C\/div\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; read \u0026nbsp; \u0026nbsp; - 4\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;write \u0026nbsp; \u0026nbsp; - 2\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;execute - 1\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp;In UNIX, the default file creation value is 666. 6 is 4+2(read + write). Permission 666 means 6 for the User, 6 for the group and 6 for others. Hence, a new file creation by default is meant to have read and write permission for User, group and others. This is the place where the umask comes into the picture. It is a kind of filter wherein we can choose to retain or block some of the default permissions from being applied on the file.\u0026nbsp;\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; Say, \u0026nbsp;the umask value is 0022. umask is by default displayed in Octal form, and hence the first 0 in the umask value is the indication for octal value. So, the actual umask is 022. This value together with the default file value(666) decides the final permission to be given to the file.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; Assume we create a file say \"file1\". The permissions given for this file will be the result coming from the substraction of the umask from the default value :\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; Default: 666\u003Cbr \/\u003E\n\u0026nbsp; umask : 022\u003Cbr \/\u003E\n---------------\u003Cbr \/\u003E\nResult : \u0026nbsp; 644\n\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;644 is the permission to be given on the file \"file1\". 644 means read and write for the User(\u003Cu\u003E6\u003C\/u\u003E44), read only for the group(6\u003Cu\u003E4\u003C\/u\u003E4) and others(64\u003Cu\u003E4\u003C\/u\u003E).\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ touch file1\n$ ls -l file1\n-rw-r--r-- 1 guru None 0 Apr 22 18:53 file1\n\u003C\/pre\u003E\nThe same rule is applied while creating a directory as well.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us have some FAQ:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. What is umask?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;umask is a number which defines the default permissions which are not to be given on a file. A umask of 022 means not to give the write permission to the group(0\u003Cu\u003E2\u003C\/u\u003E2) and others(02\u003Cu\u003E2\u003C\/u\u003E) by default.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. How to find out the umask value?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ umask\n0022\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;The option -S gives in more readable format.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ umask -S\nu=rwx,g=rx,o=rx\n\u003C\/pre\u003E\n\u0026nbsp; This means umask, at the max, allows all permissions for the user, read and execute alone for the group and others.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. How to set the umask value?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ umask 033\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; The same can be given in this below form as well:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ umask u=rwx,g=r,o=r\n\u003C\/pre\u003E\n\u003Cb\u003E4. How to set this umask permanently for a user?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; To set this value permanently for a user, it has to be put in the appropriate \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2011\/07\/what-is-profile-file.html\" target=\"_blank\"\u003E\u003Cb\u003Eprofile file\u003C\/b\u003E\u003C\/a\u003E which depends on the default shell of the user.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5. Does this umask come into picture while copying a file?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;Yes, during the copying of a file also umask is used. The new file created using the copy command also follows the same umask rules as above.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6. Can we retain the permission of the source file while copying a file?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;Yes, we can retain the permission of the source file using the cp command. As shown in the example 4 of one of our earlier articles on \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2012\/04\/cp-10-different-copy-command-examples.html\" target=\"_blank\"\u003E\u003Cb\u003Ecp command\u003C\/b\u003E\u003C\/a\u003E, the mode of the source file can be retained.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7. Who can set the umask value?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; It can be set by the root user which will be applicable across the system. Also, a given user can override the umask value by having his own setting in his\/her profile file."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/3119531682628928194\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/what-is-umask.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/3119531682628928194"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/3119531682628928194"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/what-is-umask.html","title":"What is umask?"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-1778858839679732039"},"published":{"$t":"2012-04-17T21:25:00.000+05:30"},"updated":{"$t":"2020-07-11T09:57:28.748+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Administration"},{"scheme":"http://www.blogger.com/atom/ns#","term":"CDPATH"},{"scheme":"http://www.blogger.com/atom/ns#","term":"env variable"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shells"}],"title":{"type":"text","$t":"What is CDPATH ?"},"content":{"type":"html","$t":"\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; Has it happened to you wherein you logged in to an account or to an environment and simply printed the command, say \"cd log\" and your directory gets changed to some log directory. The interesting part is: The log directory is not there in the directory from where you fired the cd command. It was somewhere also, however the cd command switched to it correctly. The question now is: \u0026nbsp;If the \"log\" directory is not present in the location from where we are giving the \"cd\" command, how did we reach the log directory? \u0026nbsp;The answer to this question is most probably \u0026nbsp;the \u003Cb\u003ECDPATH \u003C\/b\u003Eenvironment variable would have been set.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. What is CDPATH?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;CDPATH is an environment variable. It looks exactly like the PATH variable containing with many different paths concatenated using ':'.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E2. What is the use of CDPATH ?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Say, for example, some directories present in an \"X\" directory are being accessed frequently by the user. Every time the user wants to traverse to any of these directories present in \"X\", \u0026nbsp;he mostly traverses by giving the absolute path which is little time consuming if you have to find it. How nice will it be if we can do \"cd\" to the particular directory as if that directory which you are looking for is right under your current directory. This is what the CDPATH achieves.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; Normally, when the cd command is given, it checks for the directory name in the current directory and throws error if not found, else traverses to the directory. If the CDPATH is set, the \"cd\" command starts finding the directory in the list of directories present in the CDPATH variable and then makes the directory switch appropriately.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. How to set the CDPATH variable?\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;Setting the CDPATH variable is exactly like setting the PATH variable. For example:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ export CDPATH=\".:\/home\/guru:\/usr\"\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;This above command will hold good till the session is closed. In order to make this change permanent, we need to put this command in the appropriate \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2011\/07\/what-is-profile-file.html\" target=\"_blank\"\u003E\u003Cb\u003Eprofile file\u003C\/b\u003E\u003C\/a\u003E. The CDPATH command in this example is set with 3 components: The current directory followed by \/home\/guru and the \/usr.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; Let us try to test this CDPATH variable. Currently, we are in \/home\/guru. There are 3 directories present under it.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ pwd\n\/home\/guru\n$ ls -l | grep ^d\ndrwxr-xr-x+ 1 guru None       0 Feb  9 20:18 C\ndrwxr-xr-x+ 1 guru None       0 Apr 15 13:56 bin\ndrwxr-xr-x+ 1 guru None       0 Apr 15 18:35 perl\n\u003C\/pre\u003E\nNow, let us try to switch to the perl directory. And hence we will be in \/home\/guru\/perl. The switch to \"perl\" directory happened because the CDPATH contains the dot(.) at first place, and perl is under current directory(.)\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cd perl\n$ pwd\n\/home\/guru\/perl\n\u003C\/pre\u003E\nNow, let us try to switch to the bin directory which is under home whereas we are actually now under perl.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cd bin\n$ pwd\n\/home\/guru\/bin\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; This is how the CDPATH works. This is highly useful when you have some frequently traversed directories in the environment.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. How to find the value of the CDPATH variable?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ echo $CDPATH\n.:\/home\/guru:\/usr\n\u003C\/pre\u003E\n\u003Cb\u003E5. If CDPATH is set, will the cd command still search in the current directory by default before referring to the CDPATH?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; No. Once the CDPATH is set, the cd command will search only in the directories present in the CDPATH variable only. Hence, it is the responsibility of the user to make sure that the current directory is also present in the CDPATH variable. \u003Cbr \/\u003E\nNote: This point holds good when only the directory name is specified with cd. cd when given with absolute or relative path does not refer to the CDPATH command.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6. Can the current directory be anywhere in the CDPATH variable?\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; As such, there is no rule to put the current directory in specific place. However, it SHOULD always be the first component of the CDPATH. It is because if it is not there in first position, the default functioning of the cd command which is to switch to the required directory under current directory may not work. Hence, the cd command should always be made to search for the directory first in the current directory, and then only in the other directories present in the CDPATH variable.\u003C\/div\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/1778858839679732039\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/what-is-cdpath.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/1778858839679732039"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/1778858839679732039"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/what-is-cdpath.html","title":"What is CDPATH ?"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"1"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-6680738877611805866"},"published":{"$t":"2012-04-11T21:40:00.000+05:30"},"updated":{"$t":"2020-07-11T09:57:04.732+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"commands"},{"scheme":"http://www.blogger.com/atom/ns#","term":"cp"},{"scheme":"http://www.blogger.com/atom/ns#","term":"preserve timestamp"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shells"},{"scheme":"http://www.blogger.com/atom/ns#","term":"soft link"}],"title":{"type":"text","$t":"cp - 10 different copy command examples"},"content":{"type":"html","$t":"\u0026nbsp;\u0026nbsp;\u003Cb\u003Ecp \u003C\/b\u003Ecommand is used to copy files in Unix . There are some options in cp which add lot of value to this command. In this article, we will see about some of the rarely used options of the cp command which makes cp really powerful.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1.\u003C\/b\u003E\u0026nbsp;\u003Cb\u003ECopy a file\u003C\/b\u003E\u003Cbr \/\u003E\ncp command has the arguments source file and destination file. Here, we are copying file1 to file2.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp file1 file2\n$ ls -l file*\n-rw-r--r-- 1 guru users 163 Apr 11 15:36 file1\n-rw-r--r-- 1 guru users 163 Apr 11 15:40 file2\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;The same thing can also be achieved using the below command. It might look tricky if you are seeing this for the first time.\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp file{1,2}\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; When this above command runs, shell expands \"file{1,2}\" to \"file1 file2\". And hence it becomes in the right format as needed by the cp command. To understand it better, simply put an \"echo \" ahead of the cp command:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ echo cp file{1,2}\ncp file1 file2\n\u003C\/pre\u003E\n\u003Cb\u003E2.\u003C\/b\u003E\u0026nbsp; \u003Cb\u003ECopy a file named \"file\" to \"file1\":\u003C\/b\u003E\u003Cbr \/\u003E\nCopying file1 to file2 is fine. Using the same shortcut as above, how to copy \"file\" to \"file1\"?\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp file{,1}\n$ ls -l file*\n-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file\n-rw-r--r-- 1 guru users 163 Apr 11 15:36 file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; If this command is not clear, as did earlier, put an echo before cp and check it.\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;On carefully noticing the above output file listing, 2 things will be clear:\u003Cbr \/\u003E\n\u003Cul\u003E\n\u003Cli\u003Ecp command creates the new file with the current timestamp.\u003C\/li\u003E\n\u003Cli\u003EThe file permission is the default file permissions, not the file permissions of the original file.\u003C\/li\u003E\n\u003C\/ul\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E3.\u003C\/b\u003E \u0026nbsp; \u003Cb\u003EHow to copy a file by preserving the timestamp of the original file\u003C\/b\u003E? In other words, how to copy a file with the same timestamp as that of the original file?\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp --preserve=timestamp file file1\n$ ls -l file*\n-rw-rw-r-- 1 guru users 163 Apr 10 \u003Cspan style=\"color: red;\"\u003E13:50\u003C\/span\u003E file\n-rw-r--r-- 1 guru users 163 Apr 10 \u003Cspan style=\"color: red;\"\u003E13:50\u003C\/span\u003E file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;GNU cp provides an option \u003Ci\u003E--preserve\u003C\/i\u003E using which the timestamp of the original file can be retained.\u0026nbsp;3 properties of a file are of importance while copying a file:\u003Cbr \/\u003E\n\u003Cul\u003E\n\u003Cli\u003ETime stamp\u003C\/li\u003E\n\u003Cli\u003EFile Modes\u003C\/li\u003E\n\u003Cli\u003EOwnership\u003C\/li\u003E\n\u003C\/ul\u003E\n\u003Cdiv\u003E\n\u003Cb\u003E4.\u003C\/b\u003E\u0026nbsp;\u003Cb\u003ECopy a file retaining the modes of the source file\u003C\/b\u003E:\u003Cbr \/\u003E\nLike timestamp, cp can also retain the mode of the files and ownership as well. The same --preserve switch does it.\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp --preserve=mode file file1\n$ ls -l file*\n-\u003Cspan style=\"color: red;\"\u003Erw-rw-r\u003C\/span\u003E-- 1 guru users 163 Apr 10 13:50 file\n-\u003Cspan style=\"color: red;\"\u003Erw-rw-r\u003C\/span\u003E-- 1 guru users 163 Apr 11 15:35 file1\n\u003C\/pre\u003E\n\u003Cb\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003C\/b\u003EThe mode of the \"file\" has been retained in \"file1\" as well.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5.\u003C\/b\u003E\u0026nbsp;\u003Cb\u003ECopy a file preserving timestamp, mode and ownership\u003C\/b\u003E:\u003Cbr \/\u003E\nTo copy a file with all the 3 properties of the original file, use the \"-p\" option. \"-p\" is a refined form of \"--preserve\" wherein all the 3 properties(timestamp, mode and ownership) are copied as is from the original file.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp -p file file1\n$ ls -l file*\n-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file\n-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file1\n\u003C\/pre\u003E\n\u003Cb\u003E6.\u003C\/b\u003E\u0026nbsp; \u003Cb\u003ECreate a link using the cp command\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp;ln command\u0026nbsp;is the one which is used in creating \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2012\/03\/soft-links-hard-links-all-about-inodes.html\" target=\"_blank\"\u003Esoft links and hard links\u003C\/a\u003E. cp command can also create soft links and hard links.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp -l file file1\n$ ls -li file*\n\u003Cspan style=\"color: red;\"\u003E9962525 \u003C\/span\u003E-rw-rw-r-- 2 guru users 163 Apr 10 13:50 file\n\u003Cspan style=\"color: red;\"\u003E9962525 \u003C\/span\u003E-rw-rw-r-- 2 guru users 163 Apr 10 13:50 file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;The \"-l\" option of the cp command does not copy files, instead creates a hard link of the source file. The same inode numbers of file and file1 proves it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7.\u003C\/b\u003E\u0026nbsp; \u003Cb\u003ECreate a soft link using the cp command\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;cp can \u003Cb\u003E\u0026nbsp;\u003C\/b\u003Ecreate soft links using the \"-s\" option.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp -s file file1\n$ ls -l file*\n-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file\nlrwxrwxrwx 1 guru users   4 Apr 11 14:59 file1 -\u0026gt; file\n\u003C\/pre\u003E\n\u003Cb\u003E8\u003C\/b\u003E. \u0026nbsp;\u003Cb\u003ECopy only if the source file is updated\u003C\/b\u003E:\u003Cbr \/\u003E\nSometimes, we copy\u003Cb\u003E \u003C\/b\u003Ea file the need for which could be to copy only if the source file is updated(time stamp). Keep in mind, cp is an \u003Ca href=\"http:\/\/unix-school.blogspot.com\/2012\/03\/internal-vs-external-commands.html\" target=\"_blank\"\u003Eexternal command\u003C\/a\u003E. Some extra code need to be written to check if the source file is updated of late. cp provides an option \"-u\" for this purpose. With -u, the file will be copied only if the source file is newer than the destination file.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ date\nWed Apr 11 \u003Cspan style=\"color: red;\"\u003E16:33\u003C\/span\u003E:42 IST 2012\n$ cp -u file1 file2\n$ ls -l file*\n-rw-r--r-- 1 guru users  163 Apr 11 \u003Cspan style=\"color: red;\"\u003E15:36\u003C\/span\u003E file1\n-rw-r--r-- 1 guru users  163 Apr 11 \u003Cspan style=\"color: red;\"\u003E15:40\u003C\/span\u003E file2\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; As shown above, cp did not copy the file since file2 is newer than file1.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E9. \u003C\/b\u003ELet us just \u003Ci\u003Etouch\u003C\/i\u003E the file file1 and try the cp.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ touch file1\n$ cp -u file1 file2\n$ ls -l file*\n-rw-r--r-- 1 guru users  163 \u003Cspan style=\"color: red;\"\u003EApr 11 16:31\u003C\/span\u003E file1\n-rw-r--r-- 1 guru users  163 \u003Cspan style=\"color: red;\"\u003EApr 11 16:31\u003C\/span\u003E file2\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; As seen above, file2 is copied since the file1 has a timestamp later than file2.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E10. Copy a file with a pre-defined time stamp:\u003C\/b\u003E\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; Say, we want to have the copied file with a specific time stamp of the user choice. cp does not provide an option for this. Instead, we can copy the file normally, and use the touch command to change the timestamp of our choice.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cp file1 file2\n$ touch -t 1204091524 file2\n$ ls -l file2\n-rw-r--r-- 1 guru users 163 Apr  9 15:24 file2\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; As above, the touch command can be used to change the timestamp of the file to any date using the \"-t\" option. The time stamp of the file file2 has now been changed to \"1204091524\" which stands for 2012(12), April(04), 9th(08), 15 hours(15) and 24 minutes(24).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHappy Copying!!!"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/6680738877611805866\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/cp-10-different-copy-command-examples.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6680738877611805866"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6680738877611805866"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/04\/cp-10-different-copy-command-examples.html","title":"cp - 10 different copy command examples"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"1"}}]}});