The Life‑Changing Magic of Ruby and Rails

Useful Linux Commands

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 as I learn more. If you want a detailed overview of Linux operating system, I highly recommend The Linux Command Line, 2nd Edition by William E. Shotts.

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.
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 >>.

ls > file_name

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

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 and how to terminate processes that are misbehaving.

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 command.

File Modes & Permissions

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

-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.

ModeMeaningUsed For
644user: r/w; group, other: readfiles
600user: read/write; group, other: nonefiles
755user: read/write/execute; group, other: read/executedirs, programs
700user: read/write/execute; group, other: nonedirs, programs
711user: read/write/execute; group, other: executedirs

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

NAME=akshay
export NAME
echo $NAME

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

Subscribe to Akshay's Blog

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe