20-Feb-2019 • 3 min read
As a software engineer, it is more likely that you will developing software on an unix based system. Hence, it is essential that you get a hold of basic unix commands which will help you interact with unix operating system with ease. In this article we will discuss commands that are very basic and are much essential for the survival in an unix based environment.
The very first command you should be aware of is
cd command, which refers to change directory. This helps in
navigating the file system. This command takes folder path as an argument. Below are some commands that will be helpful.
Note: $ sign represents the shell prompt and text after # is a comment
$ cd ~ # Changes your working directory to the user home $ cd # Changes you working directory to the user home $ cd - # swtiches your working directory to previous directory $ cd .. # Switches to a directory one level before $ cd ../.. # Switches to a directory 2 levels before
Next command is
pwd, which stands for present working directory
$ pwd # Prints to console, the current working directory
Another commands is
mkdir, which stands for make directory
$ mkdir <directoryName> # Creates a directory in the present working directory
ls, which stands for list. This commands lists the contents of the current working directory.
It takes a lot of options which helps in sorting the results.
$ ls # Lists all the directories and files in current directory $ ls -a # Lists hidden files as well $ ls -l # Shows the long format of the list $ ls -lt # Sorts the results based on time $ ls -lr # Sorts the results in reverse lexographical order
If you want to get details of any command you can use the manual pages for that command with below command.
man ls # Brings up manual pages for ls command man pwd # Brings up manual pages for pwd command
Now that you are equipped with commands for working with the file system, let's jump to the search command, which
you will use a lot if you are interacting with your code from terminal. Next up is
# Searches for the given text in all the files in current directory grep <text to search> * # Case-insensitive search for the given search text grep -i <search text> * # Recursively search into all the sub folder files including current directory grep -r <search text> * # searches in a particular directory grep <search text> <directory path>
Let's say you want to quickly view contents of a file on terminal. Below command will help you with that.
cat command, other commands
more are also useful.
tail <file-path> # Prints last 10 lines of a file head <file-path> # Prints first 10 lines of a file
Some other commands which help in viewing the contents of the file in a nice scrollable version are below.
less <file-path> # Shows lesser contents of the file in a scrollable view more <file-path> # Serves the same purpose as less, it is an older version of less
Another utility for editing file contents is VIM. But, you should have to install it based on the flavour of unix you are using. We will try to add an article for the same in future.