Skip to content

About the Unix Command Line#

Introduction to the Unix Command Line#

The Unix command line is a powerful tool that allows you to interact with your computer's operating system through text-based commands. It is widely used in various platforms, including Linux and macOS. Mastering the command line can greatly enhance your productivity and enable you to perform various tasks efficiently.

In this tutorial, we will cover the basics of the Unix command line, including navigating the file system, manipulating files and directories, and some useful commands.

Getting Started#

Opening the Terminal#

To access the Unix command line, you need to open the terminal or shell application on your computer. On Linux systems, you can use the built-in terminal emulator. On macOS, you can use the Terminal application, which can be found in the Utilities folder within the Applications folder.

Basic Commands#

Let's start with some fundamental commands to get you acquainted with the Unix command line:

  1. pwd (Print Working Directory): Displays the current directory (folder) you are in.

    pwd
    /Users/username/documents
    

  2. ls (List): Lists the files and directories in the current directory.

    ls
    file1.txt  file2.txt  folder1  folder2
    

  3. cd (Change Directory): Allows you to navigate to a different directory.

    cd folder1
    

  4. mkdir (Make Directory): Creates a new directory.

    mkdir new_folder
    

  5. touch: Creates an empty file.

    touch new_file.txt
    

  6. rm (Remove): Deletes files or directories.

    rm file1.txt
    rm -r folder1
    

  7. mv (Move): Moves or renames files and directories.

    mv file2.txt folder2/
    mv file2.txt new_filename.txt
    

  8. cp (Copy): Copies files or directories.

    cp file1.txt folder2/
    

The Unix file system is organized in a hierarchical structure. Each directory can contain files and other directories. Here are some additional commands to help you navigate the file system:

  1. cd (Change Directory): As mentioned earlier, cd allows you to change your current directory. You can use absolute or relative paths.

    cd /path/to/directory  # Absolute path
    cd ../parent_directory  # Relative path (move up one level)
    cd folder3/subfolder   # Relative path (move down into subfolder)
    

  2. ls (List): You can use various options with ls to customize the output:

    ls -l  # Long listing format with detailed information
    ls -a  # List all files, including hidden ones (those starting with .)
    ls -h  # Human-readable file sizes
    ls *.txt  # List all files ending with .txt
    

  3. pwd (Print Working Directory): Useful to check where you are in the file system.

  4. .. and .: .. refers to the parent directory, while . refers to the current directory. They can be handy when constructing paths.

File Manipulation#

Now let's explore some additional commands to manipulate files:

  1. cat (Concatenate): Displays the content of a file.

    cat file1.txt
    

  2. less and more: Allow you to view the content of large files interactively.

    less large_file.txt
    more another_large_file.txt
    
    Use the arrow keys to scroll, and press q to exit.

  3. head and tail: Display the beginning or end of a file.

    head file1.txt  # Show the first few lines
    tail file1.txt  # Show the last few lines
    

  4. grep: Searches for a pattern in a file.

    grep "keyword" file1.txt
    

  5. wc (Word Count): Counts the number of lines, words, and characters in a file.

    wc file1.txt