Command Line Basics

Different *nixes

There are many different *nix versions and cousins. The flavors include *BSD, Linux, Unix, and others. Each version and implementation has some unique features to their shells. Some popular versions and distributions are:

  • Red Hat/CentOS

  • Fedora

  • Ubuntu

  • SUSE

  • Debian

  • Solaris

  • FreeBSD

  • MacOS/OS X

  • AIX

  • OpenBSD

File structure

  • File locations differ between distributions

  • Common directories

    • / (Root of the file system)

    • /root (root user’s home directory)

      • This is not the root(/) of the file system

    • /home/$USER (Your files)

    • /etc (System configuration files)

    • /media (Mount points for removable systems, primarily linux flavors)

    • /opt (Typically commercial software packages installed in one place)

    • /var (Variable files that change regularly)

    • /var/log (Common default location for log files)

Command Line Interface (CLI) Package Installers

yum install <package>

apt-get install <package>

  • yum/dnf

    • Red Hat

    • CentOS

    • Fedora

    • SUSE

  • apt-get

    • Debian

    • Ubuntu

(yum is in the process of being replaced by dnf)

Basic Navigation

  • CasE SensItiVity MatTERs

  • tab completion to complete command names, paths, or files

  • pwd (Print working[current] directory)

  • ls (list files in directory)

  • cd (Change Directory)

    • cd or cd ~ changes from the current directory to the user’s home directory

    • cd /path/to/directory changes from the current directory to the defined directory

    • cd .. changes from the current directory to the directory one level up

    • cd ../../path/to/directory changes from the current directory to two levels up then down to the new directory

    • cd - changes from the current directory to the previous directory

This is not navigation, but you need to know this

  • ^n reads as ctrl-n

Command Line Switches

Commands are often followed by one or more switches at the command line. When one - is used, each character following it is an option. When two '--' are used, the full string is considered the option.

ls -help attempts to run the ls command with the h,l,p options (e is invalid)

ls --help prints the ls help file to the screen

In some fonts, a -- notation will appear as a single - when rendered.


The following is the output of ls -lah (long list,all files,human readable)

total 40
drwxr-xr-x   6 chuck  staff   204B May 25  2016 .
drwxr-xr-x  16 chuck  staff   544B Oct 24 21:25 ..
drwxr-xr-x   5 chuck  staff   170B May 25  2016 fixtures
-rw-r--r--   1 chuck  staff   6.0K May 25  2016 .coffee
-rw-r--r--   1 chuck  staff   2.8K May 25  2016
-rw-r--r--   1 chuck  staff   6.0K May 25  2016
-rw-r--r--   1 chuck  staff   7.5K May 25  2016

----------   - -----  -----   ---- ------------ ----------------------------
    |        |   |      |       |       |             |
    |        |   |      |       |       |             +    File Name
    |        |   |      |       |       |
    |        |   |      |       |       +-----------  Modification Time
    |        |   |      |       |
    |        |   |      |       +-------------------   Size (in bytes)
    |        |   |      |
    |        |   |      +---------------------------        Group
    |        |   |
    |        |   +----------------------------------        Owner
    |        |
    |        +--------------------------------------        Links
    +-----------------------------------------------   File Permissions

(Diagram from



Field Definition


File Type

  • - file

  • d directory

  • l symlink


User/Owner Permissions


Group Permissions


World/Other User’s permissions


Character Effect Numerical Value










chmod changes the permissions of the file or directory

Command Permissions

chmod 644 filename


chmod 776 filename


chmod 654 dirname


Useful Commands

Command Function
  • vi/vim

  • emacs

  • pico/nano

For editing files


for copying files


for moving/renaming files


for removing/deleting files or directories


for finding files


for finding stuff in files


for viewing end/beginning of files


for starting/stoping/controlling services


show the contents of a file at the cli


for learning how to use commands


for modifying file permissions


for compressing and decompressing files


for running a command as a different user, typically root

vi/vim commands

vim filename - opens the designated file in normal mode

:help - displays the vim help file

/texttosearch - search for text in the document (case sensitive)

i - insert mode to edit the file

esc - return to command mode

:wq - write/save the file and quit the editor

:q! - quit the editor without saving the changes

cp command

cp filename /path/to/copy/to

cp filename newfilename

copies the file from the current location/name to the new location/name

cp -R /path/to/directory /path/to/new/directory

copies the the files and directories from the specified directrory to the new location

mv command

mv filename /path/to/new/location

mv filename newfilename

moves the file from the current location/name to the new location/name.

mv also acts as the rename command.

rm command

rm filename

removes(deletes) the indicated file

rm -rf directoryname

removes(deletes) the indicated directory and all of its contents, including hidden files

This is destructive. It cannot be undone.

find command

find . -name 'filename.txt'

Searches for the filename.txt file in the current directory and sub directories

Change . in the above command to a path to start at that path and search its sub directories

Other popular factors to search on include owner, time, type, size, file types

grep command

grep -iR pattern Documents/

Searches files for the phrase pattern in a case insensitive(i) manner in and below(R) the local Documents directory.

head/tail commands

head -n 15 filename.txt

head displays the first lines of a file (10 by default, 15 in the above example)

tail -n 15 filename.txt

tail displays the last lines of a file (10 by default, 15 in the above example)

tail -f filename.txt

The above use of the tail command with the -f flag continually rereads and displays the end of the file. This is useful when monitoring a log file in real time, for example. Use <ctrl>-c to stop reading the file.

service/systemctl commands

The service and systemctl commands control the status of services on the system. The following examples are for controlling the Apache web server status.

systemctl {start,stop,restart,status} httpd

service httpd {start,stop,restart,status}

less command

less filename

Displays the contents of a file in the terminal window.

Use the up and down arrows to navigate the file.

Use the v key to edit the file in the system’s default editor. When you quit the editor, you return to the less process.

Use a forward slash followed by text to search for to find particular types.


tar command

tar zxvf filename.tar.gz

Extracts the contents of the tar.gz file to the current directory

tar zcvf newcompressedfile.tar.gz file1 file2 file3

Creates a new compressed file containing all the files indicated in the command

tar zcvf newcompressedfile.tar.gz /path/to/files

Creates a new compressed file containing all the files in the indicated directory

sudo command

sudo ls /var/log/messages

By default, runs a command as the root user. Adding -u <user> will specify a different user to run the command as.

Pipe commands from one to another on a single line

The pipe | symbol passes the output of a command to another command.

The following command will list the contents of the current directory in a long format. The grep command then filters and displays only the lines of text that contain the word filename in a case insensitive manner.

ls -l|grep -i filename

-rw-rw-r--. 1 chuck chuck          0 May 28 20:01 FILENAME.ADOC
-rw-rw-r--. 1 chuck chuck          0 May 28 20:00 filename.csv
-rw-rw-r--. 1 chuck chuck          0 May 28 20:01 FileName.jpg
-rw-rw-r--. 1 chuck chuck          0 May 28 20:00 filename.txt

The following command will run the ps -ef command and show the results that match the phrase ssh on the screen

ps -ef|grep ssh

man command

man <command>

The man command displays the manual (help) page for the command indicated.

Perhaps the most useful and under-utilized command in this presentation.

Other interesting commands and concepts to follow up on

These are commands and concepts that are useful to know but may fall into niche categories. Many of these will likely be useful at some point to this audience.

  • vimdiff - for showing the differences between two text files

  • netstat/ss - for showing open ports

  • firewall-cmd - for configuring firewall rules

  • nmtui - for configuring network manager from the command line

  • git - version control for files

  • sed - Stream line editor to change file contents without opening a full editor

  • md5sum sha1sum (and others) creates a unique hash of a file to easily compare two or more files

  • ssh creates a secure shell connection between two computers

  • scp securely copies files between computers

  • !! adds the last command to the current command line

  • selinux - kernel security model that has been known to interfere with programs running correctly

  • environmental variables - easily share configuration settings between applications and processes such as $USER, $HOME, $EDITOR, $BROWSER

  • regex - Regular Expressions are your friend (once you make friends with them)

Dangerous commands

These commands should not be used unless you really understand what you’re doing with them.

rm -rf / - Will delete the entire file system

mv file /dev/null will move the file to a system device that will delete the file

:(){:|:&};: - a fork bomb which creates a function and executes it until the system freezes

$COMMAND > /dev/sda - overwrites data on the block device, in this case the main drive

mkfs.ext4 /dev/sda - formats the block device using the ext4 filesystem, in this case the main drive. mkfs.* may be a variety of filesystem types.

This list is far from comprehensive. It is provided to show that simple and innocent looking things can be very harmful. Pay attention and understand the commands you copy and paste from the internet. For more, search for dangerous linux commands in your favorite search engine

Bash system files

Bash is probably the most commonly used shell on *nix systems. Similar files will exist for other shells.

.bashrc - configuration file for non-login shells

.bash_profile - configuration for login shells

.bash_history - history of the recent commands run in the bash shell


log files are your friend

Common Location — /var/log

Read recent system messages — tail /var/log/messages

Print Kernel messages — dmesg


Links to this presentation

html github

pres html