What is a command in Linux, and how does it work?

Uncover the essence of Linux OS commands and their functionality. Explore how commands work in the Linux operating system, gaining insights into the fundamental tools that drive efficient and powerful operations.

Jan 19, 2024 - 11:57
Updated: 1 month ago
104.6k
What is a command in Linux, and how does it work?

Quick answer: A Linux command is a text instruction you type into the shell, which finds the program or built-in behind that name, runs it with your options and arguments, and shows the result. A command has the form command, options, arguments, for example ls -l /etc. The shell finds programs by searching the directories in your PATH.

Key takeaways

  • A command is a program or shell built-in that you run by typing its name.
  • The format is command, then options, then arguments.
  • The shell finds programs through the directories listed in PATH.
  • Pipes send one command's output into another; redirection sends it to files.
  • Every command returns an exit status; 0 means success.

What is a Linux command?

A command is an instruction you give to the operating system through the shell, the program that reads what you type. The shell works out what you asked for, runs it and prints the result. Some commands are separate programs, such as ls. Others are built into the shell itself, such as cd.

For more on shells, read what CLI shells are in Linux.

What is the anatomy of a command?

command [options] [arguments]
ls -l /etc
  • Command: what to do.
  • Options (flags): how to do it. Short options start with one dash, long ones with two, as in -l or --all.
  • Arguments: what to do it on, such as a file or directory.

Sample output of ls -l /etc/hostname:

-rw-r--r--. 1 root root 12 Mar 10 09:15 /etc/hostname

The columns are permissions, link count, owner, group, size, modified time and name. Your output will differ.

How does the shell find a command?

When you type a name, the shell checks whether it is an alias or built-in, and if not, searches each directory in the PATH variable in order.

$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ type ls
ls is /usr/bin/ls
$ type cd
cd is a shell builtin

If a command is not found, either it is not installed or its directory is not in PATH. This is why you sometimes run a script as ./script.sh: the current directory is not in PATH.

How do you get help?

  • man ls opens the manual page. Press q to quit. The Linux man-pages project hosts them online.
  • ls --help prints a short summary.
  • apropos keyword searches manual page descriptions.

What are pipes and redirection?

A pipe | sends one command's output into another:

$ ls /etc | wc -l

Redirection saves output into a file:

$ echo "hello" > notes.txt # overwrite
$ echo "again" >> notes.txt # append
$ cat notes.txt
hello
again

Be careful: > overwrites without asking.

What is an exit status?

Every command returns a number when it finishes. Zero means success and anything else means an error.

$ ls /nonexistent
ls: cannot access '/nonexistent': No such file or directory
$ echo $?
2

Scripts use this to decide what to do next.

Which commands should you learn on day one?

CommandWhat it does
pwdShow your current directory
ls -lList files with details
cdChange directory
mkdir / rmdirCreate or remove a directory
cp / mvCopy or move files
rmDelete files (no recycle bin)
cat / lessView file contents
grepSearch text
chmod / chownChange permissions or owner
sudoRun a command with administrator rights

What mistakes do beginners make?

  • Running rm -rf with a wrong path. Check with ls first.
  • Forgetting that Linux is case-sensitive.
  • Using sudo for everything.
  • Not reading error messages. They usually say what is wrong.

What next?

Practise in a virtual machine. Then see how Linux works underneath.

Next steps

Practise commands with an instructor in our Linux course. For interview preparation read Linux command interview questions.

Related reading

Frequently Asked Questions

A command is a text instruction you type into the shell. The shell finds the program or built-in behind the name, runs it with your options and arguments, and displays the result.

Options change how a command behaves, for example -l for a long listing. Arguments tell it what to act on, such as a file or directory. Together they follow the command name.

A built-in, such as cd, is part of the shell itself. An external command, such as ls, is a separate program stored on disk and found through the directories in PATH.

PATH is a variable listing the directories the shell searches, in order, for programs when you type a command name. If a command is not found, check that it is installed and in PATH.

The pipe sends the output of one command into the input of the next, for example ls /etc | wc -l. It lets you combine small commands to do larger jobs.

Use man followed by the command name to open its manual page, or add --help for a short summary. The apropos command searches manual descriptions when you do not know the command name.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0