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.
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
-lor--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 lsopens the manual page. Pressqto quit. The Linux man-pages project hosts them online.ls --helpprints a short summary.apropos keywordsearches 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?
| Command | What it does |
|---|---|
pwd | Show your current directory |
ls -l | List files with details |
cd | Change directory |
mkdir / rmdir | Create or remove a directory |
cp / mv | Copy or move files |
rm | Delete files (no recycle bin) |
cat / less | View file contents |
grep | Search text |
chmod / chown | Change permissions or owner |
sudo | Run a command with administrator rights |
What mistakes do beginners make?
- Running
rm -rfwith a wrong path. Check withlsfirst. - Forgetting that Linux is case-sensitive.
- Using
sudofor 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
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0