Learn Command Line / Cheatsheets.

Learn Command Line / Cheatsheets.

Navigating the File System

pwd Print Working Directory

The command "pwd" stands for "Print Working Directory." When you execute this command in a terminal or command prompt, it displays the absolute path of the current directory you are in.

For example, if you are in the following directory on a Unix/Linux-based system:

/home/user/documents

Running the "pwd" command will output:

/home/user/documents

This shows you the full path of your current location within the file system.

mkdir Make Directory

The command "mkdir" stands for "Make Directory." When you use this command in a terminal or command prompt, it allows you to create a new directory or folder within the current working directory.

The basic syntax for using the "mkdir" command is:

mkdir directory_name

For example, if you want to create a new directory named "example_folder," you would execute the following command:

mkdir example_folder

After running this command, a new directory named "example_folder" will be created in the current working directory. You can also specify a full path to create a directory in a specific location. For instance:

mkdir /home/user/documents/new_directory

This will create a new directory named "new_directory" inside the "documents" folder, which is located in the "/home/user" directory.

ls List

The command "ls" stands for "List" in the context of the terminal or command prompt. When you use the "ls" command, it lists the files and directories present in the current working directory.

The basic syntax for using the "ls" command is:

ls [options] [directory]

If you run the "ls" command without any options or arguments, it will list the files and directories in the current working directory. For example:

ls

This will show you the contents of the current directory.

You can also use various options with the "ls" command to modify its behavior. Some commonly used options include:

  • "-l": Long format listing, showing detailed information about files and directories.

  • "-a": Include hidden files and directories in the listing. Hidden files start with a dot (e.g., ".hidden_file").

  • "-h": Human-readable file sizes. Display file sizes in a more readable format, such as KB, MB, GB, etc.

  • "-r": Reverse order. List files in reverse order.

  • "-t": Sort by modification time. List files by their modification time.

For example, if you want to list all files (including hidden files) in the long format with human-readable file sizes, you can use the following command:

ls -alh

Remember that the exact options and their availability may vary slightly depending on the operating system you are using.

The commands I provided earlier were incorrect for PowerShell on Windows.

In PowerShell, you can use the Get-ChildItem cmdlet with the -Force parameter to list all files and directories, including hidden ones. The -Force parameter displays hidden items, as well as system and read-only items. Here's the correct command:

Get-ChildItem -Force

The commands I mentioned earlier are specific to PowerShell on Windows and won't work in the regular command prompt (CMD).

In the regular command prompt on Windows, there is no direct equivalent to the "-a" option in Unix-like systems. To display hidden files and directories, you need to use a different command.

To list files and directories, including hidden ones, in the command prompt on Windows, you can use the following command:

dir /A

The "/A" switch stands for "all" and includes hidden files and directories in the listing.

Alternatively, you can use the following command to list all files and directories in the current directory, including hidden ones:

dir /A:H

The "/A:H" switch specifically lists hidden files.

cd Change Directory

In the context of computer systems and command-line interfaces, "cd" stands for "Change Directory." It is a command used to navigate between directories (folders) in the file system. When you use the "cd" command, you can change your current working directory to another directory on your computer.

The basic syntax of the "cd" command is as follows:

cd [directory_path]

Here's how it works:

  1. To change to a specific directory, you provide the path to that directory as an argument to the "cd" command.

    For example, if you want to change to a directory called "Documents" located in your home directory, you would type:

     cd ~/Documents
    

    Note: The "~" symbol represents your home directory.

  2. If the directory path contains spaces, you should enclose the path in double quotes.

     cd "/path with spaces/"
    
  3. To move up one level in the directory hierarchy (to the parent directory), use ".." as the argument.

     cd ..
    
  4. To move to the root directory (the top-level directory on Unix-based systems like Linux and macOS), simply type:

     cd /
    
  5. To return to your home directory from anywhere, you can use just the "cd" command without any arguments:

     cd
    

It's important to note that the "cd" command is specific to command-line interfaces and is not typically used in graphical user interfaces (GUIs). In GUIs, you can navigate between directories by using a file manager or explorer window.

Remember that the exact behavior of the "cd" command may vary slightly between different operating systems, so it's a good idea to consult the documentation or help for your specific system if you encounter any issues.

In both the Windows Command Prompt and PowerShell, the "cd" command is used to change the current working directory. However, there are slight differences in behavior and additional features in PowerShell. Let's take a look at how the "cd" command works in each environment:

  1. Windows Command Prompt (cmd.exe):

    In the Windows Command Prompt, the "cd" command works similarly to other command-line interfaces. The basic usage is the same as mentioned earlier:

     cd [directory_path]
    

    Here are some important points to note:

    • To change to a specific directory, you provide the path to that directory as an argument to the "cd" command.

        cd C:\Users\YourUsername\Documents
      
    • You can use ".." to move up one level in the directory hierarchy (to the parent directory).

        cd ..
      
    • To move to a different drive, use the drive letter followed by a colon.

        cd D:
      
    • To change to a directory with spaces in the name, enclose the path in double quotes.

        cd "C:\Path with Spaces\"
      
    • To return to the user's home directory, use the "cd" command without any arguments:

        cd
      
  2. PowerShell:

    PowerShell is a more advanced and feature-rich command-line environment compared to the traditional Windows Command Prompt. In PowerShell, the "cd" command functions similarly to the Command Prompt but provides some additional capabilities.

    • Basic usage remains the same:

        cd [directory_path]
      
    • Similarly, you can use ".." to move up one level in the directory hierarchy (to the parent directory) and use drive letters to switch between drives.

    • PowerShell supports various provider drives, such as "FileSystem," "Registry," "Certificate," "Function," etc. You can use the "cd" command to navigate these provider drives as if they were directories.

    • PowerShell allows you to use the "Tab" key for auto-completion of directory and file names, making navigation more convenient.

    • PowerShell also supports the use of environment variables in paths. For example, you can use the following to navigate to the user's Documents folder:

        cd $env:USERPROFILE\Documents
      
    • To return to the user's home directory, you can use the following:

        cd ~
      
    • PowerShell supports aliases for commands, so "cd" can be replaced with "Set-Location" for the same functionality.

        Set-Location [directory_path]
      

Overall, while the basic functionality of the "cd" command is consistent between Windows Command Prompt and PowerShell, PowerShell provides additional features and flexibility for more advanced usage scenarios.

cp Copy

In the command-line interface, the cp command is used to copy files and directories from one location to another. Here's the basic syntax of the cp command:

cp [options] source destination
  • source: The file or directory you want to copy.

  • destination: The location where you want to copy the source.

Here are some common options you can use with the cp command:

  • -r or -R: Recursively copy directories (useful for copying directories and their contents).

  • -i: Interactive mode. Asks for confirmation before overwriting existing files.

  • -u: Copy only when the source is newer than the destination or when the destination is missing.

  • -v: Verbose mode. Prints the names of the files being copied.

  • --preserve: Preserves file attributes, such as timestamps and ownership.

  • --backup: Creates backup copies of existing files before overwriting them.

Examples:

  1. Copy a file named file.txt from the current directory to /tmp:

     cp file.txt /tmp
    
  2. Copy a directory named source_dir and its contents to a directory named destination_dir:

     cp -r source_dir destination_dir
    
  3. Copy a file named file.txt to /tmp, but prompt for confirmation if a file with the same name already exists in /tmp:

     cp -i file.txt /tmp
    
  4. Copy all files with the .txt extension from the current directory to a directory named txt_files:

     cp *.txt txt_files/
    

Remember to use the appropriate paths for both the source and destination, and consider using options that match your specific copying needs. Always exercise caution when overwriting files and directories, especially when using commands like cp in the command line.

mv Move

In the command-line interface, the mv command is used to move files and directories from one location to another. It can also be used to rename files and directories by moving them within the same location. Here's the basic syntax of the mv command:

mv [options] source destination
  • source: The file or directory you want to move or rename.

  • destination: The location where you want to move the source, or the new name if you're renaming.

Here are some common options you can use with the mv command:

  • -i: Interactive mode. Asks for confirmation before overwriting existing files.

  • -u: Move only when the source is newer than the destination or when the destination is missing.

  • -v: Verbose mode. Prints the names of the files being moved.

  • --backup: Creates backup copies of existing files before overwriting them.

Examples:

  1. Move a file named file.txt from the current directory to /tmp:

     mv file.txt /tmp
    
  2. Move a directory named source_dir and its contents to a directory named destination_dir:

     mv source_dir destination_dir
    
  3. Rename a file named old_name.txt to new_name.txt:

     mv old_name.txt new_name.txt
    
  4. Move all files with the .txt extension from the current directory to a directory named txt_files:

     mv *.txt txt_files/
    
  5. Move a file named file.txt to /tmp, but prompt for confirmation if a file with the same name already exists in /tmp:

     mv -i file.txt /tmp
    

Remember to use the appropriate paths for both the source and destination, and consider using options that match your specific moving or renaming needs. Always exercise caution when overwriting files and directories, especially when using commands like mv in the command line.

rm Remove

In the command-line interface, the rm command is used to remove (delete) files and directories. Be cautious when using this command, as deleted files and directories cannot be easily recovered. Here's the basic syntax of the rm command:

rm [options] file_or_directory
  • file_or_directory: The name of the file or directory you want to remove.

Here are some common options you can use with the rm command:

  • -i: Interactive mode. Asks for confirmation before removing each file.

  • -r or -R: Recursively remove directories and their contents (useful for removing directories).

  • -f: Force removal. This suppresses error messages and does not prompt for confirmation.

  • -v: Verbose mode. Prints the names of the files as they are being removed.

Examples:

  1. Remove a file named file.txt:

     rm file.txt
    
  2. Remove a directory named directory and its contents:

     rm -r directory
    
  3. Remove all files with the .tmp extension in the current directory without asking for confirmation:

     rm -f *.tmp
    
  4. Remove a file named file.txt, but prompt for confirmation before removal:

     rm -i file.txt
    
  5. Remove a directory named directory, its contents, and subdirectories, and prompt for confirmation:

     rm -ri directory
    

Please exercise caution when using the rm command, especially with the -r option, as it can lead to irreversible data loss if used improperly. Double-check your commands before executing them to ensure that you're targeting the correct files and directories for removal.

m