Useful Linux Commands
Tags: linux, terminal
Published: 2022-05-13 23:57:20

> **Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice.**
> 
> _The Linux Command Line, William E. Shotts_

Since I switched to Ruby on Rails from .NET, I find myself increasingly using the terminal every day. One thing I've realized as a Rails developer is that you need to have some basic competence with the terminal, as **you will use it all the time**. So I spent some time getting familiar with some basic Linux commands, and this post tries to summarize the essentials. 

This is not a comprehensive list, but I will try to keep adding to this list on the [original blog post](https://akshaykhot.com/linux-useful-commands/) as I learn more.  If you want a detailed overview of Linux operating system, I highly recommend [The Linux Command Line, 2nd Edition](https://learning.oreilly.com/library/view/the-linux-command/9781492071235/) by William E. Shotts. 

![The Linux Command Line, 2nd Edition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaon62ip2jofgiz4mnd7.jpeg)

So here are some of the commands that you might find useful as a developer on a Linux/Mac machine. 

### File system

`cat` displays the contents of a file, or concatenates the contents of multiple files. 

`touch` creates a file if it doesn't exist; updates the timestamp if it exists. 

`grep` searches the term `client` in the provided file. 

- `-i` for case-insensitive search
-  `-n` for printing line numbers next to the results. 

```shell
grep client -in /etc/ssh/sshd_config
```

`less` displays a large file one page at a time. Use the space bar to go forward and `b` to go back. 

`file` tells the format of a file. 

`head/tail` displays the top or bottom of the file. Pass `-n` for number of lines, `head -5 file`

`reset` re-initializes terminal. Especially useful after resizing the window or if a command results in scrambled window. 

### I/O Redirection

To send the output of a command to a file, use `>`, which overwrites the existing content of the file. To append, use `>>`.

```bash
ls > file_name
```

To send the output of a command to the standard input of another command, use the pipe `|` character.

```shell
head /proc/cpuinfo | tr a-z
```

`sort` sorts lines of text.

`uniq` Report or omit repeated lines

`grep` Print lines matching a pattern

`wc` Print newline, word, and byte counts for each file

`head` Output the first part of a file

`tail` Output the last part of a file

`tee` Read from standard input and write to standard output and files

### Processes

A process is a running program. Each process has a process ID (PID). Sometimes a computer will become sluggish or an application will stop responding. 

Here are some of the tools available at the command line that let us examine what programs are doing.

`ps` lists all the running processes. 

1. `ps x` Show all of your running processes.
2. `ps ax` Show all processes on the system, not just the ones you own.
3. `ps u` Include more detailed information on processes.
4. `ps w` Show full command names, not just what fits on one line.

To check on a specific process, add the PID at the end of the `ps` command, e.g. `ps u 1234`

`top` displays tasks

`jobs` lists active jobs

`bg` places a job in the background

`fg` places a job in the foreground

`kill` sends a signal to a process

`killall` kills processes by name

`shutdown` shuts down or reboots the system

**Background Process**

Normally, after you run a command, you don't get the prompt back until the process finishes. You can detach a process from the shell with the `&` which runs it as a background process. The shell returns immediately with the PID of the process. 

If you want to keep a program running when you log out from a remote machine, use the [nohup](https://linux.die.net/man/1/nohup) command. 

### File Modes & Permissions

Determine if a user can read, write, or run the file. View the permissions using `ls -l` command. 

```shell
-rw-rw-r-- 1 ak ak  14 Oct  5 07:00 file_one
```

Leftmost character:  `-` indicates a file, `d` indicates a directory. 

`rwx` stands for read, write, and execute. From left to right, the permissions stand for a user, group, and other. 

To modify the permissions, use the `chmod` command, e.g. `chmod 644 file`.

| Mode | Meaning                                              | Used For       |
| ---- | ---------------------------------------------------- | -------------- |
| 644  | user: r/w; group, other: read                        | files          |
| 600  | user: read/write; group, other: none                 | files          |
| 755  | user: read/write/execute; group, other: read/execute | dirs, programs |
| 700  | user: read/write/execute; group, other: none         | dirs, programs |
| 711  | user: read/write/execute; group, other: execute      | dirs           |

### Security

`id` displays user identity

`chmod` changes a file’s mode

`su` runs a shell as another user

`sudo` executes a command as another user

`chown` changes a file’s owner

`chgrp` changes a file’s group ownership

`passwd` changes a user’s password


### Environment Variables

The Linux shell maintains information about the current environment. Programs use this data to change their runtime behavior, e.g. selecting different database when the application is running in the production environment, as opposed to the test environment. 

`printenv` prints part or all of the environment

`set` sets shell options (show the environment when used without argument)

`export` exports environment to subsequently executed programs

`alias` creates an alias for a command (or show all aliases when used without argument)

**Set environment variable**

```shell
NAME=akshay
export NAME
echo $NAME
```

This sets it locally. For setting it globally, add it in the `~/.bashrc` or `~/.zshconfig` file. 

Hope that helps.