The Linux kernel has found its way into an incredible number of diverse systems. It can now be found in everything from automobiles to rockets, watches to televisions, and netbooks to the fastest supercomputers. Linux only accounts for a relatively small percentage of the operating systems found on desktop computers, but has gained widespread use in servers, Internet of Things (IoT) devices, networking equipment, smartphones, and many other devices you may not think of as even being computers.
Whether you’re studying for your next certification or an experienced IT professional, learning a handful of Linux commands is a great way to enhance your skillset. The CompTIA A+ certification covers 19 Linux commands that are the perfect place to start.
To execute a command, the first step is to type the name of the command followed by any options and/or arguments before pressing the Enter key.
command [options…] [arguments…]
In other words, you type a command, followed by any options and/or arguments before pressing the Enter key. Typically options alter the behavior of the command and arguments are items or values for the command to act upon. Although there are some commands in Linux that aren’t entirely consistent with this syntax, most commands use this syntax or something similar.
Note: In the command formats below the items contained within brackets should be replaced with the appropriate option or argument.
In order to discover where you are currently located within the filesystem, the pwd command can be used.
pwd [OPTIONS]
The pwd command prints the working directory, your current location within the filesystem:
sysadmin@localhost:~$ pwd /home/sysadmin
To navigate the filesystem structure, use the cd (change directory) command to change directories.
cd [OPTIONS] [PATH]
To move to the Documents directory, use it as argument to the cd command:
sysadmin@localhost:~$ cd Documents sysadmin@localhost:~/Documents$
The ls command is used to list the contents of a directory.
ls [OPTIONS] [DIRECTORY]
In the next example, the Documents directory will be used as an argument. The resulting output is a list of files contained with the Documents directory:
sysadmin@localhost:~$ ls Documents School alpha-second.txt food.txt linux.txt os.csv Work alpha-third.txt hello.sh longfile.txt people.csv adjectives.txt alpha.txt hidden.txt newhome.txt profile.txt alpha-first.txt animals.txt letters.txt numbers.txt red.txt
Some useful options to the ls command are shown below:
Command | Function |
ls -l
|
Use long listing format. |
ls -t
|
Sort files by timestamp. |
ls -S
|
Sort files by file size. |
ls -r
|
Reverse the order of any sort. |
It can be inconvenient to type the full path argument so some of the more common paths have shortcuts.
Path Argument | Directory Represented |
.
|
Current Directory |
..
|
Home Directory |
/
|
Root directory |
~
|
Home directory |
The su command allows you to temporarily act as a different user. By default, if a user account is not specified, the su command will provide administrative privileges.
su OPTIONS USERNAME
Utilizing the login shell option is recommended, as the login shell fully configures the new shell with the settings of the new user. This option can be specified one of three ways:
su - su -l su --login
The example below shows this being executed:
sysadmin@localhost:~$ su - Password: root@localhost:~#
The sudo command allows a user to execute a command as another user.
sudo [OPTIONS] COMMAND
Like the su command, the sudo command assumes by default the root user account should be used to execute commands. To specify another user, the -u option can be used.
Permissions and Ownership
Permissions determine the ways different users can interact with a file or directory. Reference a long listing ls -l to view the permissions and ownership information.
sysadmin@localhost:~/Documents$ ls -l hello.sh -rw-rw-r-- 1 sysadmin sysadmin 21 Aug 1 02:35 hello.sh
The fields in the output provide information about the permissions of the following groups. Read below to learn more about the characters which represent permissions.
Owner
-rw-rw-r-- 1 sysadmin sysadmin 21 Aug 1 02:35 hello.sh
Group
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
Others
drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt
The chmod command is used to change the permissions of a file or directory. Administrative access is required unless you are the user who owns the file.
chmod [<SET><ACTION><PERMISSIONS>]... FILE
To use the symbolic method of chmod first indicate which set of permissions is being changed:
chmod [<SET><ACTION><PERMISSIONS>]... FILE
Set Symbol | Meaning |
u
|
User |
g
|
Group. |
o
|
Others |
a
|
All |
Next, specify an action symbol:
chmod [<SET><ACTION><PERMISSIONS>]... FILE
Action Symbol | Meaning |
+
|
Add |
=
|
Exact |
-
|
Remove |
After an action symbol, specify one or more permissions to be acted upon:
chmod [<SET><ACTION><PERMISSIONS>]... FILE
Permission Symbol | Meaning |
r
|
Read |
w
|
Write |
x
|
Execute |
To give the user owner of the file execute permission, execute the command below.
sysadmin@localhost:~/Documents$ chmod u+x hello.sh
Initially, the owner of a file is the user who creates it. To change the ownership of a file use the chown command.
chown [OPTIONS] [OWNER] FILE
The chown command is used to change the ownership of files and directories. Changing the owner requires administrative access.
sysadmin@localhost:~/Documents$ sudo chown root hello.sh [sudo] password for sysadmin:
The mv command is used to move a file from one location in the filesystem to another.
mv File(s) Directory
The mv command requires at least two arguments. The first argument is the source, a path to the file to be moved. The second argument is the destination, a path to where the file will be moved to.
sysadmin@localhost:~/Documents$ mv people.csv Work mv File_Name New_File_Name sysadmin@localhost:~/Documents$ mv animals.txt zoo.txt
The cp command is used to copy files.
cp [OPTIONS] SOURCE DESTINATION
Similar to the mv command it requires at least two arguments: a source and a destination.
sysadmin@localhost:~$ cp Documents/School .
The dd command is a utility for copying files or entire partitions at the bit level.
dd [OPTIONS] OPERAND
The command requires a couple arguments, see the table below to learn more about them.
sysadmin@localhost:~$ dd if=/dev/zero of=/tmp/swapex bs=1M count=50 500+0 records in 500+0 records out 524288000 bytes (524 MB) copied, 0.825745 s, 635 MB/s
Argument | Description |
if
|
Input File |
of
|
Output File |
bs
|
Block Size |
count
|
Count |
The rm command is used to delete files and directories.
rm [OPTIONS] FILE
When a file is deleted with the rm command, it is almost always permanently gone.
sysadmin@localhost:~/Documents$ rm linux.txt
The grep command is a text filter that will search input and return lines, which contain a match to a given pattern.
grep [OPTIONS] PATTERN [FILE]
The easiest pattern is a simple string, like a word or phrase.
sysadmin@localhost:~/Documents$ grep sysadmin passwd sysadmin:x:1001:1001:System Administrator,,,,:/home/sysadmin
Patterns can also take the form of regular expressions, a much more powerful way of matching text. The basic regular expressions are shown below.
Basic Regex Character(s) | Meaning |
. | Any one single character |
[ ] | Any one specified character |
[^ ] | Not the one specified character |
* | Zero or more of the previous character |
^ | If first character in the pattern, then pattern must be at beginning of the line |
$ | If last character in the pattern, then pattern must be at the end of the line |
The shutdown command arranges for the system to be brought down in a safe way.
shutdown [OPTIONS] TIME [MESSAGE]
The command requires a measure of time as an argument.
root@localhost:~# shutdown now Broadcast message from sysadmin@localhost (/dev/console) at 2:05 ... The system is going down for maintenance NOW! root@localhost:~# shutdown +1 "Goodbye World!" Broadcast message from sysadmin@localhost (/dev/console) at 3:07 ... The system is going down for maintenance in 1 minute! Goodbye World! root@localhost:~# Broadcast message from sysadmin@localhost (/dev/console) at 3:08 ... The system is going down for maintenance NOW! Goodbye World!
The ifconfig command stands for “interface configuration” and is used to display network configuration information.
ifconfig [OPTIONS]
sysadmin@localhost:~$ ifconfig eth0 Link encap:Ethernet HWaddr b6:84:ab:e9:8f:0a inet addr:192.168.1.2 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::b484:abff:fee9:8f0a/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:95 errors:0 dropped:4 overruns:0 frame:0 TX packets:9 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25306 (25.3 KB) TX bytes:690 (690.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:460 (460.0 B) TX bytes:460 (460.0 B)
Displays wireless network configuration information.
Running a command results in something called a process. The ps command can be used to list processes.
ps [OPTIONS]
The columns in the output contain columns of information about each process.
sysadmin@localhost:~$ ps PID TTY TIME CMD 80 ? 00:00:00 bash 94 ? 00:00:00 ps
The columns correspond to the following information.
PID
|
Process Identifier |
TTY
|
Terminal |
TIME
|
Total Processor Time |
CMD
|
Command |
The following options to the ps command will display more information.
ps -e
|
Display every process. |
ps -f
|
Display additional details. |
Package management is a system by which software can be installed, updated, queried or removed from a filesystem. The Advanced Package Tool, apt-get , makes management of packages easy.
Before installing a package, it is good practice to refresh the list of available packages using the apt-get update command.
sudo apt-get update
To search for keywords within these packages, you can use the apt-cache search command.
apt-cache search [keyword]
The output will contain a list a packages that contain that keyword.
sysadmin@localhost:~$ apt-cache search cow cowsay - configurable talking cow
Once you’ve found the package that you want to install, you can install it with the apt-get install command:
sudo apt-get install [package]
The command will output updates as the package is installed.
sysadmin@localhost:~$ sudo apt-get install cowsay [sudo] password for sysadmin: Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: filters The following NEW packages will be installed: cowsay 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/18.5 kB of archives. After this operation, 90.1 kB of additional disk space will be used. Selecting previously unselected package cowsay. (Reading database ... 24313 files and directories currently installed.) Preparing to unpack .../cowsay_3.03+dfsg1-6_all.deb ... Unpacking cowsay (3.03+dfsg1-6) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Setting up cowsay (3.03+dfsg1-6) ...
When updating all packages of the system two steps should be taken. First, update the cache of all packages available with apt-get update. Second, execute the apt-get upgrade command and all packages and dependencies will be updated.
apt-get update apt-get upgrade
An administrator can execute the apt-get remove command to remove a package or the apt-get purge command to purge a package completely from the system.
apt-get remove [package] apt-get purge [package]
Here is a recap of the package management commands.
sudo apt-get update
|
Refresh package list. |
apt-cache search [keyword]
|
Search for packages by keyword. |
sudo apt-get install [package]
|
Install a package. |
sudo apt-get upgrade
|
Update all packages and dependencies. |
sudo apt-get remove [package]
|
Remove a package. |
sudo apt-get purge [package]
|
Purge a package completely from the system. |
The passwd command is used to update a user’s password. Change the password of the current user.
passwd [OPTIONS] [USER]
Users can only change their own passwords, whereas the root user can update the password for any user.
sysadmin@localhost:~$ passwd Changing password for sysadmin. (current) UNIX password: netlab123 Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
With the -s option the passwd command prints the status information of the current user’s password:
sysadmin@localhost:~$ passwd -S sysadmin sysadmin P 03/01/2015 0 99999 7 -1
The fields in the output contain the following information:
Field | Example | Meaning |
User Name
|
sysadmin
|
The name of the user. |
Password Status |
P
|
P indicates a usable password.
L indicates a locked password. NP indicates no password. |
Change Date |
03/01/2015
|
The date when the password was last changed. |
Minimum |
0
|
The minimum number of days that must pass before the current password can be changed by the user. |
Maximum |
99999
|
The maximum number of days remaining for the password to expire. |
Warn |
7
|
The number of days prior to password expiry that the user is warned. |
Inactive |
-1
|
The number of days after password expiry that the user account remains active. |