Linux Command Line

These commands are the foundation of working with Linux through the command line. They are useful for navigating the file system, managing files, searching for information, and learning how to use Linux.

2. Creating and Viewing Files

These commands allow you to create files, identify file types, and read their contents.

touch – Create File

  • What it does: Creates an empty file if it doesn’t already exist. If it does exist, it updates its timestamps.

  • When it’s useful: When you need to quickly create a file without opening a text editor.

  • Examples:

touch notes.txt

Creates an empty file named notes.txt.

touch file1.txt file2.txt

Creates two files at once.

file – Identify File Type

  • What it does: Examines a file and identifies its actual file type based on its contents.

  • When it’s useful: When you want to determine what kind of file you’re dealing with, especially if the file extension is misleading.

  • Examples:

file notes.txt

Identifies the type of notes.txt.

file *

Identifies the types of files in the current directory.

  • Cybersecurity application: Useful when examining suspicious files to determine their actual formats rather than trusting their extensions.

cat – Concatenate

  • What it does: Displays file contents directly in the terminal. It can also combine files.

  • When it’s useful: When you want to quickly read a short text file.

  • Examples:

cat notes.txt

Displays everything inside notes.txt.

cat file1.txt file2.txt

Displays the contents of both files.

cat file1.txt file2.txt > combined.txt

Combines the contents of both files into a new file called combined.txt.

less – View File Contents

  • What it does: Opens a file in a viewer that allows you to scroll through its contents without displaying everything at once.

  • When it’s useful: When reading large files, system logs, or configuration files.

  • Example:

less notes.txt

Opens the file in the viewer.

Useful keyboard shortcuts inside less:

  • Space – Move down one page.

  • b – Move up one page.

  • Up Arrow / Down Arrow – Move up or down one line.

  • /word – Search for a specific word.

  • n – Find the next search result.

  • q – Quit the viewer.

  • Cybersecurity application: Extremely useful for reviewing system logs and investigating suspicious activity.

history – Command History

  • What it does: Displays a list of commands you previously entered into the terminal.

  • When it’s useful: When you need to remember a command you used earlier or repeat a previous task.

  • Examples:

history

Displays your command history.

history 10

Displays your last 10 commands.

!25

Repeats command number 25 from your history.

!!

Repeats your most recently entered command.

3. Managing Files and Directories

These commands allow you to copy, move, rename, create, and delete files and directories.

cp – Copy

  • What it does: Copies files or directories.

  • When it’s useful: When you need to duplicate files or create backups.

  • Examples:

cp notes.txt backup.txt

Creates a copy of notes.txt named backup.txt.

cp notes.txt Documents/

Copies notes.txt into the Documents directory.

cp -r folder1 folder2

Copies an entire directory and its contents.

  • Important option:

    • -r – Copies directories recursively, including their contents.

mv – Move

  • What it does: Moves files and directories or renames them.

  • When it’s useful: When organizing files or changing their names.

  • Examples:

mv notes.txt Documents/

Moves notes.txt into Documents.

mv notes.txt study_notes.txt

Renames notes.txt to study_notes.txt.

mv file1.txt file2.txt folder/

Moves both files into the specified folder.

mkdir – Make Directory

  • What it does: Creates a new directory.

  • When it’s useful: When organizing your files into separate folders.

  • Examples:

mkdir Cybersecurity

Creates a directory named Cybersecurity.

mkdir Linux Networking Security

Creates three directories.

mkdir -p Cybersecurity/Linux/Commands

Creates nested directories, including any parent directories that don’t already exist.

rm – Remove

  • What it does: Deletes files and directories.

  • When it’s useful: When you need to remove unnecessary or unwanted files.

  • Examples:

rm notes.txt

Deletes notes.txt.

rm -r folder1

Deletes a directory and its contents.

rm -i notes.txt

Asks for confirmation before deleting the file.

rm -f notes.txt

Forces deletion without prompting.

Important safety warning:

Be extremely careful with rm. Deleted files generally cannot be recovered easily.

Never experiment with commands like rm -rf unless you fully understand exactly what they will delete.

4. Searching for Files

find – Find Files and Directories

  • What it does: Searches for files and directories based on their names, locations, types, and other properties.

  • When it’s useful: When you need to locate a particular file somewhere in your system.

  • Examples:

find . -name notes.txt

Searches the current directory and all subdirectories for notes.txt.

find /home -name "*.txt"

Finds all files ending in .txt inside /home.

find . -type d

Finds all directories in the current directory and its subdirectories.

find . -type f

Finds all regular files.

find . -name "*.log"

Finds all files ending in .log.

Important options:

  • -name – Searches by filename.

  • -type f – Searches for regular files.

  • -type d – Searches for directories.

  • -iname – Searches by filename without distinguishing uppercase and lowercase.

  • -size – Searches based on file size.

Cybersecurity application: Useful when searching for suspicious files, locating log files, and investigating a Linux system.

5. Getting Help and Learning Commands

These commands help you understand how Linux commands work.

help – Built-in Command Help

  • What it does: Displays information about commands built into the Bash shell.

  • When it’s useful: When you need a quick explanation of a Bash command.

  • Examples:

help

Displays a list of Bash built-in commands.

help cd

Displays information about the cd command.

help history

Displays information about the history command.

Important: help primarily works with Bash built-in commands, not every Linux command.

man – Manual

  • What it does: Opens the manual page for a command.

  • When it’s useful: When you need detailed information about a command, its options, and how to use it.

  • Examples:

man ls

Opens the manual for ls.

man cp

Opens the manual for cp.

man find

Opens the manual for find.

Useful keyboard shortcuts:

  • Space – Next page.

  • b – Previous page.

  • /word – Search for a word.

  • q – Exit.

whatis – Command Description

  • What it does: Provides a short, one-line description of a command.

  • When it’s useful: When you want to quickly understand what a command does without reading its entire manual.

  • Examples:

whatis ls

Provides a short description of ls.

whatis mkdir

Provides a short description of mkdir.

whatis cp

Provides a short description of cp.

Difference between help, man, and whatis:

  • help – Quick information about Bash built-in commands.

  • man – Detailed documentation about commands.

  • whatis – One-line command description.

6. Customizing the Terminal

alias – Create Command Shortcuts

  • What it does: Creates a shortcut that represents another command.

  • When it’s useful: When you frequently use a long command and want to make it easier to type.

  • Examples:

alias ll='ls -la'

Creates a shortcut called ll that runs ls -la.

ll

Runs the shortcut you just created.

alias

Displays all currently defined aliases.

unalias ll

Removes the ll shortcut.

Important: Aliases normally only last for your current shell session. To make one permanent, you can add it to your ~/.bashrc file.

7. Exiting the Terminal

exit – Exit Shell

  • What it does: Closes your current shell session.

  • When it’s useful: When you’re finished working in the terminal or want to close a remote SSH session.

  • Examples:

exit

Closes your current shell.

exit 0

Exits with status code 0, which conventionally means success.

exit 1

Exits with status code 1, which conventionally indicates an error.

8. Quick Reference Cheat Sheet

Command

Main Purpose

pwd

Display current directory

cd

Change directory

ls

List files and directories

touch

Create an empty file

file

Identify file type

cat

Display file contents

less

Scroll through file contents

history | View previous commands

cp

Copy files and directories

mv

Move or rename files

mkdir

Create directories

rm

Delete files and directories

find

Search for files and directories

help

Get Bash built-in help

man

Open command manuals

whatis

Get a short command description

alias

Create command shortcuts

exit

Exit the shell