|
|
|
|
8.12 Important Linux Commands
This section gives insight into the most important commands. There are
many more commands than listed in this chapter. Along with the individual
commands, parameters are listed and, where appropriate, a typical sample
application is introduced. To learn more about the various commands, use
the manual pages, accessed with man followed by the
name of the command, for example, man ls.
Man pages are displayed directly in the shell. To navigate them, move up
and down with PgUp and
PgDn. Move between the beginning and the end of a
document with Home and End.
End this viewing mode by pressing Q. Learn more about the
man command itself with man man.
In the following overview, the individual command elements are written in
different typefaces. The actual command and its mandatory options are
always printed as command option. Specifications or
parameters that are not required are placed in [square
brackets].
Adjust the settings to your needs. It makes no sense to write ls
file if no file named file actually exists.
You can usually combine several parameters, for example, by writing
ls -la instead of ls -l -a.
8.12.1 File Commands
The following section lists the most important commands for file
management. It covers anything from general file administration to
manipulation of file system ACLs.
File Administration
- ls[options][files]
-
If you run ls without any additional parameters,
the program lists the contents of the current directory in short
form.
- -l
-
Detailed list
- -a
-
Displays hidden files
- cp[options]source target
-
Copies source to target.
- -i
-
Waits for confirmation, if necessary, before an existing
target is overwritten
- -r
-
Copies recursively (includes subdirectories)
- mv[options]source target
-
Copies source to target
then deletes the original source.
- -b
-
Creates a backup copy of the source before
moving
- -i
-
Waits for confirmation, if necessary, before an existing
targetfile is overwritten
- rm[options]files
-
Removes the specified files from the file system. Directories are not
removed by rm unless the option
-r is used.
- -r
-
Deletes any existing subdirectories
- -i
-
Waits for confirmation before deleting each file
- ln[options]sourcetarget
-
Creates an internal link from source to
target. Normally, such a link points directly to
source on the same file system. However, if
ln is executed with the -s
option, it creates a symbolic link that only points to the directory
in which source is located, enabling linking
across file systems.
- -s
-
Creates a symbolic link
- cd[options][directory]
-
Changes the current directory. cd without any
parameters changes to the user's home directory.
- mkdir[options]directory
-
Creates a new directory.
- rmdir[options]directory
-
Deletes the specified directory if it is already empty.
- chown[options] username[:[group]]files
-
Transfers ownership of a file to the user with the specified
username.
- -R
-
Changes files and directories in all subdirectories
- chgrp[options]groupnamefiles
-
Transfers the group ownership of a given file to
the group with the specified group name. The file owner can only
change group ownership if a member of both the current and the new
group.
- chmod[options]modefiles
-
Changes the access permissions.
The mode parameter has three parts:
group, access, and
access type. group accepts the
following characters:
- u
-
User
- g
-
Group
- o
-
Others
For access, grant access with +
and deny it with -.
The access type is controlled by the following
options:
- r
-
Read
- w
-
Write
- x
-
Execute—executing files or changing to the directory
- s
-
Setuid bit—the application or program is started as if it
were started by the owner of the file
As an alternative, a numeric code can be used. The four digits of
this code are composed of the sum of the values 4, 2, and 1—the
decimal result of a binary mask. The first digit sets the set user ID
(SUID) (4), the set group ID (2), and the sticky (1) bits. The second
digit defines the permissions of the owner of the file. The third
digit defines the permissions of the group members and the last digit
sets the permissions for all other users. The read permission is set
with 4, the write permission with 2, and the permission for executing
a file is set with 1. The owner of a file would usually receive a 6
or a 7 for executable files.
- gzip[parameters]files
-
This program compresses the contents of files using complex
mathematical algorithms. Files compressed in this way are given the
extension .gz and need to be uncompressed before
they can be used. To compress several files or even entire
directories, use the tar command.
- -d
-
Decompresses the packed gzip files so they return to their
original size and can be processed normally (like the command
gunzip)
- taroptionsarchivefiles
-
tar puts one or more files into an archive.
Compression is optional. tar is a quite complex
command with a number of options available. The most frequently used
options are:
- -f
-
Writes the output to a file and not to the screen as is usually
the case
- -c
-
Creates a new tar archive
- -r
-
Adds files to an existing archive
- -t
-
Outputs the contents of an archive
- -u
-
Adds files, but only if they are newer than the files already
contained in the archive
- -x
-
Unpacks files from an archive (extraction)
- -z
-
Packs the resulting archive with gzip
- -j
-
Compresses the resulting archive with bzip2
- -v
-
Lists files processed
The archive files created by tar end with
.tar. If the tar archive was also compressed
using gzip, the ending is
.tgz or .tar.gz. If it was
compressed using bzip2, the ending is
.tar.bz2.
- locatepatterns
-
This command is only available if you have installed the
findutils-locate package.
The locate command can find in which directory a
specified file is located. If desired, use wild cards
to specify filenames. The program is very speedy, because it uses a
database specifically created for the purpose (rather than searching
through the entire file system). This very fact, however, also
results in a major drawback: locate is unable to find any files
created after the latest update of its database. The database can be
generated by root
with updatedb.
- updatedb[options]
-
This command performs an update of the database used by
locate. To include files in all existing
directories, run the program as
root. It also makes
sense to place it in the background by appending an ampersand
(&), so you can immediately continue
working on the same command line (updatedb &).
This command usually runs as a daily cron job (see
cron.daily).
- find[options]
-
With find, search for a file in a given directory.
The first argument specifies the directory in which to start the
search. The option -name must be followed by a
search string, which may also include wild cards. Unlike
locate, which uses a database,
find scans the actual directory.
Commands to Access File Contents
- file[options][files]
-
With file, detect the contents of the specified
files.
- -z
-
Tries to look inside compressed files
- cat[options]files
-
The cat command displays the contents of a file,
printing the entire contents to the screen without interruption.
- -n
-
Numbers the output on the left margin
- less[options]files
-
This command can be used to browse the contents of the specified
file. Scroll half a screen page up or down with PgUp
and PgDn or a full screen page down with
Space. Jump to the beginning or end of a file using
Home and End. Press
Q to exit the program.
- grep[options]searchstringfiles
-
The grep command finds a specific search string in the specified
files. If the search string is found, the command displays the line
in which searchstring was found along with the
filename.
- -i
-
Ignores case
- -H
-
Only displays the names of the respective files, but not the text
lines
- -n
-
Additionally displays the numbers of the lines in which it found a
hit
- -l
-
Only lists the files in which searchstring does
not occur
- diff[options]file1file2
-
The diff command compares the contents of any two
files. The output produced by the program lists all lines that do not
match. This is frequently used by programmers who need only send
their program alterations and not the entire source code.
- -q
-
Only reports whether the two files differ
- -u
-
Produces a unified diff, which makes the output
more readable
File Systems
- mount[options][device]mountpoint
-
This command can be used to mount any data media, such as hard disks,
CD-ROM drives, and other drives, to a directory of the Linux file
system.
- -r
-
Mount read-only
- -t filesystem
-
Specify the file system, commonly ext2 for
Linux hard disks, msdos for MS-DOS media,
vfat for the Windows file system, and
iso9660 for CDs
For hard disks not defined in the file
/etc/fstab, the device type must also be
specified. In this case, only
root can mount it.
If the file system should also be mounted by other users, enter the
option user in the appropriate line in the
/etc/fstab file (separated by commas) and save
this change. Further information is available in the
mount(1) man page.
- umount[options]mountpoint
-
This command unmounts a mounted drive from the file system. To
prevent data loss, run this command before taking a removable data
medium from its drive. Normally, only
root is allowed to
run the commands mount and
umount. To enable other users to run these
commands, edit the /etc/fstab file to specify
the option user for the respective drive.
8.12.2 System Commands
The following section lists a few of the most important commands needed
for retrieving system information and controlling processes and the
network.
System Information
- df[options][directory]
-
The df (disk free) command, when used without any
options, displays information about the total disk space, the disk
space currently in use, and the free space on all the mounted drives.
If a directory is specified, the information is limited to the drive
on which that directory is located.
- -h
-
Shows the number of occupied blocks in gigabytes, megabytes, or
kilobytes—in human-readable format
- -T
-
Type of file system (ext2, nfs, etc.)
- du[options][path]
-
This command, when executed without any parameters, shows the total
disk space occupied by files and subdirectories in the current
directory.
- -a
-
Displays the size of each individual file
- -h
-
Output in human-readable form
- -s
-
Displays only the calculated total size
- free[options]
-
The command free displays information about RAM
and swap space usage, showing the total and the used amount in both
categories. See Section 16.1.6, The free Command, (↑ Reference ) for more information.
- -b
-
Output in bytes
- -k
-
Output in kilobytes
- -m
-
Output in megabytes
- date[options]
-
This simple program displays the current system time. If run as
root, it can also be used
to change the system time. Details about the program are available in
the date(1) man page.
Processes
- top[options]
-
top provides a quick overview of the currently
running processes. Press H to access a page that
briefly explains the main options for customizing the program.
- ps[options][process ID]
-
If run without any options, this command displays a table of all your
own programs or processes—those you started. The options for
this command are not preceded by hyphen.
- aux
-
Displays a detailed list of all processes, independent of the
owner
- kill[options]process ID
-
Unfortunately, sometimes a program cannot be terminated in the normal
way. In most cases, you should still be able to stop such a runaway
program by executing the kill command, specifying
the respective process ID (see top and
ps). kill sends a
TERM signal that instructs the program to shut
itself down. If this does not help, the following parameter can be
used:
- -9
-
Sends a KILL signal instead of a
TERM signal, bringing the specified process
to an end in almost all cases
- killall[options]processname
-
This command is similar to kill, but uses the
process name (instead of the process ID) as an argument, killing all
processes with that name.
Network
- ping[options]hostname or IP address
-
The ping command is the standard tool for testing
the basic functionality of TCP/IP networks. It sends a small data
packet to the destination host, requesting an immediate reply. If
this works, ping displays a message to that
effect, which indicates that the network link is basically
functioning.
- -cnumber
-
Determines the total number of packages to send and ends after
they have been dispatched (by default, there is no limitation set)
- -f
-
flood ping: sends as many data packages as
possible; a popular means, reserved for
root, to test
networks
- -ivalue
-
Specifies the interval between two data packages in seconds
(default: one second)
- host[options]hostname[server]
-
The domain name system resolves domain names to IP addresses. With
this tool, send queries to name servers (DNS servers).
- ssh[options][user@]hostname[command]
-
SSH is actually an Internet protocol that enables you to work on
remote hosts across a network. SSH is also the name of a Linux
program that uses this protocol to enable operations on remote
computers.
Miscellaneous
- passwd[options][username]
-
Users may change their own passwords at any time using this command.
The administrator
root can use the
command to change the password of any user on the system.
- su[options][username]
-
The su command makes it possible to log in under a
different username from a running session. Specify a username and the
corresponding password. The password is not required from
root, because
root is authorized to
assume the identity of any user. When using the command without
specifying a username, you are prompted for the
root password and change to
the superuser
(root). Use
su - to start a login shell for a different user.
- halt[options]
-
To avoid loss of data, you should always use this program to shut
down your system.
- reboot[options]
-
Does the same as halt except the system performs
an immediate reboot.
- clear
-
This command cleans up the visible area of the console. It has no
options.
8.12.3 For More Information
There are many more commands than listed in this chapter. For information
about other commands or more detailed information, the O'Reilly
publication Linux in a Nutshell is recommended.
|
|
|