# Useful Linux Commands

*Published on May 12, 2022*

---


> _**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&nbsp;**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 as I learn more. If you want a detailed overview of Linux operating system, I highly recommend&nbsp;[The Linux Command Line, 2nd Edition](https://learning.oreilly.com/library/view/the-linux-command/9781492071235/)&nbsp;by William E. Shotts.

![linux_command_line.jpeg](https://writesoftwarewell.com/media/5j0nflihpgj11akpg4qhk5eesf4o.jpeg "The Linux Command Line")

### File system

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

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

`grep`&nbsp;searches the term&nbsp;`client`&nbsp;in the provided file.

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

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

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

`file`&nbsp;tells the format of a file.

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

`reset`&nbsp;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&nbsp;`>`, which overwrites the existing content of the file. To append, use&nbsp;`>>`.

```bash
ls > file_name
```

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

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

`sort`&nbsp;sorts lines of text.

`uniq`&nbsp;Report or omit repeated lines

`grep`&nbsp;Print lines matching a pattern

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

`head`&nbsp;Output the first part of a file

`tail`&nbsp;Output the last part of a file

`tee`&nbsp;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 and how to terminate processes that are misbehaving.

`ps`&nbsp;lists all the running processes.

1. `ps x`&nbsp;Show all of your running processes.
2. `ps ax`&nbsp;Show all processes on the system, not just the ones you own.
3. `ps u`&nbsp;Include more detailed information on processes.
4. `ps w`&nbsp;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&nbsp;`ps`&nbsp;command, e.g.&nbsp;`ps u 1234`

`top`&nbsp;displays tasks

`jobs`&nbsp;lists active jobs

`bg`&nbsp;places a job in the background

`fg`&nbsp;places a job in the foreground

`kill`&nbsp;sends a signal to a process

`killall`&nbsp;kills processes by name

`shutdown`&nbsp;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&nbsp;`&`&nbsp;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&nbsp;[nohup](https://linux.die.net/man/1/nohup)&nbsp;command.

### File Modes & Permissions

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

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

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

`rwx`&nbsp;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&nbsp;`chmod`&nbsp;command, e.g.&nbsp;`chmod 644 file`.

<table><tbody><tr><th class="lexxy-content__table-cell--header"><p>Mode</p></th><th class="lexxy-content__table-cell--header"><p>Meaning</p></th><th class="lexxy-content__table-cell--header"><p>Used For</p></th></tr><tr><td><p>644</p></td><td><p>user: r/w; group, other: read</p></td><td><p>files</p></td></tr><tr><td><p>600</p></td><td><p>user: read/write; group, other: none</p></td><td><p>files</p></td></tr><tr><td><p>755</p></td><td><p>user: read/write/execute; group, other: read/execute</p></td><td><p>dirs, programs</p></td></tr><tr><td><p>700</p></td><td><p>user: read/write/execute; group, other: none</p></td><td><p>dirs, programs</p></td></tr><tr><td><p>711</p></td><td><p>user: read/write/execute; group, other: execute</p></td><td><p>dirs</p></td></tr></tbody></table>

### Security

`id`&nbsp;displays user identity

`chmod`&nbsp;changes a file’s mode

`su`&nbsp;runs a shell as another user

`sudo`&nbsp;executes a command as another user

`chown`&nbsp;changes a file’s owner

`chgrp`&nbsp;changes a file’s group ownership

`passwd`&nbsp;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`&nbsp;prints part or all of the environment

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

`export`&nbsp;exports environment to subsequently executed programs

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

**Set environment variable**

```bash
NAME=akshay
export NAME
echo $NAME
```

This sets it locally. For setting it

