Linux Essentials
October 6, 2021 in linux

I’ve been working exclusively with .NET, Windows, and PowerShell for the past five years. As I make the transition to the wonderful world of Ruby and Rails, I realize that my Linux skills are virtually non-existent, and to become a good Rails developer, I need to understand Linux.

Last week, I got two classic books on Linux: How Linux Works by Brian Ward, and The Linux Command Line by William E. Shotts. This post tries to summarize some of the basics. Future posts will try to gradually explore the advanced concepts as I make my way through the books.

Useful Commands

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 and -n for printing line numbers next to the results. For example,

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.

Environment Variables

NAME=akshay
export NAME
echo $NAME

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

Processes

A process is a running program. Each process has a process ID (PID).

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

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.

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

Archiving and Compressing Files

gzip file compresses the file. To keep the original file, pass the -k flag.

gunzip uncompresses a .gz file.

However, gzip doesn’t create archives of files, i.e. it doesn’t pack multiple files and dirs into a single file. For that, use the tar command.

tar command creates an archive.

tar cfv archive.tar file1 file2

The c option requires creating the archive, the v option requests the verbose operation, and the f option takes an argument that sets the name of the archive to operate upon.

The following command instructs tar to store all files from the directory /etc into the archive file etc.tar, verbosely listing the files being archived:

tar cfv etc.tar /etc

To unpack a .tar file, use the x flag, which operates tar in extract mode.

tar xvf archive.tar

To unpack a compressed archive, first uncompress the file and then unpack.

gunzip file.tar.gz
tar xvf file.tar

Or, use the shortcut with the z option that does the same, i.e. tar zxvf file.tar.gz.

That’s it for now. In the future posts, we will tackle some of the more advanced concepts in Linux.