As soon as you start working on the Linux command line, you have to start working with files. Linux follows a very powerful design philosophy expressed as everything is a file. This can take some getting used to, but is incredibly useful once you get it. Because once you’ve learned how to read and manipulate text files, you can do pretty much anything on your machine.
The first command you need to know is
cat. Cat is short for ‘concatenate’, and is used for writing text to and from files. So if I have a file in my current working directory, I can get its contents with cat:
$ echo "hello world" > myfile.txt $ cat myfile.txt hello world
The first line created the file
myfile.txt and directed the string “hello world” into it. The second line used
cat to read the contents of the file.
Here’s a more real-world example
$ cat /var/log/syslog
This prints out all the lines in the
syslog file, present on most unix systems. Don’t worry too much about the contents of the file, it’s probably mostly log records from your system booting up. Now, if you’re inspecting a log file, very often you only want the last few lines, rather than the entire thing. Linux provides a useful command for this very purpose
$ tail /var/log/syslog
Tail, as you might guess, prints out the ‘tail’, or last few lines, of a file. Its companion is
head, which prints the first few lines
$ head /var/log/syslog
If you want the ability to view large amounts of text and scroll through it, then the command you want is ‘
$ less /var/log/syslog
This allows you to scroll up and down through your file. Press q to return to the command line.
Here’s one last neat little trick. I mentioned that on Linux, everything is a file. Even the state of the system is represented as a file.
trevor@vm2:~$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 70 model name : Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz stepping : 1 microcode : 0xf cpu MHz : 2294.218 cache size : 6144 KB ...
Rather than having many different tools for different tasks, a Linux system is more like a box of Lego. The same tools can be plugged into all sorts of different places to enable you to do what you want. I use the humble
cat command on a daily basis to modify system settings, read files, and even monitor the health of my machines.
Read the rest of my ‘technology toolkit’ posts: