HomeTutorsContact
linux
Linux Introduction
Konrad Kuśmierz
Konrad Kuśmierz
June 21, 2023
7 min

Table Of Contents

01
Unix, Linux, and BSD Directory Structure
02
Important Files
03
Devices
04
Shell
05
Commands
Linux Introduction

Unix, Linux, and BSD Directory Structure

Most Unix, Linux, and BSD systems share a similar directory structure. The table below lists the main directories commonly used across all platforms.

/ Root (top level) directory

/boot Linux kernel and boot loader files

/kernel Unix kernel directories

/usr/bin Core binary programs for normal users

/usr/sbin Administrative programs primarily used by the root user

/opt Optional add-on/third-party software

/etc System configuration files

/home User home directories

/root Home directory of the root user

/lib Shared libraries used by various programs

/mnt Mounted local and remote file systems

/var Variable data (such as system logs) that changes often

/tmp Temporary files used by various programs

/dev Device files

/sys Virtual device driver files used by the kernel

/proc Virtual parameter and informational files used by the kernel

/lost+found Files that have been recovered (after a system failure)

/media

These directories are organized in a manner which separates various components of the operating system in an easy to manage file structure. The /home directory is where users spend most of their time. Files outside of this directory are usually secured so that only the root user can edit or delete them. This protects vital programs and configuration files and prevents users from accidently damaging the core operating system. It also helps secure the system by preventing malicious programs from being able to compromise critical files and settings.

Important Files

In addition to sharing a similar directory structure, most systems will also have the important files listed in following table.

/etc/passwd User account settings

/etc/shadow Encrypted passwords for user accounts

/etc/group Group settings

/etc/fstab Local file system settings

/etc/mtab Mounted file systems

/etc/inittab System startup settings

/etc/exports NFS share settings

/etc/hosts Static name resolution file

/etc/hostname System host name setting

/etc/hosts.allow Network hosts allowed to connect to the system

/etc/resolve.conf DNS settings

/etc/hosts.deny Network hosts not allowed to connect to the system

/etc/issue Message displayed at shell login

/etc/issue.net Remote login message

/etc/motd Message displayed after logging in

/etc/profile Shell environment settings

/etc/shells Valid shells permitted to login

/etc/sudoers Users allowed to use the su command (discussed on page 91)

/var/log/messages Kernel messages log file

/var/log/wtmp Current user login log file

/var/log/lastlog User login history log file

/var/log/kern.log Kernel log file

/var/log/syslog Syslog messages log file

Devices

Every Unix, Linux, and BSD system has a directory that is known as /dev. This directory contains device files (sometime referred to as special files) that represent hardware installed on the system such as /dev/mouse and /dev/cdrom.

ls -l /dev/

Within /dev, there are also a few helpful pseudo-devices. Pseudo-devices do not represent actual hardware and exist only to perform a specific task. The table below describes the most commonly used pseudo-devices.

/dev/zero

File that produces a continuous stream of 0-byte characters

/dev/random

Random number generator

/dev/null

Special file that discards any data written to it

Shell

The command line interpreter, also known as the shell, is a program that accepts input from a user (typically a command of some sort) and performs the requested task. Once that task has completed, the program will terminate and return the user to the shell. The shell’s prompt identifies the type of shell being used. There are two basic types of shell prompts:

$ Normal user shell (may also be % or > on some systems)
# Root user shell

Below is the output of the whoami command (discussed on page 94) which highlights the difference between normal user and root shell prompts on the command line

$ whoami

Normal users typically have a limited amount of access to the system while the root user has unrestricted access. Many systems add customized information to the shell prompt. This information can be a handy indicator of things like the current user’s login name, system host name, and the current directory as shown in the next example.

Commands

export
export USER="Jane Doe"

makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.

grep
$ grep "Mount" mountains.txt

grep stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. It is case sensitive.

grep -i
$ grep -i "Mount" mountains.txt

grep -i enables the command to be case insensitive.

grep -R
$ grep -R Arctic /home/ccuser/workspace/geography

grep -R searches all files in a directory and outputs filenames and lines containing matched results. -R stands for “recursive”.

grep -Rl
$ grep -Rl Arctic /home/ccuser/workspace/geography

grep -Rl searches all files in a directory and outputs only filenames with matched results. -R stands for “recursive” and l stands for “files with matches”.

HOME
$ echo $HOME

The HOME variable is an environment variable that displays the path of the home directory.

ls
$ ls

2014 2015 hardware.txt ls lists all files and directories in the working directory

ls -a
$ ls -a

. .. .preferences action drama comedy genres.xt ls -a lists all contents in the working directory, including hidden files and directories

ls -l
$ ls -l

drwxr-xr-x 5 cc eng 4096 Jun 24 16:51 action drwxr-xr-x 4 cc eng 4096 Jun 24 16:51 comedy drwxr-xr-x 6 cc eng 4096 Jun 24 16:51 drama -rw-r—r— 1 cc eng 0 Jun 24 16:51 genres.txt ls -l lists all contents of a directory in long format. Here’s what each column means.

ls -t ls -t orders files and directories by the time they were last modified.

mkdir
$ mkdir media

mkdir takes in a directory name as an argument, and then creates a new directory in the current working directory. Here we used mkdir to create a new directory named media/.

mv
$ mv superman.txt superhero/

To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument. Here we move superman.txt into superhero/.

nano
$ nano hello.txt

nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.

PATH
$ echo $PATH

/home/ccuser/.gem/ruby/2.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin

PATH is an environment variable that stores a list of directories separated by a colon. Each directory contains scripts for the command line to execute. PATH lists which directories contain scripts.

pwd
$ pwd

/home/ccuser/workspace/blog pwd prints the name of the working directory

rm $ rm waterboy.txt rm deletes files. Here we remove the file waterboy.txt from the file system.

rm -r
$ rm -r comedy

rm -r deletes a directory and all of its child directories.

sed
$ sed 's/snow/rain/' forests.txt

sed stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data.

In the expression ‘s/snow/rain/‘:

s: stands for “substitution”. snow: the search string, the text to find. rain: the replacement string, the text to add in place.

sort
$ sort lakes.txt

sort takes a filename or standard input and orders each line alphabetically, printing it to standard output.

standard error standard error, abbreviated as stderr, is an error message outputted by a failed process.

source
source ~/.bash_profile

source activates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.

standard input standard input, abbreviated as stdin, is information inputted into the terminal through the keyboard or input device.

standard output standard output, abbreviated as stdout, is the information outputted after a process is run.

touch
$ touch data.txt

touch creates a new file inside the working directory. It takes in a file name as an argument, and then creates a new empty file in the current working directory. If the file exists, touch is used to update the modification time of the file

uniq
$ uniq lakes.txt

uniq, short for “unique”, takes a filename or standard input and prints out every line, removing any exact duplicates.

>
$ cat oceans.txt > continents.txt

> takes the standard output of the command on the left and redirects it to the file on the right.

\>>
$ cat glaciers.txt >> rivers.txt

>> takes the standard output of the command on the left and appends (adds) it to the file on the right.

<
$ cat < lakes.txt

< takes the standard input from the file on the right and inputs it into the program on the left.

|
$ cat volcanoes.txt | wc

| is a “pipe”. The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “command to command” redirection.

~/.bash_profile
$ nano ~/.bash_profile

~/.bash_profile is the name of file used to store environment settings. It is commonly called the “bash profile”. When a session starts, it will load the contents of the bash profile before executing commands.

alias alias pd=“pwd” The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.

cd
$ cd Desktop/

cd takes a directory name as an argument, and switches into that directory.

$ cd jan/memory

To navigate directly to a directory, use cd with the directory’s path as an argument. Here, cd jan/memory/ command navigates directly to the jan/memory directory.

cd ..
$ cd ..

To move up one directory, use cd … Here, cd .. navigates up from jan/memory/ to jan/.

cp
$ cp ada_lovelace.txt historical/

cp copies files or directories. Here, we copy the file ada_lovelace.txt and place it in the historical/ directory

Wildcards () $ cp satire/ The wildcard * selects all of the files in the current directory. The above example will copy all of the files in the current directory to the directory called satire. There are other types of wildcards, too, which are beyond the scope of this glossary.

$ cp m.txt scifi/ Here, m.txt selects all files in the working directory starting with “m” and ending with “.txt”, and copies them to scifi/.

env $ env The env command stands for “environment”, and returns a list of the environment variables for the current user.

env | grep VARIABLE $ env | grep PATH env | grep PATH is a command that displays the value of a single environment variable.

man Online manual for command line programs.

whatis Display a description of the specified command.

ls List the contents of a directory.

pwd Display the current/working directory.

cd Change (navigate) directories.

tree Display the contents of a directory in a tree hierarchy format.

find Search for files and directories.

locate Search the locate database for files and directories.

whereis Display the location of binary files, manual pages, and source code for the specified command.

file Display the file type of the specified file.

stat Display extended information about a file system, file, or directory.

date Display or set the system clock.

cal Display a calendar on the command line.

history Display commands that have recently been executed.

clear Clear the contents of the current screen.

logout Logout of the system.

exit Exit the current shell.

Glossary of terms used in this section:

Argument One or more variable input items used by a command line program.

Binary A compiled program or data file.

Flag Synonym for Option.

Man Page Online manual for shell commands.

Option A modifier that alters the default operation of a command.

Parameter Synonym for Argument.

Recursively Includes all subdirectories when executing a command.

Shell Script A grouping of commands in a plain text executable file.

Source Code Uncompiled code for a program.

Switch Synonym for Option.

Working Directory Your current location within the directory tree.

mv Move or rename files and directories.

cp Copy files and directories.

rm Remove files.

mkdir rmdir Create/remove directories.

touch Update time stamps on a file.

lsof List open files.

fuser Display information about open files.

cksum Display the checksum of a file.

md5sum Display the MD5 hash of a file.

ln Create links (shortcuts) to files or directories.

alias Create command line aliases.

gzip gunzip Compress/uncompress files.

split Split large files into multiple pieces.

shred Securely erase files.

watch Periodically execute the specified command.

env Display environment variables.

Glossary of terms used in this section:

Alias A shortcut for a command.

Append Add data to the end of a file instead of overwriting its contents.

Checksum A data integrity verification algorithm.

Compression A process used to reduce the size of files.

Interactive Display confirmation prompts before executing a task.

Link A shortcut to a file or directory.

MD5 Sum An enhanced data integrity verification algorithm.

Parent Directory Higher level directory that contains the current directory.

Pipe A command line facility that connects the output of one command to the input of another.

Redirection Command line facilities used to redirect the input or output of a command.

Variable Adjustable program/environment settings stored in memory.

Verbose Extended output from a command.

Wildcards Symbols used to match text patterns.

strings
Purpose: Extract readable characters from binary files.
cat
Purpose: Concatenate files and display their contents.

Common usage examples: cat [FILE] Display the contents of the specified file cat [FILE1][FILE2] [ETC] Join the specified files cat -n [FILE] Display numbered output for each line cat -s [FILE] Suppress blank lines

find

Usage syntax: find [PATH] [OPTIONS] [CRITERIA]
# find / -name hosts
/etc/avahi/hosts
/etc/hosts
/usr/share/hosts

Tags

Share

Konrad Kuśmierz

Konrad Kuśmierz

Software Engineer

Founder

Expertise

devops
ai

Social Media

instagramtwitterwebsite

Related Posts

Ansible Introduction
Ansible Introduction
June 21, 2023
7 min