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. 1. Navigation Commands ---------------------- These commands help you move around the Linux file system and understand where you are. pwd – Print Working Directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * **What it does:** Displays the full path of the directory you are currently in. * **When it's useful:** When you aren't sure what directory you are currently working in. * **Example:** .. code-block:: bash pwd * **Example output:** .. code-block:: text /home/greyson cd – Change Directory ~~~~~~~~~~~~~~~~~~~~ * **What it does:** Moves you from one directory to another. * **When it's useful:** When you need to access files or folders in different locations. * **Examples:** .. code-block:: bash cd Documents Moves into the Documents directory. .. code-block:: bash cd .. Moves up one directory. .. code-block:: bash cd ~ Takes you to your home directory. .. code-block:: bash cd / Takes you to the root directory. .. code-block:: bash cd - Returns to the previous directory. ls – List ~~~~~~~~~ * **What it does:** Displays files and directories inside your current directory. * **When it's useful:** When you want to see what files and folders are available. * **Examples:** .. code-block:: bash ls Lists files and directories. .. code-block:: bash ls -l Displays detailed information, including permissions, ownership, size, and modification date. .. code-block:: bash ls -a Displays all files, including hidden files. .. code-block:: bash ls -lh Displays detailed information with file sizes in an easier-to-read format. .. code-block:: bash ls -la Combines detailed information with hidden files. 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:** .. code-block:: bash touch notes.txt Creates an empty file named ``notes.txt``. .. code-block:: bash 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:** .. code-block:: bash file notes.txt Identifies the type of ``notes.txt``. .. code-block:: bash 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:** .. code-block:: bash cat notes.txt Displays everything inside ``notes.txt``. .. code-block:: bash cat file1.txt file2.txt Displays the contents of both files. .. code-block:: bash 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:** .. code-block:: bash 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:** .. code-block:: bash history Displays your command history. .. code-block:: bash history 10 Displays your last 10 commands. .. code-block:: bash !25 Repeats command number 25 from your history. .. code-block:: bash !! 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:** .. code-block:: bash cp notes.txt backup.txt Creates a copy of ``notes.txt`` named ``backup.txt``. .. code-block:: bash cp notes.txt Documents/ Copies ``notes.txt`` into the Documents directory. .. code-block:: bash 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:** .. code-block:: bash mv notes.txt Documents/ Moves ``notes.txt`` into Documents. .. code-block:: bash mv notes.txt study_notes.txt Renames ``notes.txt`` to ``study_notes.txt``. .. code-block:: bash 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:** .. code-block:: bash mkdir Cybersecurity Creates a directory named ``Cybersecurity``. .. code-block:: bash mkdir Linux Networking Security Creates three directories. .. code-block:: bash 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:** .. code-block:: bash rm notes.txt Deletes ``notes.txt``. .. code-block:: bash rm -r folder1 Deletes a directory and its contents. .. code-block:: bash rm -i notes.txt Asks for confirmation before deleting the file. .. code-block:: bash 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:** .. code-block:: bash find . -name notes.txt Searches the current directory and all subdirectories for ``notes.txt``. .. code-block:: bash find /home -name "*.txt" Finds all files ending in ``.txt`` inside ``/home``. .. code-block:: bash find . -type d Finds all directories in the current directory and its subdirectories. .. code-block:: bash find . -type f Finds all regular files. .. code-block:: bash 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:** .. code-block:: bash help Displays a list of Bash built-in commands. .. code-block:: bash help cd Displays information about the ``cd`` command. .. code-block:: bash 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:** .. code-block:: bash man ls Opens the manual for ``ls``. .. code-block:: bash man cp Opens the manual for ``cp``. .. code-block:: bash 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:** .. code-block:: bash whatis ls Provides a short description of ``ls``. .. code-block:: bash whatis mkdir Provides a short description of ``mkdir``. .. code-block:: bash 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:** .. code-block:: bash alias ll='ls -la' Creates a shortcut called ``ll`` that runs ``ls -la``. .. code-block:: bash ll Runs the shortcut you just created. .. code-block:: bash alias Displays all currently defined aliases. .. code-block:: bash 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:** .. code-block:: bash exit Closes your current shell. .. code-block:: bash exit 0 Exits with status code 0, which conventionally means success. .. code-block:: bash 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 | +-----------+--------------------------------------+ 9. Recommended Practice ----------------------- Practice these commands in your Linux terminal rather than just memorizing them. Exercise 1: Navigate Your File System ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Display your current directory using ``pwd``. 2. List all files, including hidden files, using ``ls -a``. 3. Navigate into your Documents directory using ``cd``. 4. Return to your home directory using ``cd ~``. Exercise 2: Create and Manage Files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Create a directory called ``Cybersecurity``. 2. Enter the Cybersecurity directory. 3. Create a file called ``notes.txt``. 4. Display the contents of ``notes.txt`` using ``cat``. 5. Create a copy called ``backup.txt``. 6. Rename ``backup.txt`` to ``old_notes.txt``. 7. List all files to verify your work. **Learning recommendation:** Get comfortable navigating Linux, managing files, and reading system information before moving into networking and security tools. These commands will become part of almost everything you do later, including penetration testing, system administration, and incident investigation.