What is a CLI?

Usually, most users have seen the Windows command prompt at least once while using their computer systems. Be it for seeing any system errors or just for the sake of exploration. The Windows command prompt is a text-based interface used for interaction with the computer's operating software. This is an example of a command line interface or CLI.

CLI is a text-based interface to interact with an operating system or software by entering commands as text lines. The user usually types CLI-specific commands, followed by arguments or options, and then presses the "Enter" key to execute the command. The operating system or software then processes the command and provides the desired output or performs the requested action.

In this Answer, we will look at the examples of command line interfaces and the commonly used commands.

CLI commands

In this text-based interface, we have several commands which are used to interact with it. These commands are generally used across the majority of text-based interfaces. Let's see some of the commands.

The cd command

cd is used for navigating through the different directories in a system. The implementation is shown below:

cd Courses/dataStructures

Moreover, if we want to go back to a directory we use:

cd ..

While the command that takes the user back to the home directory is by using just cd.

The ls command

ls is used to list all the files and folders in a directory. This helps to visualize whatever the directory stores.

cd Courses
ls
  • In this command, we have some other options which can change the output.

  • -l: Long format listing (detailed information about files and directories)

  • -a: All files (including hidden files and directories)

  • -h: Human-readable sizes (file sizes in KB, MB, or GB)

  • -r: Reverse order (listing in reverse alphabetical order)

  • -t: Sort by modification time (most recently modified first)

  • -R: Recursive listing (contents of subdirectories included)

  • --color: Enable colorized output (highlight files and directories with different colors)

The mkdir command

The mkdir command stands for "make directory," and it creates new directories or folders within the file system. Its implementation is shown below:

mkdir dir1
mkdir Courses/new_dir2

If you want to create nested directories (directories within directories), you can use the -p option:

mkdir -p path/to/nested/directory

The del command

The del command is used to delete any file or folder in the computer system. The rm command is used in Unix systems such as Linux or macOS. This is implemented as shown below:

del file1.txt file2.txt file3.txt

Moreover, we can use /s and /p to change the output.

/s : Deletes specified files from all subdirectories as well

/p : Suppresses the confirmation prompt before deleting each file

del /s /q directory_name

The cp command

The cp command is used for copying information from a source file to a destination file. This is implemented below to clarify any issues.

cp source_file destination

We use the -r or -R option to copy a directory and its contents.

cp -r /path/to/source_directory/ /path/to/destination/

The mv command

The mv command is used to move or rename files and directories in the command-line interface. Implementation of moving a file from one place to another:

mv source destination

While if we want to use this to rename a file, then the command would be:

mv current_name new_name

The touch command

The touch command in CLI is used to create new, empty files in the computer system. This command comes in help for various tasks. Its implementation can be seen below:

touch filename.txt
touch /path/to/directory/filename.txt

Examples of CLI

There are various examples of CLI which are used by various users. Let's discuss the most commonly used one, the Windows command prompt.

Windows command prompt.
Windows command prompt.

The other example we can see is the Ubuntu command line interface.

Ubuntu command line interface.
Ubuntu command line interface.

Just like these, we can find similar CLIs in most computer operating systems, such as the macOS.

Let's take a small quiz about the basic commands we discussed above.

Assessment

Q

Which command is used to create new files?

A)

ls

B)

mv

C)

touch

D)

mkdir

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved