permalink

0

12 Useful Unix/Linux command line tools – also for Windows or Macs

Tools for the command line seem to be a relict of the past. Who wants to deal with various commands having cumbersome options and syntax when you can acheive the same using a specialized tool with an elegant GUI? Or maybe even Excel.

Reasons to master basic command line tools are manyfold

  • They are available for most platforms. No need to learn a different set when switching from one operating system to another.
  • Each of them is quite specialized but they can be easily combined to perform relatively complex tasks.
  • Easy to integrate into scheduled tasks

Common characteristics

You’ll read the term Unix various times in this article. It is used as a common term which includes Linux and Mac OS X. The tools mentioned in this article are all published by the Free Software Foundation as part of their GNU project. As they form an important part of any Linux distribution, these systems are often called GNU/Linux systems. A port of the tools for Windows is available from Sourceforge. Just unzip the archive and add the contained directory ‘usr/local/wbin’ to your PATH environment variable.

All of the listed commands provide basic help by calling them with the parameter –help. On Unix systems you can usually get more detailed help by typing man <command>. The headings below also link to the corresponding manual page.

Instead of operating on files, most of these command line tools can also operate on standard input which means they can be used as filters and therefore be easily combined.

The Commands

1. ls – list directory contents

The Unix equivalent to the DOS command dir. It can list directory content in various formats and sort orders. Using the -1 switch it can also create a simple list containing just the file or directory names each on a separate line.

2. less – opposite of more

An improved version of more. Helps to browse through long files which would otherwise scroll out of reach. Allows to scroll up or down through a file as well as to search for text using the ‘/’ command.

3. cut – remove sections from each line of files

The title is misleading, cut allows you to select only certain parts of input lines for output. E.g. if your input lines look like this:

lastname,firstname,address,zip,city

you can print out a list of the contained ZIP codes using cut -d’,’ -f4.

4. sort – sort lines of text files

Does what it says. Assuming you run it on the list of ZIP codes mentioned above, you’d get a list of ZIP codes in alphabetical order.

5. uniq – report or omit repeated lines

Filter the input so that consecutive identical lines are omitted. Again, if you take a sorted list of ZIP codes and run it through uniq, you’ll get a list of unique ZIP codes.

6. head – output the first part of files

Print only the first 5 lines of the input stream: head -n 5.

Print all but the first 5 lines of the input stream: head -n -5.

7. tail – output the last part of files

Basically the opposite of head with a notable extension: the option -f (or –follow) instructs the command to wait for data to be appended to a file and then print it as it is added.

8. find – search for files in a directory hierarchy

find is a very mighty command due to its many options. It can search for files in a directory tree filtering by file names, types, modification times, ownership, permissions and more. See the man page for find for more detailed information.

NOTE: Windows users take care not to confuse the GNU version of find with the version that is installed with Windows. The GNU version is much more flexible.

9. gzip – compress or expand files

by simply running gzip with a file name as the only parameter it compresses a file and replaces it with its gzipped version.

Can also be used to compress or uncompress standard input and thereby acting as a filter.

10. tar – Tape archiver

Tar is not only useful for backups to tape but a very versatile archiving utility.

11. grep – print lines matching a pattern

Filters the input (stdin or a file) for lines matching a given pattern and prints only those. May also be used to do the opposite or simply list files which have matching content.

12. wc – print the number of newlines, words, and bytes in files

What should I say? That’s what it does.

 

Leave a Reply

Required fields are marked *.