Unix Commands
Unix Commands
Contents [hide]
1 man
2 info
3 apropos
4 whatis
5 makewhatis
[edit] man
man displays the manual page for the specified command
-k TEXT search manual page titles and synopsis lines for TEXT
Examples:
$ man chown
man has different sections.
$ man 2 chmod
To search the man pages for "newsgroups",
$ man -k newsgroups
actsync (8) - synchronize newsgroupsoups
newsgroups (1) - a program to list unsubscribed newsgroups
If this does not work you may have to run the makewhatis command.
[edit] info
info is an advanced man command that is sometimes available. It displays the improved manual pages
in Info format for specified command.
Examples:
$ info grep
To find occurrence of 'grep' in all info manual pages:
$ info -w grep
/usr/share/info/grep.info.gz
$ info -f ./some_cmd.info.gz
[edit] apropos
apropos searches the manual page short descriptions for a specified keyword
On many systems this is exactly the same as the -k option of the man command.
Examples:
$ apropos newsgroups
active (5) - list of active Usenet newsgroups
newsgroups (1) - a program to list unsubscribed newsgroups
[edit] whatis
whatis displays short man page descriptions. Unlike
Examples:
$ whatis info
info (1) - read Info documents
info (5) - readable online documentation
$ whatis chmod
chmod (1) - change file modes
chmod, fchmod (2) - change mode of file
[edit] makewhatis
makewhatis creates the database for the whatis, appropos, and man -k commands. This is commonly
run automatically by your system however sometimes you need to run this manually.
Examples:
# makewhatis
$ sudo makewhatis
Contents [hide]
1 ls
2 mkdir
3 cd
4 pwd
5 chroot
6 cp
7 mv
8 rm
9 rmdir
10 shred
11 touch
12 df
13 ln
14 chown
15 chmod
[edit] ls
ls
LiSt contents of directory
=1 FreeBSD manual page
NetBSD manual page
=1 OpenBSD manual page
-a
all files (include files with . prefix)
-l
long detail (provide file statistics)
-t
order by creation time
-r
reverse order
-F
mark directories with /, executables with *, symbolic links with @, local sockets with =, named pipes
(FIFOs) with |
Other options include:
-Fx --color
color-coded listing
-s
show filesizes
-s -h
show filesizes in kilobytes and megabytes
ls *st* : list files that contain st in name
ls > list : output list to file named "list"
ls | more : fit listing to screen
Examples
$ ls
fish hello.txt
$ ls -l
-rw-r--r-- 1 username groupname 0 Apr 11 00:09 fish
-rw-r--r-- 1 username groupname 11 Apr 11 00:10 hello.txt
Several systems have an alias ll which does the same as ls -l:
$ ll
-rw-r--r-- 1 username groupname 0 Apr 11 00:09 fish
-rw-r--r-- 1 username groupname 11 Apr 11 00:10 hello.txt
Be careful with the -F option. Here is one example:
$ ls -F /usr/X11R6/bin/X*
/usr/X11R6/bin/X@ /usr/X11R6/bin/Xnest* /usr/X11R6/bin/Xprt*
/usr/X11R6/bin/Xmark* /usr/X11R6/bin/Xorg* /usr/X11R6/bin/Xvfb*
We do not know yet if there is a symbolic link "X" and an executable "Xmark" or if "X@" and
"Xmark*" are just the names of normal files. (Though "@" and "*" are not much found in filenames,
they are possible.) So we check by dropping the -F:
$ ls /usr/X11R6/bin/X*
/usr/X11R6/bin/X /usr/X11R6/bin/Xnest /usr/X11R6/bin/Xprt
/usr/X11R6/bin/Xmark /usr/X11R6/bin/Xorg /usr/X11R6/bin/Xvfb
[edit] mkdir
mkdir is a utility for creating a directory.
Examples
$ mkdir newdirectoryname
[edit] cd
cd
change Current Directory
cd at the Linux questions wiki
cd changes the current directory of the shell. This current directory will be used by other programs
launched from the shell.
Because "cd" changes the state of the shell, it is a shell built-in command. It contrast, most commands
are separate programs which the shell starts.
Examples
$ cd foobar
Change to your home directory, cd command used without an option will drop you back into your
home directory.
$ cd
~ (tilde) stores the path to your home directory, this command has same effect as the previous one.
$ cd ~
Change to parent directory:
$ cd ..
Tips:
The "CDPATH" might only work in some shells. For example, ksh has it.
By setting "CDPATH" environment variable in your shell you can take advantage of shell command
completion facility.
$ echo $CDPATH
.:/usr/local:/usr/share/doc
If you have the $CDPATH set, then you press 'TAB' key and get possible path completions
$ cd bas [TAB]
base-config/ base-files/ base-passwd/ bash/ bastille/
[edit] pwd
pwd
Print Working Directory
=1 FreeBSD manual page
NetBSD manual page
=1 OpenBSD manual page
pwd (for Print Working Directory) shows the current directory that you are in.
Though "pwd" is often available as an external program (like /bin/pwd), many shells offer an
equivalent version as a shell builtin command. Like any external command, "pwd" would inherit the
current directory from the shell or other program that starts it.
Examples
$ pwd
/home/username
You can change the directory, you can also
$ cd /usr
$ pwd
/usr
You can also use "pwd" in scripts. If you have enough experience with scripting, then you would know
that the next line complains if the current directory is /home/username.
[edit] chroot
chroot
CHange ROOT directory
=8 FreeBSD manual page
NetBSD manual page
=8 OpenBSD manual page
chroot changes the root filesystem. The "chroot" page at the Linux questions wiki explains why you
might want to do this.
Examples
To change the root filesystem so /mnt/usbdrive/ becomes / and files outside of it cannot be seen:
# chroot /mnt/usbdrive/
You must be root user to "chroot". Other users would be able to use "chroot" to gain root (superuser)
priveleges, so their use of "chroot" is disallowed.
$ chroot /mnt/usbdrive/
chroot: /mnt/usbdrive/: Operation not permitted
[edit] cp
cp copies a file
-r
copies directories (recursively)
-p
preserves permissions, ownership, and timestamps
-i
prompt before overwrite
Examples
Makes a copy of file 'debian' and call it 'Debian' (assuming 'Debian' is not already a directory)
$ cp -i debian Debian
Makes a copy of file 'debian' and put it at /tmp/debian
$ cp -i debian /tmp/debian
Same as the previous command (the filename defaults to be the same).
$ cp -i debian /tmp
Makes a copy of directory 'mydir' (and all its contents) and put it at /tmp/debian
$ cp -ir mydir/ /tmp
Copy multiple files to directory /tmp
$ cp -i foo bar baz /tmp
$_ The File System Utilities module or this section of Wikibooks Guide to Unix Computing is a stub.
You can help Wikibooks by expanding it.
[edit] mv
mv move and/or rename files
Examples
[edit] rm
rm
ReMove and delete files
=1 FreeBSD manual page
NetBSD manual page
=1 OpenBSD manual page
rm deletes a file from the filesystem, like the "del" command in DOS.
The GNU long options (like --directory) are available on Linux, but not most other systems.
-d, --directory
unlink FILE, even if it is an empty directory (some systems let superuser unlink non-empty directories
too)
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
-P
(*BSD only) overwrite file before deletion
-r, -R, --recursive
remove the contents of directories recursively (the force option must often be used to successully run
rm recursively)
-v, --verbose
(GNU only) explain what is being done
--help
(GNU only) display help and exit
--version
(GNU only) output version information and exit
Examples:
The usage of "rm" is considered potentially more dangerous than equivalents in other operating
systems because of the way the shell parses wildcards and names of special directories and in its non-
verbose actions.
Here is a classic example. Instead of deleting files that end with .o ("*.o") it deletes all files in the
directory ("*") and also a file called .o. There is an unwanted space between the asterisk and the period.
$ rm * .o
rm: cannot remove `.o': No such file or directory
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
$ rm -- -foo
$ rm ./-foo
It might be useful to create an alias such as "remove" which moves the files to a local "trash" file so
you can go there and recover files you accidentally "remove"d.
Note that if you use rm to remove a file, it is usually possible to recover the contents of that file since
rm does not remove it from the hard disk. It simply removes the filesystems link to it.
On *BSD systems, the -P option overwrites the data with the file before removing it.
$ rm -P secretfile
However, as the NetBSD manual page explains it:
Recent research indicates that as many as 35 overwrite passes with carefully chosen data patterns may
be necessary to actually prevent recovery of data from a magnetic disk. Thus the -P option is likely
both insufficient for its design purpose and far too costly for default operation.
So while examining the data (using fsdb or making a disk image) will not reveal the secret data, other
methods (such as laboratory examination of the disk) will reveal the data. In short, rm -P does not
delete data securely. A program that attempts to delete data securely is GNU shred, available on Linux.
But "shred" is not always successful in secure deletion; read its entry below.
[edit] rmdir
rmdir is a utility for deleting empty directories.
Examples
$ rmdir directoryname
If the directory is not empty, the correct way to remove the directory and all its contents recursively is
to use
$ rm -r directoryname
[edit] shred
shred
Attempt to securely delete files
shred at the GNU core-utils manual
shred overwrites a file multiple times with special data patterns to make the old contents of the file
unrecoverable from a disk, especially a hard disk. This command is part of GNU coreutils, so it is often
only available on Linux systems.
Note that this actually is ineffective on most filesystems because they can keep old copies of data. Most
popular Linux filesystems (including ext3) keep such copies through journaling. However, "shred" is
very useful for destroying the data on entire partitions or disks.
-u, --remove
unlink the file after removing it
-NUMBER, -n NUMBER, --iterations=NUMBER
the number of iterations of overwriting the file; default is 25 iterations
Examples: Remove and completely destroy secretfile from a filesystem that overwrites data in place
and does not use journaling (for example, the UFS filesystem of *BSD). For the last step, after the data
is destroyed, the "-u" option unlinks the file from the filesystem.
$ shred -u secretfile
Note that if secretfile has multiple hard links (with ln for example), it will continue to exist with those
other names, but will contain only random data.
[edit] touch
touch lets you change the date on a file. Can also be used to create a blank file.
Examples
This will change the access date and time of filename to the current time. If filename doesn't exist it
will create a blank file.
$ touch filename
[edit] df
df reports the amount of free disk space available on each partition.
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/md0 5763508 207380 5263352 4% /
/dev/md1 78819376 13722288 61093296 19% /home
/dev/md4 23070564 4309572 17589056 20% /usr
/dev/md2 5763508 1757404 3713328 33% /var
/dev/md3 2877756 334740 2396832 13% /tmp
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda1 2.3G 2.1G 133M 95% /
tmpfs 61M 8.0K 61M 1% /dev/shm
/dev/hda2 2.0G 1.8G 113M 94% /usr
[edit] ln
ln creates links between files
Examples:
$ ln -s /home/alice/code/bin/world hello
[edit] chown
chown
CHanges OWNer of file
=8 FreeBSD manual page
NetBSD manual page
=8 OpenBSD manual page
chown changes the owner and group of files. Normally, only root is allowed to do this, but if a user
owns a file, then that user can change the group, but only to groups containing that user. On old
systems, the ability of users to give files to other users caused abuses, so most systems prohibit non-
root users from changing the owner of files.
-R
recursively change owner/group on an entire directory tree
-h
do not follow symbolic links i.e. changes owner of the link, not the target file
-f
indicate no errors if change failed
Examples:
Root changes the ownership of "/etc/passwd" and "/etc/shadow" to user root, group wheel:
$ ls -ld /usr/local/src/xc
drwxr-xr-x 11 tux tux 512 Sep 30 16:19 /usr/local/src/xc
$ chown tux:wheel /usr/local/src/xc
$ ls -ld /usr/local/src/xc
drwxr-xr-x 11 tux wheel 512 Sep 30 16:19 /usr/local/src/xc
[edit] chmod
chmod
CHanges file MODe
=1 FreeBSD manual page
NetBSD manual page
=1 OpenBSD manual page
chmod changes permissions of files. One must be familiar with Unix file permissions to understand this
command. There are three permissions: read ("r"), write ("w"), and execute ("x"). There are three sets
of permissions: for the owning user of the file ("u"), for the group of the file ("g"), and for other users
("o").
For a file, "execute" means to run it as a program. For a directory, "execute" permission is required to
use anything in that directory tree, so doing anything with "/usr/share/doc/README" requires execute
permissions on all of "/", "/usr", "/usr/share", and "/usr/share/doc".
If you are interested in more advanced topics like the set-uid, set-gid, sticky bits and octal numbers, try
reading the FreeBSD manual page at http://www.FreeBSD.org/cgi/man.cgi (type "chmod" in the form
and submit).
-R
recursively change or set permissions on an entire directory tree
Examples:
We wrote a shell script called "configure". We make it executable ("+x") and then execute it as a
command. Usually, "+x" is the same as "u+x" or "ug+x", depending on the status of the file mode
creation mask.
$ chmod +x configure
$ ./configure
Allow the owning user to run "configure":
The "root" user wants to set up /usr/local/src so that all users in group "wsrc" (including "tux") can
create files there. Root will continue to own the directory. This is done by changing the group of
/usr/local/src to "wsrc" and then by granting to the group ("g") the read, write, and execute permissions
("+rwx").
UNIX/COMMANDS/FINDING FILES
Contents [hide]
1 find
2 whereis
3 which
4 locate
[edit] find
find searches a given path for a file or folder. The syntax is: find [path...] [expression...]
Examples: On some of the latest Unix-like OS's, the -print option is a default and can be omitted. The
following command searches for the file 'grub.conf' starting at the root ('/') directory.
[edit] whereis
whereis searches the normal executable and man page locations for a specified file.
Examples:
$ whereis ls
ls: /bin/ls /usr/bin/ls /usr/man/man1/ls.1.gz /usr/share/man/man1/ls.1.gz
[edit] which
which searches the locations in your PATH variable for a specified file. If you know a program is in
your path (i.e you can run it) this is faster than whereis.
$ which pine
/usr/bin/pineddd
[edit] locate
locate finds all filenames that match the specified query.
Examples:
$ locate make.conf
/etc/make.conf
/etc/make.conf.orig
/etc/make.conf.example
/usr/qt/3/mkspecs/linux-g++/qmake.conf
/usr/share/man/man5/make.conf.5.gz
locate however, is a GNU software and the command is not a standard in traditional UNIX systems like
Solaris. The locate command comes standard with Linux based systems.
UNIX/COMMANDS/FILE VIEWING
Contents [hide]
1 cat
2 more
3 less
4 od
5 head
6 tail
[edit] cat
cat displays the contents of a file to screen. It can also display multiple files concatenated together, and
using the shell, its standard output can be redirected from the screen to the file.
-n line numbering
-s squeeze sequences of blank lines into one
-v show non-printing characters
Examples
$ cat example.txt
The contents of the file example.txt are now
displayed.
$ cat -n example.txt
1 The contents of the file
2 example.txt are now
3 displayed.
Concatenating multiple files (in this example, the same file twice):
N^HNA^HAM^HME^HE
p^Hpa^Hax^Hx - read and write file archives and copy directory hierarchies
Using "cat" with no arguments makes it copy standard input to standard output. Combined with shell
redirection, this makes it easy to write a very short text file. All one needs to know is to press Control-
D (^D) to indicate end of input, finish the file, and return to the shell. Here is how to write
"example.txt":
$ cat > example.txt
The contents of the file
example.txt are now
displayed.
^D $
If you put "cat" with no arguments in a pipe, it only copies standard input to standard output. This
might seem useless. For example, the following two pipes have the same function:
$ dmesg | less
$ dmesg | cat | less
However, "cat" can be used as insulation to make programs think that they are not running on
terminals. In the next example, GNU bc does not print its copyright message on startup. We enter one
calculation ("3 + 9") and then quit (^D):
$ bc | cat
3+9
12
^D
[edit] more
more paginates output. The problem with "cat" is that if a file is too long, then it falls beyond the top of
the screen. The job of "more" is to stop and wait when it fills the screen. Most users find it easier to use
"less", but on some systems "more" has all of the features of "less".
Keys:
$ more hello.txt
Hello World
[edit] less
less paginates output. The program is called "less" because of the joke that "less is more", "less"
actually has several features which "more" lacks.
Keys:
h read help. You might forget the other commands, but remember this one!
j go down one line. The down-arrow key might also work.
k go up one line. The up-arrow key might also work.
d go down one-half screen.
u go up one-half screen.
f go forward one screen.
b go back one screen.
p return to the top of the file.
q quit the pager.
Number arguments:
0 through 9: type a number. The number will be used as the argument N to the next command.
j go down N lines.
k go up N lines.
p jump to the N% position, where 0% is the first line and 100% the last line of the file.
Examples:
$ less example.txt
Pipe "dmesg" into "less" so that the dmesg does not scroll off the screen:
$ dmesg | less
[edit] od
od is a utility that lets you view binary files.
Examples:
$ od wordlist.dat
0000000 064506 071562 006564 051412 061545 067157 006544 040412
0000020 070160 062554 005015 072522 061155 062554 005015 061501
View a file in hex format:
$ od -x wordlist.dat
0000000 6946 7372 0d74 530a 6365 6e6f 0d64 410a
0000020 7070 656c 0a0d 7552 626d 656c 0a0d 6341
View a file in character format:
$ od -c wordlist.dat
0000000 F i r s t \r \n S e c o n d \r \n A
0000020 p p l e \r \n R u m b l e \r \n
[edit] head
head displays 10 lines from the head (top) of a given file
Examples:
$ head wordlist.dat
First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eighth
Ninth
Tenth
Display the top two lines:
$ head -2 wordlist.dat
First
Second
[edit] tail
tail displays last 10 lines of the file
Examples:
$ tail wordlist.dat
Ninety-First
Ninety-Second
Ninety-Third
Ninety-Fourth
Ninety-Fifth
Ninety-Sixth
Ninety-Seventh
Ninety-Eighth
Ninety-Ninth
One Hundredth
Display the bottom two lines:
$ tail -2 wordlist.dat
Ninety-Ninth
One Hundredth
Tips: The -f option displays the tail, then waits for and displays any new options to the file. This is
normally used to watch log files. (The next example has only three lines from tail, but the 80-column
terminal was too narrow, so the lines were broken into five lines.)
$ tail -f /var/log/messages
Apr 14 00:05:33 redserver sshd[1575]: Accepted password for rumbear from 24.52.1
45.23 port 33372 ssh2
Apr 14 00:05:34 redserver sshd[1594]: subsystem request for sftp
Apr 14 00:06:35 redserver sshd[1594]: Received disconnect from 242.122.35.47: 11
: Disconnect requested by Windows SSH Client.
UNIX/COMMANDS/FILE EDITING
Contents [hide]
1 pico
2 nano
3 zile
4 vi
5 joe
6 emacs
[edit] pico
Pico (text editor), a text editor originally designed for composing e-mail for Pine
[edit] nano
nano (text editor), a text editor originally designed to be a clone of Pico
[edit] zile
[edit] vi
For details see the Wikibooks Learning the vi editor.
[edit] joe
[edit] emacs
UNIX/COMMANDS/FILE COMPRESSION
Contents [hide]
1 gzip
2 gunzip
3 zcat
4 gzcat
5 tar
6 pax
7 bzip2
8 zip
9 compress
[edit] gzip
gzip compresses files. Each single file is compressed into a single file. The compressed file consists of
a GNU zip header and deflated data.
If given a file as an argument, gzip compresses the file, adds a ".gz" suffix, and deletes the original file.
With no arguments, gzip compresses the standard input and writes the compressed file to standard
output.
Compress the file named README. Creates README.gz and deletes README.
$ gzip README
Compress the file called README. The standard output (which is the compressed file) is redirected by
the shell to gzips/README.gz. Keeps README.
[edit] gunzip
gunzip uncompresses a file that was compressed with "gzip" or "compress". It tries to handle both the
GNU zip format of gzip and the older Unix compress format. It does this by recognizing the extension
(".gz" or ".Z" or several others) of a file.
$ gunzip README.gz
Write the uncompressed contents of README.gz to standard output. Pipe it into a pager for easy
reading of a compressed file.
[edit] zcat
zcat is same thing as uncompress -c, though on many systems it is actually same as "gzcat" and gunzip
-c.
[edit] gzcat
gzcat is same as gunzip -c which is gzip -dc.
[edit] tar
tar archives without compression.
An archive contains one or more files or directories. (If archiving multiple files, it might be better to
put them in one directory, so extracting will put the files into their own directory.)
Modes:
-f FILE name of archive - must specify unless using tape drive for archive
-v be verbose, list all files being archived/extracted
-z create/extract archive with gzip/gunzip
-j create/extract archive with bzip2/bunzip2
Examples:
Compress (gzip) and package (tar) the directory myfiles to create myfiles.tar.gz:
If you have access to a tape device or other backup medium, then you can use it instead of an archive
file. If the material to be archived exceeds the capacity of the backup medium, the program will prompt
the user to insert a new tape or diskette.
$ ls -F
from-stuff/
to-stuff/
As described in Running Linux, one can mirror everything from from-stuff to to-stuff this way:
$ tar cf - . | (cd ../to-stuff; tar xvf -)
Reference: Welsh, Matt, Matthias Kalle Dalheimer and Lar Kaufman (1999), Running Linux. Third
edition, O'Reilly and Associates.
[edit] pax
pax is like "tar" but with different command-line syntax. Because "pax" does not assume the tape
device, some prefer it to "tar".
$_ The File Compression module or this section of Wikibooks Guide to Unix Computing is a stub.
You can help Wikibooks by expanding it.
[edit] bzip2
bzip2 and bunzip2 are similar to "gzip"/"gunzip" but with a different compression method.
Compression is generally better but slower than "gzip". Decompression is somewhat fast.
$_ The File Compression module or this section of Wikibooks Guide to Unix Computing is a stub.
You can help Wikibooks by expanding it.
[edit] zip
zip is an archive which compresses the members individually. (Imagine gzip of every file before tar-ing
them, but with a different format.) The "zip" format is a common archiving file format used on
Microsoft Windows PCs.
$_ The File Compression module or this section of Wikibooks Guide to Unix Computing is a stub.
You can help Wikibooks by expanding it.
[edit] compress
compress is a compressed file format that is popular on UNIX systems. Files compressed with
compress will have a ".Z" extension appended to its name.
UNIX/COMMANDS/FILE ANALYSING
file
file displays the file type. To get the mimetype, use the -i option.
Examples
$ file Unix.txt
Unix.txt: ASCII text
$ file -i Unix.txt
Unix.txt: text/plain; charset=us-ascii
[edit] wc
wc tells you the number of lines, words and characters in a file.
Examples:
$ wc hello.txt
2 6 29 hello.txt
$ wc -l hello.txt
2 hello.txt
$ wc -w hello.txt
6 hello.txt
$ wc -c hello.txt
29 hello.txt
[edit] cksum
cksum gives you the CRC checksum of some files.
Checksums can be used to protect against accidental modifications to files: if the checksum has not
changed, then the file is probably undamaged. The default CRC checksum is not cryptographic.
Cryptographic checksums are those checksums which protect against both accidental modifications and
malicious modifications. Use these to verify that there is no trojan inserted into your file. The "md5"
algorithm is beginning to show weaknesses against attacks, so "sha1" is preferred.
Examples:
$ cksum /etc/passwd
3052342160 2119 /etc/passwd
Some "cksum" implementations provide other algorithms, such as "md5" and "sha1":
UNIX/COMMANDS/MULTIUSER COMMANDS
[edit] who
who gives information about the users logged into the machine. The information includes the user's
terminal, login time and the location they are connecting from.
Examples:
$ who
alice pts/0 Mar 23 08:05 (213.23.423.24)
bob pts/2 Apr 10 22:06 (domain.aol.com)
carol pts/3 Apr 10 18:34 (space.com)
Using "who" with two non-option words gives your username. On some systems, this gives your actual
username, and using "su" or "sudo" to switch user does not change this name.
$ who am i
puffy
On other systems, this gives more information:
$ who am i
puffy ttyp2 Oct 27 10:08
[edit] finger
finger finds out information about a user. If the user has created a .plan file in their home directory this
will also be displayed.
Examples:
$ finger alice
Login: alice Name: Alice Makemerry
Directory: /home/alice Shell: /bin/bash
On since Sat Apr 10 18:34 (BST) on pts/3 from ip.fakedomain.com
1 hour 25 minutes idle
Mail last read Sat Apr 10 23:57 2004 (BST)
No Plan.
[edit] su
su switch user
Examples:
user> su bob
Password:
bob>
Run a program as another user:
UNIX/COMMANDS/SELF INFORMATION
Contents [hide]
1 whoami
2 groups
3 id
4 tty
[edit] whoami
whoami tells you your current username.
Examples:
$ whoami
abicool
[edit] groups
groups states the groups the current user is a member of
Examples:
$ groups
wheel slocate www
[edit] id
id gives you the same information as the whoami and groups commands, but also includes the user id
(uid) and group id (gid) integers associated with the login.
Examples:
$ id
uid=3426(alice) gid=10(wheel)
groups=10(wheel),21(slocate),401(www)
[edit] tty
tty tells you the terminal device that is assigned to your interactive login. The tty represents your
console device, network connection ("ssh", ...), or terminal emulator process ("xterm", "konsole", ...).
Examples:
$ tty
/dev/pts/14
UNIX/COMMANDS/SYSTEM INFORMATION
Contents [hide]
1 uptime
2 uname
3 dmesg
4 free
5 vmstat
6 top
7 df
8 hostname
[edit] uptime
uptime tells you how long the computer has been running since its last reboot or power-off.
Example:
$ uptime
22:27:49 up 10:14, 2 users, load average: 0.03, 0.32, 0.28
[edit] uname
uname displays the system information such as hardware platform,system name and processor,
Operating System type.
Example:
$ uname -a
Linux DarkBox 2.4.27-1-k6 #1 Wed Apr 14 19:00:29 UTC 2004 i586 GNU/Linux
[edit] dmesg
dmesg display the messages from the kernel, since boot.
Example:
$ dmesg
Tips:
While a UNIX system is booting, usually a lot of messages flash on the console screen in rapid
succession; to view those messages after the system is booted, use the following command:
$ dmesg | less
Using a command option, dmesg can filter the kernel messages, based on priority. The '-n 1' arguments
will display only the panic messages:
$ dmesg -n 1
[edit] free
free display used and free memory
Example:
$ free
total used free shared buffers cached
Mem: 123260 119540 3720 0 8752 58096
-/+ buffers/cache: 52692 70568
Swap: 369452 63212 306240
Display in human readable form using MegaByte block sizes:
$ free -m
total used free shared buffers cached
Mem: 120 116 3 0 8 56
-/+ buffers/cache: 51 68
Swap: 360 61 299
Tips: Display system memory usage every 5 seconds, use Ctl+c to exit:
$ free -m -s 5
total used free shared buffers cached
Mem: 120 116 3 0 8 56
-/+ buffers/cache: 51 68
Swap: 360 61 299
total used free shared buffers cached
Mem: 120 116 3 0 8 55
-/+ buffers/cache: 52 68
Swap: 360 61 299
[edit] vmstat
vmstat displays a compact summary of overall system activity (processes, memory, and cpu
information).
Example:
$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
2 0 63108 4484 7432 56480 8 11 93 45 1110 622 41 11 48 0
Tips: Print out vmstat summaries every two seconds, for five iterations.
$ vmstat -n 2 5
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 63100 5172 7440 55892 8 10 90 44 1110 622 41 10 49 0
2 0 63100 5168 7440 55892 0 0 0 0 1120 559 32 3 65 0
1 0 63100 5160 7440 55892 0 0 0 0 1111 499 8 6 86 0
1 0 63100 5160 7440 55892 0 0 0 0 1113 505 12 3 85 0
1 0 63100 5168 7440 55892 0 0 0 0 1121 532 20 3 77 0
[edit] top
top displays system process in real time
Example:
$ top
Tasks: 50 total, 2 running, 45 sleeping, 2 stopped, 1 zombie
Cpu(s): 40.9% user, 10.5% system, 0.0% nice, 48.7% idle
Mem: 123260k total, 119508k used, 3752k free, 7420k buffers
Swap: 369452k total, 63036k used, 306416k free, 57212k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5340 arky 15 0 968 968 780 R 13.8 0.8 0:00.22 top
1408 root 6 -10 23712 6692 3252 S 1.5 5.4 3:39.58 [XFree86]
1 root 8 0 500 472 448 S 0.0 0.4 0:00.31 init [2]
2 root 9 0 0 0 0 S 0.0 0.0 0:01.60 [keventd]
3 root 19 19 0 0 0 S 0.0 0.0 0:00.02 [ksoftirqd_CPU0]
4 root 9 0 0 0 0 S 0.0 0.0 0:07.03 [kswapd]
5 root 9 0 0 0 0 S 0.0 0.0 0:00.00 [bdflush]
6 root 9 0 0 0 0 S 0.0 0.0 0:00.44 [kupdated]
154 root 9 0 0 0 0 S 0.0 0.0 0:00.00 [khubd]
562 root 9 0 604 588 508 S 0.0 0.5 0:05.09 /sbin/syslogd
565 root 9 0 1152 492 448 S 0.0 0.4 0:01.24 /sbin/klogd -c 3
.........
[edit] df
df reports the amount of free disk space available on each partition.
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/md0 5763508 207380 5263352 4% /
/dev/md1 78819376 13722288 61093296 19% /home
/dev/md4 23070564 4309572 17589056 20% /usr
/dev/md2 5763508 1757404 3713328 33% /var
/dev/md3 2877756 334740 2396832 13% /tmp
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/hda3 321952 32558 289394 11% /
/dev/hda2 67320 67 67253 1% /boot
/dev/mapper/vg00-home
372352 34227 338125 10% /home
/dev/mapper/vg00-tmp 242784 11649 231135 5% /tmp
/dev/mapper/vg00-usr 1821568 208669 1612899 12% /usr
/dev/mapper/vg00-var 1282560 75704 1206856 6% /var
Reports disk usage in human readable format with block-sizes in Kilo,Mega,Gigabytes. This option is
specific for Wikipedia:GNU version of df.
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda1 2.3G 2.1G 133M 95% /
tmpfs 61M 8.0K 61M 1% /dev/shm
/dev/hda2 2.0G 1.8G 113M 94% /usr
In some of Unix systems (SYS V family ie. HP-UX) df displays information in diffrent way:
$ df
/home (/dev/vg01/lvol2 ): 262478 blocks 2647709 i-nodes
/tmp (/dev/vg00/lvol5 ): 952696 blocks 125941 i-nodes
/usr (/dev/vg00/lvol6 ): 132842 blocks 17633 i-nodes
/var (/dev/vg00/lvol7 ): 131704 blocks 17288 i-nodes
/stand (/dev/vg00/lvol1 ): 47548 blocks 13390 i-nodes
/ (/dev/vg00/lvol3 ): 160772 blocks 21215 i-nodes
In such cases try to use bdf command.
[edit] hostname
hostname displays and set system host name
Example:
$ hostname
Darkstar
Display the IP address of the system:
$ hostname -i
61.95.196.52
Set the host name of the system to 'DarkHorse':
$ hostname DarkHorse
DarkHorse
Unix/Commands/Networking
NAME
Since kernel release 2.2 there are no explicit interface statistics for
alias interfaces anymore. The statistics printed for the original
address are shared with all alias addresses on the same device. If you
want per-address statistics you should add explicit accounting rules
for the address using the ipchains(8) or iptables(8) command.
Since net-tools 1.60-4 ifconfig is printing byte counters and human
readable counters with IEC 60027-2 units. So 1 KiB are 2^10 byte. Note,
the numbers are truncated to one decimal (which can by quite a large
error if you consider 0.1 PiB is 112.589.990.684.262 bytes :)
Interrupt problems with Ethernet device drivers fail with EAGAIN (SIOC‐
SIIFLAGS: Resource temporarily unavailable) it is most likely a inter‐
rupt conflict. See http://www.scyld.com/expert/irq-conflict.html for
more information.
Retrieved from "http://en.wikibooks.org/wiki/Guide_to_Unix/Commands/Networking"
ViewsModule Discussion Edit this page History Personal toolsLog in / create account Navigation
Main Page
Help
Recent changes
Wikijunior
Cookbook
Bookshelves
Featured Books
Donations
Community
Bulletin Board
Community Portal
Staff lounge
Module cleanup
Search
Toolbox
Search this book
What links here
Related changes
Upload file
Special pages
Printable version
Permanent link
UNIX/COMMANDS/PROCESS MANAGEMENT
Contents [hide]
1 nohup
2 ps
3 kill
4 pgrep
5 pidof
6 killall
[edit] nohup
nohup lets you run a program in a way which makes it ignore hangup signals. This can be used to make
a program continue running after a user has logged out. The output of the program is redirected from
the standard output to the file nohup.out.
Examples:
[edit] ps
ps displays a list of current processes and their properties.
Examples:
$ ps
PID TTY TIME CMD
17525 pts/0 00:00:00 su
17528 pts/0 00:00:00 bash
17590 pts/0 00:00:00 ps
All processes:
$ ps -A
[edit] kill
kill is used to send termination signals to processes.
Examples
$ kill -9 17525
see Guide to Unix/Commands/Process Management/Kill
[edit] pgrep
pgrep search and kill system processes
$ pgrep -f apache
5580
5581
5582
5583
5584
5585
Stop xterm program with 'pkill' program:
$ pkill -9 xterm
Tips: Display all the process of a user
$ pgrep -l -u arky
894 bash
895 bash
897 bash
898 bash
899 bash
1045 links
1396 startx
1407 xinit
1411 openbox
1412 xterm
1413 xfaces
1414 xsetroot
1415 emacs
[edit] pidof
pidof display Process ID (PID) of a task
$ pidof emacs
1415
[edit] killall
killall kill a process by name
Example:
$ killall xfaces
UNIX/COMMANDS/DEVICES
fuser
fuser tells you what process is using an indicated filesystem object (ordinary file, device, etc.)
$ fuser /dev/dsp
/dev/dsp: 8369
[edit] lsof
lsof lists all open files,is more detailed than fuser.
Example:
$ lsof /dev/dsp
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
mplayer 8406 alex 7w CHR 14,3 389 /dev/sound/dsp
Tips:
Using -i 4 option will report all programs currently using IPv4 network, it is useful for watching the
programs accessing the network and Internet resources.
$ lsof -i 4
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
btdownloa 2618 arky 3u IPv4 9524 TCP *:6886 (LISTEN)
btdownloa 2618 arky 6u IPv4 9544 TCP dsl-KK-229.53.101.203.ttel.net:1539->cpc1-leed3-3-
0-cust10.ldst.cable.ntl.com:59074 (ESTABLISHED)
[edit] fstat
fstat lists all open files.
The previous two commands (fuser and lsof) do not exist on all systems. The 4.3BSD-Tahoe system
introduced the "fstat" command that is found on many *BSD systems. Unlike the previous two
commands, it seems not to know the exact path of each file, but only what filesystem it is on?
$ fstat | less
Get the process ID of the running Bourne shell and then list the files it opened.
$ echo $$
5283
$ fstat -p 5283
USER CMD PID FD MOUNT INUM MODE R/W DV|SZ
...
The init process always has ID of 1. List its open files. In this example it only opened one file
somewhere on the / filesystem.
$ fstat -v -p 1
USER CMD PID FD MOUNT INUM MODE R/W DV|SZ
root init 1 wd / 2 drwxr-xr-x r 512
UNIX/COMMANDS/KERNEL COMMANDS
[edit] lsmod
lsmod lists the modules loaded by the Linux kernel.
[edit] modprobe
modprobe loads a Linux kernel module. You can specify only the name of the module, and modprobe
will load it from the correct location and also load any dependent modules.
Many modules load automatically. For example, Linux loads a USB keyboard module when a USB
keyboard is attached. It also loads the base USB modules as dependencies. Some modules must be
loaded manually, and "modprobe" is the easiest way to do this.
[edit] sysctl
sysctl sets a parameter to change the behavior of the kernel. The available parameters vary by kernel,
so check the man page for sysctl in your distribution.
Examples:
$ sysctl vm.swapencrypt.enable
vm.swapencrypt.enable=0
Root can set the parameter to 1.
$ sysctl vm.swapencrypt.enable=1
vm.swapencrypt.enable: 0 -> 1
UNIX/COMMANDS/MISCELLANEOUS
Contents [hide]
1 sync
2 echo
3 cal
4 date
5 time
6 from
7 mail
8 clear
9 PS1
[edit] sync
sync write memory buffers to disk
$ sync
Tips:
It is always good to type sync a couple of times, one the important function of sync is to update your
superblock information.
The sync calls sync Unix system call and exits with success code '0' or '1' if it fails. These exit codes
stored in $? variable.
$ sync
$ echo $?
0
The above example shows that sync was successful.
[edit] echo
echo outputs its parameters to the standard output.
Examples:
$ echo $EDITOR
emacs
Check the parameters passed in the previous command:
$ ls -l
.........
$ echo $_
-l
Check the current parent process:
$ echo $0
bash
Check the exit code of the last command:
$ echo $?
0
Create a empty file (same as touch /tmp/newfile):
[edit] cal
cal displays a calender for the current month. If the command is followed by a date (a month or a year)
it will return a calender for that period.
Examples:
$ cal
April 2004
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
$ cal 01 2007
January 2007
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tips: The Gregorian Calendar was adopted in the British Empire in 1752. The 2nd day of September
1752 was immediately followed by the 14th day of September, as shown by the example below.
$ cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
[edit] date
date displays the current date and time.
Example:
$ date
Mon Jun 26 12:34:56 CDT 2006
[edit] time
time time a program
Example:
real 0m1.818s
user 0m0.770s
sys 0m0.210s
[edit] from
from display the names of those who sent you mail recently
Example:
$ from
From andy@box.po Sat Feb 05 08:52:37 2005
From andy@box.po Sat Feb 05 08:53:52 2005
Count the number of mail in your mailbox
$ from -c
There are 2 messages in your incoming mailbox.
[edit] mail
mail allows you to read and write emails.
Example:
$ mail
No mail for user.
$ mail user2
Subject: What's up?
Hi user2, you can delete this rubbish by pressing 'd'.
Cc: user
Tips: Note that you need to press enter then ctrl+d to confirm.
$ mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/user": 1 message 1 new
>N 1 user@unix.com Tue Jun 27 12:34 16/674 "What's up?"
&
Tips: Press enter to read.
[edit] clear
clear clears the screen.
Example:
[edit] PS1
PS1 is an environment variable which defines the shell prompt. If not defined, the prompt defaults to
"$"
Example:
$ PS1='yes? '
yes? PS1='$ '
$
A more complicated example: