DEV Community

Akshay Khot
Akshay Khot

Posted on • Originally published at akshaykhot.com

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 on the original blog post 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.

The Linux Command Line, 2nd Edition

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.
grep client -in /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

head /proc/cpuinfo | tr a-z
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode

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

NAME=akshay
export NAME
echo $NAME
Enter fullscreen mode Exit fullscreen mode

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

Hope that helps.

Top comments (7)

Collapse
 
software_writer profile image
Akshay Khot

Thanks, Jeremy. Yes, tree is very helpful.

Btw, How did you add that code snippet with a dropdown arrow, to be expanded? Is that only for the comments, or can I do that in a post? Thanks!

Collapse
 
guithomas profile image
Guilherme Thomas • Edited

I've be using more the terminal too, one command I dig it its the double exclamation mark. Tried to do something and forgot sudo? Instead of arrow up, home, just type sudo !!

Collapse
 
software_writer profile image
Akshay Khot

Wow, that's handy. Thanks for sharing!

 
software_writer profile image
Akshay Khot

Whoa, that's super useful. Thanks for sharing!

Collapse
 
mhmxs profile image
Richard Kovacs

I use lsof a lot.