 music |
 | � | OSdata.com |
cat
summary
����This subchapter looks at cat, a UNIX (and Linux) command.
����The cat (as in concatenate) utility can be used to concatenate several files into a single file.
����cat is most often used to obtain the contents of a file for input into a Linux or UNIX shell script.
����cat was part of the original 1969 version of UNICS (the original name for UNIX).
create names file
����If you did not create the names file suggested in the quick tour subchapter, please do so now, because you will use this file in future exercises. If you already created the names file, then you can safely skip this step and go to the main body of instruction for this subchapter.
����Type the command line, followed by RETURN or ENTER, then type each of the six suggested lines, each followed by the RETURN KEY. After having enetered each line, make sure you are at the beginning of a new (blank or empty) line and type Ctrl-D (hold downt he CONTROL key and the D key at the same time).
����$ cat > names
����James
����Mary
����John
����Patricia
����Robert
����Linda
����CONTROL-D
����$
����The choice of names is based on the most popular names in the United States in the year before this subchapter was written. Bribe me if you want your name put into the example.
check the file was created
����Use ls to make sure the file was created properly. It should be added to your directory listing.
����$ ls
����Desktop����������Movies��������Send registration
����Documents��������Music���������Sites
����Downloads��������Pictures������names
����Library����������Public
����$
display file contents
����Use the cat command to show the contents of your new file.
����$ cat names
����James
����Mary
����John
����Patricia
����Robert
����Linda
����$
cat
����This subchapter looks at cat, a UNIX (and Linux) command.
����cat is used concatenate file. The name is an abbreviation of catenate, a synonym of concatenate.
creating files
����One simple use of cat is to create simple files.
����Type cat > file01, followed by ENTER or RETURN.
����$ cat > file01
����The cursor will be at the beginning of a line that has no prompt. You are no longer in the shell, but instead in the cat utility. There is no cat prompt.
����Type the following lines (by convention, all of the input should be in bold, but to make it easier on the eye, it is in italics here):
����This is a line of text in my first file.
����This is another line.
����To be, or not to be: that is the question:
����1234567890
����ABC
����XYZ
����abc
����xyz
����Once you have typed in these lines, press RETURN to make sure you are at the beginning of a new line.
����Press Ctrl-D (hold down the CONTROL key and the D key at the same time). This indicates end-of-file (EOF), which informs cat that you have no more input. This will return you to the shell.
����Note that unlike some operating systems, the �.txt� file extension is not required by UNIX or Linux. It is optional, but the file extensions are often used to make it easier for a human to distinguish file types.
����Now repeat the process with the following file (by convention, all of the input should be in bold, but to make it easier on the eye, it is in italics here):
����There is a lot of typing in this example, but it is important for the upcoming exercises. Type the following lines (by convention, all of the input should be in bold, but to make it easier on the eye, it is in italics here):
����Because of the length of this file, you may want to download a copy from the internet. There is a copy at http://www.osdata.com/programming/shell/file02.txt � if you know how to download a copy and place it in your home directory, save yourself some typing time.
����$ cat > file02.txt
����This is a line of text in my second file.
����This is another line.
����To be, or not to be: that is the question:
����Whether 'tis nobler in the mind to suffer
����The slings and arrows of outrageous fortune,
����Or to take arms against a sea of troubles,
����And by opposing end them? To die: to sleep;
����No more; and by a sleep to say we end
����The heart-ache and the thousand natural shocks
����That flesh is heir to, 'tis a consummation
����Devoutly to be wish'd. To die, to sleep;
����To sleep: perchance to dream: ay, there's the rub;
����For in that sleep of death what dreams may come
����When we have shuffled off this mortal coil,
����Must give us pause: there's the respect
����That makes calamity of so long life;
����For who would bear the whips and scorns of time,
����The oppressor's wrong, the proud man's contumely,
����The pangs of despised love, the law's delay,
����The insolence of office and the spurns
����That patient merit of the unworthy takes,
����When he himself might his quietus make
����With a bare bodkin? who would fardels bear,
����To grunt and sweat under a weary life,
����But that the dread of something after death,
����The undiscover'd country from whose bourn
����No traveller returns, puzzles the will
����And makes us rather bear those ills we have
����Than fly to others that we know not of?
����Thus conscience does make cowards of us all;
����And thus the native hue of resolution
����Is sicklied o'er with the pale cast of thought,
����And enterprises of great pith and moment
����With this regard their currents turn awry,
����And lose the name of action. - Soft you now!
����The fair Ophelia! Nymph, in thy orisons
����Be all my sins remember'd.
����1234567890
����ABC
����XYZ
����abc
����xyz
����Now repeat the process with the following file (by convention, all of the input should be in bold, but to make it easier on the eye, it is in italics here � note the cheat method below):
����$ cat > numberfile
����1
����2
����3
����4
����5
����6
����7
����8
����9
����10
����11
����12
����13
����14
����15
����16
����17
����18
����19
����20
����Once again, use the RETURN key to enter a new line and then use Ctrl-D to exit cat.
����If you want to cheat on making this file, you can use the following command on Mac OS X or BSD systems:
����$ jot 20 1 > numberfile.txt
����$
����You will use these files in many of the exercises in this book.
PC-DOS equivalent
����cat is the UNIX equivalent of the MS-DOS or PC-DOS command TYPE. You can add the PC-DOS equivalent to your shell session with the alias command. To make the change permanent, add the following line to the .bashrc file in your home directory. Note that if you add this PC-DOS/MS-DOS equivalent, only add the all upper case version, because the lower case type is an important UNIX command that you will also need.
����$ alias TYPE="cat"
view a file with cat
����Type cat file 01, followed by the RETURN or ENTER key. You should see the contents of your first file. The beginning of the file may scroll off the top of your screen (this is normal).
����$ cat file01
����This is a line of text in my first file.
����This is another line.
����To be, or not to be: that is the question:
����1234567890
����ABC
����XYZ
����abc
����xyz
����$
combine files with cat
����Type cat file 02 numberfile, followed by the RETURN or ENTER key. This will show you the contents of both files, one immediately after the other. This is the use for which cat was named.
����$ cat file02 numberfile
����This is a line of text in my second file.
����This is another line.
����To be, or not to be: that is the question:
����Whether 'tis nobler in the mind to suffer
����The slings and arrows of outrageous fortune,
����Or to take arms against a sea of troubles,
����And by opposing end them? To die: to sleep;
����No more; and by a sleep to say we end
����The heart-ache and the thousand natural shocks
����That flesh is heir to, 'tis a consummation
����Devoutly to be wish'd. To die, to sleep;
�����������������many text lines�
����The fair Ophelia! Nymph, in thy orisons
����Be all my sins remember'd.
����1234567890
����ABC
����XYZ
����abc
����xyz
����1
����2
����3
����4
����5
����6
����7
����8
����9
����10
����11
����12
����13
����14
����15
����16
����17
����18
����19
����20
����$
����This should list your new file that combines combines two of the files you created. The beginning of the listing will scroll off the top of your screen. Don�t worry about it. We�ll see how to view long files in the subchapter about less.
display with line numbers
����The following example uses the command cat with the -n option to add line numbers to the listing of the file.
����Type cat -n file01.txt.
����$ cat -n file01.txt
��������1��This is a line of text in my first file.
��������2��This is another line.
��������3��To be, or not to be: that is the question:
��������4
��������5��1234567890
��������6��ABC
��������7��XYZ
��������8��abc
��������9��xyz
����$
����Notice that the blank line is numbered. You can use the -b option to get line numbers, skipping all blank lines.
����Type cat -b file01.txt.
����$ cat -b file01.txt
��������1��This is a line of text in my first file.
��������2��This is another line.
��������3��To be, or not to be: that is the question:
��������4��1234567890
��������5��ABC
��������6��XYZ
��������7��abc
��������8��xyz
����$
empty a file
����You can use cat to empty a file by sending /dev/null to the file to be emptied. The null device is bit nothingness.
����Start by making a copy of one of the files you just created so that you don�t wipe out your hard work.
����$ cp file01.txt emptyfile
����$
����Use cat to verify that the new file really does exist and has a complete copy of the original file.
����$ cat emptyfile.txt
����This is a line of text in my first file.
����This is another line.
����To be, or not to be: that is the question:
����1234567890
����ABC
����XYZ
����abc
����xyz
����$
����Now type cat /dev/null > emptyfile.txt. This will leave the file in existence, but empty it of all characters.
����$ cat /dev/null > emptyfile.txt
����$
����Confirm this by using both cat and wc (word count). The cat will simply return to the prompt because there is nothing to print out. The wc will show zero lines, zero words, and zero characters.
����$ cat file01.txt > emptyfile.txt
����$ wc names
�����������0�������0������0 emptyfile.txt
����$
replacing files
����The previous example brings up the point that if you use cat to output to an existing file, the previous file will be completely replaced by the new one. There is no warning at all.
appending files
����You can append to the end of an existing file by using two greater than signs (no spaces in between) >>.
����Start by making a copy of an existing file and then confirming the copy is correct.
����$ cp file01.txt file03.txt
����$ cat file03.txt
����This is a line of text in my first file.
����This is another line.
����To be, or not to be: that is the question:
����1234567890
����ABC
����XYZ
����abc
����xyz
����$
����Now try the append.
����$ cat numberfile.txt >>.file03.txt
����$
����And confirm that your append worked.
����$ cat file03.txt
����This is a line of text in my first file.
����This is another line.
����To be, or not to be: that is the question:
����1234567890
����ABC
����XYZ
����abc
����xyz
����1
����2
����3
����4
����5
����6
����7
����8
����9
����10
����11
����12
����13
����14
����15
����16
����17
����18
����19
����20
����$
hidden characters and spaces
����There are several switches that can be used to view files with non-printable characters, trailing spaces, and other hidden characters that can drive you crazy.
����On Linux (not on BSD or SYS V or Mac OS X), you can use the -E command to place a $ dollar sign at the end of each line. This will reveal trailing spces that would not otherwise be visible.
����$ cat -E names
����James$
����Mary$
����John$
����Patricia$
����Robert$
����Linda$
����$
����The -v switch will display non-printing characters. Control characters are preceded by the ^ caret character, so control-X would be listed as ^X. The delete character (octal 0177) is displayed as ^?. The non-ASCII characters (high bit set) are displayed with M- (for meta) followed by the character for the low seven bits.
����On Linux only, you can combine the -vE with the -e character (show nonprinting characters and the end of line marker).
����On Linux only, you can use the -T switch to show tab characters as ^I.
����You can use the -t switch to show tab characters as ^I and non-printing characters in the manner described for the -v switch. This is the equivalent of the Linux only -vT.
����On Linux only, you can use the -A switch as a shorter version of the ^vET combination.
squeeze lines
����You can use the -s switch to suppress multiple consecutive empty lines. Only one empty line will be displayed.
cat in scripts
����While cat is a great tool for qork from the command line, if you find yourself using cat in a script, you should probably rethink your algorithm. It is rare that you are on the right path when you find yourself using cat inside a shell script. The use of cat in a shell script is a common sign of amateur hacking and the script probably has serious beginner mistakes and problems as well.
other
����On November 8, 2010, Ramesh Natarajan named this the number 30 most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).
����In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.
comments, suggestions, corrections, criticisms
free music player coding example
����Coding example: I am making heavily documented and explained open source code for a method to play music for free � almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
����View music player in action: www.musicinpublic.com/.
����Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
����Names and logos of various OSs are trademarks of their respective owners.