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
ls,cd,pwd,cp,mvandrmhandle files and folders.grepsearches text inside files.findsearches 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.
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.
| Number | Meaning |
|---|---|
| 4 | Read |
| 2 | Write |
| 1 | Execute |
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
| Command | Purpose |
|---|---|
ps | Lists running processes |
top | Live view of processes, central processing unit (CPU) and memory |
df -h | Free and used space on mounted file systems, in readable units |
ip addr | Shows network interfaces and addresses |
dig example.com | Queries Domain Name System (DNS) for a name |
ping | Tests 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.
| Family | Tool |
|---|---|
| Debian and Ubuntu | apt and apt-get |
| Fedora and Red Hat | dnf, and the older yum |
sudo apt-get install nano
sudo dnf install nano
Common configuration files
| File | What it holds |
|---|---|
/etc/passwd | The list of user accounts: name, user ID, group ID, home folder and shell. Anyone can read it, and it holds no password hashes |
/etc/shadow | Password hashes. Only root can read it |
/etc/hosts | Fixed name to address mappings, checked before Domain Name System (DNS) |
/etc/fstab | File systems to mount at boot |
/etc/resolv.conf | DNS server addresses and the search domain |
OS components
| Component | Role |
|---|---|
| Bootloader | Loads the kernel at startup. GRUB is a common one |
| Kernel | The core of the OS. It manages hardware, memory and processes |
systemd | The init system and service manager, which starts services at boot |
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.
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,mvandrmhandle files and folders.grepsearches text inside files.findsearches for files.- Permissions use r (4), w (2) and x (1) for owner, group and others.
chmod 755gives the owner full access and others read and execute.chownchanges ownership.sudoruns one command as root.suswitches user.psandtopshow processes.dfshows disk space.ip addrshows addresses anddigtests DNS.aptis used on Debian and Ubuntu.dnforyumon 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 filegive 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(orapt-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.