Operating Systems · Topic 13 of 15

Linux Commands and Tools

Learn Linux desktop commands ls, cd, mv, cp, rm, chmod, chown, grep, find, ps, top, df, ip, dig and sudo, plus permissions, packages and backups.

  • 5 min
In a nutshell
  • ls, cd, pwd, cp, mv and rm handle files and folders.
  • grep searches text inside files. find searches for files.
  • Permissions use r (4), w (2) and x (1) for owner, group and others.

What it is

Linux is an open source operating system (OS) used on servers, desktops and many devices. Technicians meet it mostly through the command line, called the shell or terminal. The common commands are the same across most distributions (distros), the packaged versions of Linux.

UsertypesShellterminalrunsCommandls, cp, grepacts onFiles and system

How it works

Files and navigation

pwd                      # print the current directory
ls -l                    # list files with permissions and owners
cd /etc                  # change directory
cp -r docs docs_backup   # copy a folder and its contents
mv old.txt new.txt       # move or rename
rm old.txt               # delete a file
rm -r olddir             # delete a folder and its contents
cat notes.txt            # show a file
nano notes.txt           # edit a file in a simple editor (vi is another common editor)
man ls                   # open the manual page for a command

Linux is case sensitive, so File.txt and file.txt are different files. Deleted files do not go to a recycle bin when rm is used.

Searching

grep "error" syslog          # lines that contain the word
find /home -name "report.txt"  # locate a file by name

grep searches inside files for text. find searches the file system for files by name or other attributes.

Permissions and ownership

ls -l shows ten characters for each item, such as -rwxr-xr--. The first is the type, then three sets of read (r), write (w) and execute (x) for the owner, the group and everyone else.

NumberMeaning
4Read
2Write
1Execute

Add the numbers for each set. So 7 is read, write and execute, 6 is read and write, and 5 is read and execute.

chmod 755 script.sh      # owner rwx, group r-x, others r-x
chmod 644 notes.txt      # owner rw-, group r--, others r--
chown alex:staff notes.txt   # change owner and group

chmod changes permissions and chown changes the owner. A script needs the execute permission before it can be run directly by name.

Privileges

The root user is the administrator account. Rather than log in as root, a user runs a single command with elevated rights using sudo (superuser do). The su command switches to another user account.

sudo apt update
su - alex

Processes, disks and network

CommandPurpose
psLists running processes
topLive view of processes, central processing unit (CPU) and memory
df -hFree and used space on mounted file systems, in readable units
ip addrShows network interfaces and addresses
dig example.comQueries Domain Name System (DNS) for a name
pingTests reachability

The older ifconfig command also shows interface settings and may still appear on older systems.

Filesystem management

sudo fsck /dev/sdb1          # check and repair a file system
sudo mount /dev/sdb1 /mnt    # attach the device at /mnt
sudo umount /mnt             # detach it again

fsck checks and repairs a file system, and it should be run on an unmounted volume. mount attaches a device to a folder so its files can be used, and umount detaches it. Note the spelling umount, with no n.

More disk and network commands

du -sh /var/log                       # size of one folder
curl -I https://example.com          # fetch only the headers
curl -O https://example.com/a.zip    # download a file
traceroute example.com               # list the hops to a host

df reports space for each mounted file system, while du reports how much space a folder uses. curl transfers data to or from a URL. traceroute is the Linux counterpart of tracert.

Installing software

Package managers fetch and install software from repositories.

FamilyTool
Debian and Ubuntuapt and apt-get
Fedora and Red Hatdnf, and the older yum
sudo apt-get install nano
sudo dnf install nano

Common configuration files

FileWhat it holds
/etc/passwdThe list of user accounts: name, user ID, group ID, home folder and shell. Anyone can read it, and it holds no password hashes
/etc/shadowPassword hashes. Only root can read it
/etc/hostsFixed name to address mappings, checked before Domain Name System (DNS)
/etc/fstabFile systems to mount at boot
/etc/resolv.confDNS server addresses and the search domain

OS components

ComponentRole
BootloaderLoads the kernel at startup. GRUB is a common one
KernelThe core of the OS. It manages hardware, memory and processes
systemdThe init system and service manager, which starts services at boot
FirmwareBootloaderGRUBKernelsystemdstarts services

Use systemctl to control services, for example sudo systemctl start cups, sudo systemctl stop cups and systemctl status cups.

Other features

A graphical desktop environment sits on top of the OS for users who prefer it. Linux file systems commonly use ext4.

Samba lets a Linux computer share files and printers with Windows using Server Message Block (SMB). Backups can use tools such as rsync and tar, and cron can run them on a schedule.

Watch out

rm -r deletes without asking and without a recycle bin. Check the path before pressing Enter, especially when running it with sudo.

What to remember for the exam

  • ls, cd, pwd, cp, mv and rm handle files and folders.
  • grep searches text inside files. find searches for files.
  • Permissions use r (4), w (2) and x (1) for owner, group and others.
  • chmod 755 gives the owner full access and others read and execute.
  • chown changes ownership.
  • sudo runs one command as root. su switches user.
  • ps and top show processes. df shows disk space.
  • ip addr shows addresses and dig tests DNS.
  • apt is used on Debian and Ubuntu. dnf or yum on Red Hat family systems.

Common mistakes

Ignoring case. Report.txt and report.txt are not the same file in Linux.

Mixing up chmod and chown. One changes permissions and the other changes the owner.

Using rm on the wrong folder. There is no undo, so confirm the target.

Running everything as root. Use sudo for the single command that needs it.

Check yourself

  • Which command shows the permissions of files in a folder?

    ls -l.

  • What does chmod 644 file give the owner and everyone else?

    The owner can read and write, and the group and others can only read.

  • Which command installs software on Ubuntu?

    sudo apt install name (or apt-get install).

Topic 13 of 15 in Operating Systems

Test yourself on Operating Systems

The full CompTIA A+ 220-1202 question bank has 835 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by CompTIA. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.