Unix shell: getting started

Unix Shell

Having a basic knowledge about UNIX shell commands may be useful when you are developing an application because it speeds up development and some common actions that you may need to do every day. We will go through few basic methods that I’m mostly using

Listing all files in a current directory

To list all files placed in the directory that you are in simply type ls in your UNIX shell. Beware of large directories when there are a thousand of files because it may be impossible to find files that you are looking for.

Show file contents

Sometimes you have to display small log or config file in the production environment to quickly check something. You can use cat method so calling cat config.txt will display all contents of config.txt file. Beware of large files as it may kill your shell or force you to scroll for a whole years in order to find the desired line of a file.

Show first lines of a file

You may want to show just a few first lines of given file and then head command comes with help. By the default, this command would display first 10 lines of a given file if you use it without any parameters like this: head config.txt. If you want to specify the count of lines to display, you have to pass n option which takes the number of lines as value. So if you want to display only 3 first lines of a file you would type head -n 3 config.txt

Show last lines of a file

Instead of showing first lines you may want show only a few last lines of given file: it may be super useful in case of log files. In order to do this, you should use tail command. The rules are the same as in case of the head method so if you would not pass any option it will show you 10 last lines of the file and passing n option will force command to show only given number of last lines in the file. If you want to show last 3 lines you would use tail -n 3 config.txt