Appendix A: Linux Commands


Appendix A: Linux Commands

This appendix presents alphabetically arranged reference entries for the most important Linux commands. The goal here is to provide you with an overview of all commands needed to manage files and directories, start and stop processes, find files, work with text files, and access online help.

If you are looking for a command for a specific task, but don’t know which command to use, you may find it helpful to browse through the commands by category. Table A-1 shows the Linux commands organized by categories.

Table A-1: Linux Commands

Command Name

Action

Getting Online Help

 

apropos

Finds man pages for a specified keyword

info

Displays online help information about a specified command

man

Displays online help information

whatis

Similar to apropos, but searches for complete words only

Making Commands Easier

 

alias

Defines an abbreviation for a long command

type

Shows the type and location of a command

unalias

Deletes an abbreviation defined using alias

Managing Files and Directories

 

cd

Changes the current directory

chattr

Changes file attributes in Linux ext2 and ext3 file systems

chgrp

Changes group ownership of file

chmod

Changes file permissions

chown

Changes file owner and group

cp

Copies files

ln

Creates symbolic links to files and directories

ls

Displays the contents of a directory

mkdir

Creates a directory

mv

Renames a file as well as moves a file from one directory to another

rm

Deletes files

rmdir

Deletes directories

pwd

Displays the current directory

touch

Updates a file’s time stamp

Finding Files

 

find

Finds files based on specified criteria such as name, size, and so on

lsof

List open files

locate

Finds files using a periodically updated database

whereis

Finds files based in the typical directories where executable (also known as binary) files are located

which

Finds files in the directories listed in the PATH environment variable

Processing Files

 

cat

Displays a file on standard output (can be used to concatenate several files into one big file)

cut

Extracts specified sections from each line of text in a file

dd

Copies blocks of data from one file to another (used to copy data from devices)

diff

Compares two text files and finds any differences

expand

Converts all tabs into spaces

file

Displays the type of data in a file

fold

Wraps each line of text to fit a specified width

grep

Searches for regular expressions within a text file

less

Displays a text file, one page at a time (can go backwards also)

lpr

Prints files

more

Displays a text file, one page at a time (goes forward only)

nl

Numbers all nonblank lines in a text file and prints the lines to standard output

paste

Concatenates corresponding lines from several files

patch

Updates a text file using the differences between the original and revised copy of the file

sed

Copies a file to standard output while applying specified editing commands

sort

Sorts lines in a text file

split

Breaks up a file into several smaller files with specified size

tac

Reverses a file (last line first and so on)

tail

Displays the last few lines of a file

tr

Substitutes one group of characters for another throughout a file

uniq

Eliminates duplicate lines from a text file

wc

Counts the number of lines, words, and characters in a text file

zcat

Displays a compressed file (after decompressing)

zless

Displays a compressed file one page at a time (can go backwards also)

zmore

Displays a compressed file one page at a time

Archiving and Compressing Files

 

compress

Compresses files

cpio

Copies files to and from an archive

gunzip

Decompresses files compressed with GNU ZIP (gzip) or compress

gzip

Compresses files (more powerful than compress)

tar

Creates an archive of files in one or more directories (originally meant for archiving on tape)

uncompress

Decompresses files compressed with compress

Managing Processes

 

bg

Runs an interrupted process in the background

fg

Runs a process in the foreground

free

Displays the amount of free and used memory in the system

halt

Shuts down Linux and halts the computer

kill

Sends a signal to a process (usually used to terminate a process)

ldd

Displays the shared libraries needed to run a program

nice

Runs a process with lower priority (referred to as nice mode)

ps

Displays a list of currently running processes

printenv

Displays the current environment variables

pstree

Similar to ps, but shows parent-child relationships clearly

reboot

Stops Linux and then restarts the computer

shutdown

Shuts down Linux

top

Displays a list of most processor- and memory-intensive processes

uname

Displays information about the system and the Linux kernel

Managing Users

 

chsh

Changes the shell (command interpreter)

groups

Prints the list of groups that includes a specified user

id

Displays the user and group ID for a specified user name

passwd

Changes the password

su

Starts a new shell as another user or root (when invoked without any argument)

Managing the File System

 

df

Summarizes free and available space in all mounted storage devices

du

Displays disk usage information

fdformat

Formats a diskette

fdisk

Partitions a hard disk

fsck

Checks and repairs a file system

mkfs

Creates a new file system

mknod

Creates a device file

mkswap

Creates a swap space for Linux in a file or a disk partition

mount

Mounts a device (for example, the CD-ROM) on a directory in the file system

swapoff

Deactivates a swap space

swapon

Activates a swap space

sync

Writes buffered data to files

tty

Displays the device name for the current terminal

umount

Unmounts a device from the file system

Working with Date and Time

 

cal

Displays a calendar for a specified month or year

date

Shows the current date and time or sets a new date and time

The rest of this appendix covers individual reference entries for each command shown in Table A-1. Each reference entry is organized as follows:

  • Purpose tells you when to use the command.

  • Syntax shows the syntax of the command with a few common options. Also, typical option values are shown. All optional items are shown in square brackets.

  • Options lists most options, along with a brief description of each option. For many commands, you will find all options listed in this section. However, some commands have too many options to cover here. For those commands, I describe the most commonly used options.

  • Description describes the command and provides more details about how to use it.

alias

Purpose

Define an abbreviation for a long command or view the current list of abbreviations.

Syntax

alias [abbrev=command]

Options

None

Description

If you type alias alone, you get a listing of all currently defined abbreviations. Typically, you use alias to define easy-to-remember abbreviations for longer commands. For example, if you type ls -l often, you might add a line with alias ll=’ls -l’ in the .bashrc file in your home directory. Then, you can type ll instead of ls -l to see a detailed listing of a directory. alias is a built-in command of the bash shell and is described in Chapter 7.