Command Line Interface (CLI)

Command Line Interface

Command line interface(CLI) is a text based interface that allows users to interact with a computer program or operating system by typing commands into a terminal or console. In a CLI, users input commands as text strings, and the system responds with text-based output.

Key features of CLI

  1. Text-based : A CLI primarily relies on text input and output. Users enter command as text strings, and the system provides feedback or results in text format.

  2. Command Execution : Users can execute commands by typing them into the CLI and pressing ENTER. The system then processes the command and performs the requested action.

  3. Scripting : CLIs often support scripting, allowing users to create scripts or batch files containing sequences of commands to automate tasks or perform complex operations.

  4. Flexibility : CLIs typically offer a high degree of flexibility and customization. Users can combine commands, use command-line options and arguments, and pipe the output of one command as input to another.

  5. Accessibility : CLIs are often preferred by advanced users, developers, and system administrators due to their efficiency,power and ability to perform tasks quickly and programmatically.

Examples of popular CLI environments include the command prompt on Windows, the terminal on macOS and Linux, and the Command Prompt or PowerShell on Windows. Additionally, many programming langauges provide their own CLI interfaces for executing scripts and interacting with language-specific features and tools.

What is a Shell?

  • The shell is a user program or it is an environment provided for user interaction.

  • It is a command language interpreter that executes commands read from the standard input device such as keyboard or from a file.

  • The shell gets started when you log on or open a console(terminal).

  • The shell is not part of the system kernel but uses the system kernel to execute programs, create files, etc.

  • BASH (Bourne-Again SHell) - The most common shell in Linux. It's open source.

  • CSH (C SHell) - The C Shell's syntax and usage are very similar to the C programming language.

  • KSH (Korn SHell) - Created by David Korn at AT&T Bell Labs. The Korn Shell also was the base for the POSIX Shell Standard specifications.

  • TCSH - It is an enhanced but completely compatible version of the Berkeley UNIX C Shell.

Directory Structure and file hierarchy

If you are coming from Windows, the Linux file system can seem particularly alien. The C:\ drive and drive letters are gone, replaced by a / and cryptic sounding directories, most of which have three letter names. The Filessytem Hierarchy Standard (FHS) defines the structure of file systems on Linux and other UNIX-like operating systems. However, Linux file systems also contain some directories that aren't yet defined by the standard.

/ - The Root directory

Eveything on your linux system is located under the / directory, known as the root directory. You can think of the / directory as being similar to the C:\ directory on Windows - but this isn't strictly true. as the Linux doesn't have drive letters. While another parition would be located at D:\ on Windows, this other partition would appear in another folder under / on Linux.

/bin - Essential user binaries

The /bin directory contains the essential user binaries(programs) that must be present when the System is mounted in single user mode. Applications such as FireFox are stored in /usr/bin, while important system programs and utilities such as the bash shell are located in /bin. The /usr directory may be stored on another partition - placing these files in the /bin directory ensures the system will have these important utilities even if no other file systems are mounted. The /sbin directory is similar - it contains essential system administration binaries.

/etc - Configuration files

The /etc directory contains configuration files, which can generally be edited by hand in a text editor. Note that the /etc/ directory contains system-wide configuration files - user-specific configuration files are located in each user's home directory.

/home - Home Folders

The /home directory contains a home folder for each user. For example, if your user name is bob, you have a home folder located at /home/bob. This home folder contains the user's data files and user-specific configuration files. Each user only has to write access to their own home folder and must obtain elevated permissions (become the root user) to modify other files on the system.

/opt - Optional Packages

The /opt directory contains subdirectories for optional software packages. It’s commonly used by proprietary software that doesn’t obey the standard file system hierarchy – for example, a proprietary program might dump its files in /opt/application when you install it.

/root - Root Home directory

The /root directory is the home directory of the root user. Instead of being located at /home/root, it’s located at /root. This is distinct from /, which is the system root directory.

/sbin - System administration binaries

The /sbin directory is similar to the /bin directory. It contains essential binaries that are generally intended to be run by the root user for system administration.

/tmp - Temporary Files

Applications store temporary files in the /tmp directory. These files are generally deleted whenever your system is restarted and may be deleted at any time by utilities such as tmpwatch.

/usr - User Binaries & Read-Only Data

The /usr directory contains applications and files used by users, as opposed to applications and files used by the system. For example, non-essential applications are located inside the /usr/bin directory instead of the /bin directory and non-essential system administration binaries are located in the /usr/sbin directory instead of the /sbin directory. Libraries for each are located inside the /usr/lib directory. The /usr directory also contains other directories – for example, architecture-independent files like graphics are located in /usr/share.

The /usr/local directory is where locally compiled applications install to by default – this prevents them from mucking up the rest of the system.

/var Variable data files

The /var directory is the writable counterpart to the /usr directory, which must be read-only in normal operation. Log files and everything else that would normally be written to /usr during normal operation are written to the /var directory. For example, you’ll find log files in /var/log.

CLI commands

  • man : The man command in CLI is used to display the manual pages for various commands, functions, and system calls available on UNIX-like OS. It provides detailed information, usage instructions, options, and examples for a specified command or topic.
    man [command]
  • cd : In CLI 'cd' stands for "change directory". It's a command used to navigate between directories in the file system.

The following command will take you to the 'Documents' directory.

    cd Documents

The following command will move up one directory.

    cd ..

The following command can be used to take you to the home directory.

    cd ~

The following command can be used to take you to the previous directory.

    cd -
  • mkdir : This command is used to create new directories.

The following command will create a new directory called "dir".

    mkdir dir
  • chmod : This command is used to change the permissions of files and directories.

    The "chmod" command allows you to set permissions for three different categories of users.

    1. Owner (u) : The user who owns the files or directory.

    2. Group (g) : Users who are members of the files's group.

    3. Others (o) : All other users.

    chmod [permissions] [file/directory]
  • chown : In CLI "chown" is used to change the ownership of files and directories.

    The "chown" command allows you to change the user and/or group ownership of files and directories. You must have appropriate permissions to change the ownership of files and directories.

    chown [options] [owner]:[group] [file/directory]
  • find : In CLI "find" is used to search for files and directories within a specified directory heirarchy.
    find [directory] [options] [expression]