The Linux cat command at first seems so simple as to be unnecessary. In actuality, it is an excellent example of the Unix philosophy: create programs which do one thing and do it well. The thing cat does well is display the contents of a file or files. Many other utilities can handle this task, but none have all the options cat does. First, let's look at the simplest case:
cat /etc/motdThis command will display the contents of the motd (messages of the day) file to your screen. Unlike more (or less), cat will not stop when the screen is full. This is a feature, not a bug. You don't want pauses when redirecting (using the > operator) cat's output to a device, e.g., a printer or modem:
cat /etc/motd > /dev/modemcat comes from the word conCATenate, which describes one of its best uses: to concatenate or “glue together” two or more files. If you have several individual files about animals you would like to collect together into one file, cat will do the work for you. For example:
cat tiger lion cougar > bigcatswould redirect the concatenation output, containing the three feline files in the specified order, into a new file named bigcats. If you find another file, panther, that needs to be added to the bigcats file, use cat with the append (>>) operator in the following way:
cat panther >> bigcatsUsing >> ensures any prior content of bigcats is preserved. The content of panther is appended to bigcats. If you were to use the > operator here, you would replace the contents of bigcats with the contents of panther. Always use >> when you wish to add to the end of an existing file.
Be careful not to use the same file name when redirecting the output of the cat command, or you could lose one of the files. For example, don't do the following:
cat myfile yourfile > yourfile
In this case yourfile gets overwritten by myfile.
Another surprisingly handy use for cat is to redirect standard input like this:
cat > newfile some notes I want to save in newfile. CTRL-D
This creates a new file (named newfile). You type as much text as you wish, then type ctrl-D to save the file. You can backspace over mistakes, but you cannot go to a previous line after you press the enter key. I often teach this particular option of cat to novice Unix users, who occasionally need to create simple files, but don't want to learn vi or other simple editors. There may be Unix systems without vi or your favorite editor, but cat is always there.
The operator >> can also be used to append notes to the end of newfile:
cat >> newfile Adding another note to newfile. CTRL-D
Like most Unix commands, the behavior of cat can be modified by command line switches. If you use the diff command to compare files, it will show you the numbers of lines that differ between the files. However, most files don't have line numbers. Use cat with the -n switch to number each line of a file:
cat -n kittens > num_kittens
The file num_kittens is kittens with a number in front of each line, including blank lines. Use the -b switch to number only lines that are not blank.
One last cat trick: using the -v switch will show you “hidden” characters, such as control characters that may not show up in your editor. Try this experiment:
cat > catestv CTRL-v testing CTRL-O Testing esc-b CTRL-D
If we use cat to view the file, we see only the normal text:
cat catestv Testing TestingTo see what's actually in the file, use the -v option:
cat -v catestv ^V Testing ^O Testing ^[bHere, a ^ in front of a character signifies a control character. (CTRL-[ is the same as ESC).
Patrick Hill (email@example.com) is a computer engineer at Alabama Power Company in Birmingham, Alabama. He is known around the office as the guy who uses cat for an editor.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Doing for User Space What We Did for Kernel Space
- Tech Tip: Really Simple HTTP Server with Python
- SuperTuxKart 0.9.2 Released
- Parsing an RSS News Feed with a Bash Script
- Rogue Wave Software's Zend Server
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide