UNIX
UNIX systems usage, administration and development information.

Guests are welcome to view our materials. To subscribe, edit, view raw markup, etc., you'll need to register for an account. Accounts are free (and will always be free) - your involvement helps us directly and indirectly (by demonstrating that our work matters to our funders...) StartingPoints has more info.
UNIX

Unix Commands

A quick reference listing of commonly used UNIX commands & command line utilities.

alias

[edit]

list and set your aliases
list your current aliases

$ alias
set your alias for grep to use color (for this bash session only)
$ alias grep='grep --color'
  • to unset your alias use unalias
  • to permanently set your alias for a command, you will need to insert it into your .bashrc file

[top]


apropos

[edit]

searches in the man pages
search the man pages for md5

apropos md5

[top]


awk

[edit]

powerful text processing (but maybe you should just use perl or python instead?)

[top]


bzip2

[edit]

file compression

  • compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding.
  • supposedly has better compression, but i haven't test it yet.

[top]


cal

[edit]

cal will print a calendar for you, either the current month or the entire year. Note that in the examples below, you can't see it, but on a terminal the current date will show up via reverse background/text colors.

[user@dayz ~]$ cal
   December 2008    
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

Day of the year is also available via -j (i.e. Julian dates)

[user@dayz ~]$ cal -j
       December 2008       
Sun Mon Tue Wed Thu Fri Sat
    336 337 338 339 340 341
342 343 344 345 346 347 348
349 350 351 352 353 354 355
356 357 358 359 360 361 362
363 364 365 366

The entire year is available via -y

[user@dayz ~]$ cal -y
                               2008                                

       January               February                 March        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2                      1
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
                                              30 31
        April                   May                   June         
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
27 28 29 30            25 26 27 28 29 30 31   29 30

        July                  August                September      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2       1  2  3  4  5  6
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    7  8  9 10 11 12 13
13 14 15 16 17 18 19   10 11 12 13 14 15 16   14 15 16 17 18 19 20
20 21 22 23 24 25 26   17 18 19 20 21 22 23   21 22 23 24 25 26 27
27 28 29 30 31         24 25 26 27 28 29 30   28 29 30
                       31
       October               November               December       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          1  2  3  4                      1       1  2  3  4  5  6
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    7  8  9 10 11 12 13
12 13 14 15 16 17 18    9 10 11 12 13 14 15   14 15 16 17 18 19 20
19 20 21 22 23 24 25   16 17 18 19 20 21 22   21 22 23 24 25 26 27
26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31
                       30

Look at the man page for your system to see if more options are available.

[top]


cd

[edit]

change to your home directory

 $ cd 
change to directory /home/ifi
 $ cd ~ifi 
change to the docs directory in your home directory
 $ cd ~/docs 

[top]


chown

[edit]

change ownership of a file and/or direcotry
To change all files recursively within a directory:

sudo chown -R httpd:httpd /usr/local/src/twiki

[top]


cp

[edit]

copy files and/or directories
Copy all files from a directory and subdirectories contained within while preserving set permission attributes:

% cp -rp /staff2/dallen/sample_webapp_1 sample_webapp

[top]


chsh

[edit]

change shell, limited to shells listed in /etc/shells

[top]


cut

[edit]

text removal, useful for delimited file data

[top]


df

[edit]

report file system disk space usage, use -h for human readable output

 $ df -h 

[top]


diff

[edit]

check the differences between two files
also see:

  • sdiff
  • vimdiff
  • gvimdiff
  • patch

[top]


dig

[edit]

dig - DNS lookup utility

[top]


du

[edit]

gives disk usage information (i.e. filesize)

% du -sk filename
% du -sk directory

[top]


find

[edit]

  • searches entire file system for a file
  • slower than locate, but more powerful since you can search for attributes other than name

example: find a file based on timestamp (finds files that have been accessed in the last 4 minutes)

 $ find /var/www/twiki -amin -4 -print 

example: find all files on the system named "mysql" (exclude directories) and don't print errors to screen

 $ find / -name "mysql" -type f -print 2>/dev/null

[top]


grep

[edit]

string searches within files

$grep spider /etc/httpd/logs/access_logs


useful to pipe output from commands to grep to search for what you really want to see
ps -ef | grep firefox


search recursively for a text string in all files within a directory (and all subdirectories) and provide the line number the text string occurs on within that file
sudo grep -Rn mod_perl /etc/httpd/conf.d


to run without error messages of non-existent files use:
sudo grep -Rns mod_perl /etc/httpd/conf.d


same as above, except just outputs name of a file that matches the search criteria
sudo grep -Rls mod_perl /etc/httpd/conf.d


to ignore case, use -i

[top]


htpasswd

[edit]

htpasswd is used to create and update the flat-files used to store usernames and password for basic authentication of HTTP users. (1)

[edit resources]

How to use htpasswd by httpd.apache.org

add user2 to the passed password file .htpasswd:

htpasswd .htpasswd user2

[top]


last

[edit]

list the last few users who have logged on successfully

  • looks through the file /etc/var/wtmp
  • list IP number:
    last -i
  • list host name in last column:
    last -a

[top]


lastb

[edit]

list the last few bad login attempts

  • looks through the file /etc/var/btmp
  • list IP number:
    lastb -i
  • list host name in last column:
    lastb -a

[top]


locate

[edit]

locate files by searching for names in the updatedb database

  • faster than using find
  • you may want to run updatedb before you run locate to update the updatedb database, especially if you've added or removed files recently

$ locate FILENAME
$ locate httpd.conf

[top]


ls

[edit]

list inodes of files

 $ ls -i 

[top]


lynx

[edit]

lynx

[top]


md5sum

[edit]

md5sum

[top]


md5

[edit]

calculates the md5 sum of a file or directory
(Mac OS X) it may take a while, especially on big files, so be patient

$ md5 [filename]
also see openssl md5.

[top]


mkdir

[edit]

make parent directories as needed

 mkdir -p /home/alice/books/fiction 

make a directory with stated permissions
 mkdir -m 711 /home/alice/public_html 

[top]


openssl md5

[edit]

calculates the md5 sum of a file or directory
(on Mac OS X) open a terminal window, type openssl md5 and then drag and drop the file you want analyzed.

$ openssl md5 [path/filename]

[top]


package-cleanup

[edit]

package-cleanup - program for cleaning up the locally-installed RPMs.

some examples (from man page)...

List all dependency problems:

package-cleanup --problems


List all packages that are not in any Yum repository:

package-cleanup --orphans

see also rpm and yum.

[top]


passwd

[edit]

change your password

 $ passwd 


[top]


ping

[edit]

send ICMP ECHO_REQUEST to network hosts (ctrl+C to quit pinging)

 $ ping yahoo.com 


ping only once (change 1 to X for X pings)
 $ ping -vc1 google.com 


-v is for verbose output, but i haven't seen a difference

[top]


ps

[edit]

shows information about processes running

ps -ef | grep firefox

  • -e select all process (identical to -A )
  • -f full format

[top]


pstree

[edit]

produces a tree of processes, providing parent/child relationships at a glance

print process tree with PID's (alphabetical order)

 $ pstree -p 


print process tree with PID's (numerical order)
 $ pstree -pn 


print process tree with PID's and highlight current process
 $ pstree -ph 


print process tree with PID's, highlight current process, and order numerically by PID
 $ pstree -phn 


example of pstree output:
$pstree -p 
init(1)─┬─acpid(1984)
        ├─auditd(20764)─┬─audispd(20766)───{audispd}(20767)
        │               └─{auditd}(20765)
        ├─crond(2176)
        ├─dbus-daemon(1949)
        ├─denyhosts.py(11836)
        ├─httpd(624)─┬─httpd(2973)
        │            ├─httpd(7785)
        │            ├─httpd(9278)
        │            ├─httpd(14645)
        │            ├─httpd(14787)
        │            ├─httpd(26891)
        │            ├─httpd(28084)
        │            ├─httpd(28684)
        │            ├─httpd(29070)
        │            └─httpd(29071)
        ├─irqbalance(1807)
        ├─master(974)─┬─pickup(28545)
        │             └─qmgr(977)
        ├─mingetty(2267)
        ├─mingetty(2268)
        ├─mingetty(2269)
        ├─mingetty(2270)
        ├─mingetty(2271)
        ├─mingetty(2272)
        ├─ntpd(10416)
        ├─python(892)─┬─python(895)
        │             ├─python(896)
        │             ├─python(897)
        │             ├─python(898)
        │             ├─python(899)
        │             ├─python(900)
        │             ├─python(901)
        │             └─python(902)
        ├─rklogd(11154)
        ├─rsyslogd(11150)───{rsyslogd}(11874)
        ├─sshd(8412)───sshd(25778)───sshd(25780)───bash(25781)───pstree(32007)
        └─udevd(616)

[top]


rpm

[edit]

rpm - redhat package manager

can refer to a few related things..

  1. a unix command used to manage (build, install, query, verify, update, erase) software packages
  2. the rpm file format
  3. software packaged in the rpm file format. (i.e. fedora-release-*.noarch.rpm )

packages...

  • consist of an archive of files and metadata used to install and erase archive files
  • meta-data includes helper scripts, file attributes, and descriptive information about the package
  • binary packages - used to encapsulate software to be installed
  • source packages - contain source code and instructions necessary to produce binary packages

[top]


rsync

[edit]

remotely synchronize files and directories

[top]


scp

[edit]

transfer files securely between two computers using SSH

example copying a file foo.tar.gz to a directory ./bar in your home directory on unix.edu:

scp foo.tar.gz username@unix.edu:./bar

[top]


sed

[edit]

stream editor (edit streams of text)

[top]


sftp

[edit]

secure ftp

[top]


sort

[edit]

default is alpha-numeric order

$ sort
sort in reverse alpha-numeric order
$ sort -r
you can sort according to the nth column as a key
$ sort -k n,n
here we sort the output of ps -e (ps normally sorts according to the pid value) according to alpha-numeric order of the CMD name
$ ps -e | sort -k 4,4

[top]


ssh

[edit]

secure remote login program

[top]


strings

[edit]

lists all ASCII text in a file (useful for looking at binary file)
search an executable file for the ascii text "error":

$ strings /bin/ps | grep error 

[top]


su

[edit]

become the root user
use - to enable the root user's environment:

$ su -

to become another user:
$ su - username

[top]


sudo

[edit]

  • configured in /etc/sudoers
  • if you can't use sudo, see Sudo and Group Wheel discussion in UnixCommonTasks
  • every use of sudo is logged in /var/log/secure

[top]


svn

[edit]

subversion - version control system

for detailed info, manuals, and tutorials (check your svn version number first!) see: http://svnbook.red-bean.com/

svn version info:

svn --version


help info on a command

$ svn help command


import your current project (not already in svn) into svn:

  • $ svn import myProj path -m "Comments on your project" 
    • EXAMPLE import:
      • myProj is the directory that contains my project files:
        ./helloWorld
      • path is the path to the SVN server. Note that it can be a file path on the server you are working on or a URL (depends on your SVN setup)...
        • Here is an example path to the svn server where 10000 is a port for the SVN server in this example:
          http://server.csueastbay.edu:10000/svn/lab/ 
        • If you have web access to the svn, you can see the directory structure and this can help you create an appropriate path for you new project. In this example, our path will be:
          http://server.csueastbay.edu:10000/svn/lab/mySubPath/helloWorld 
      • so to import the project in this example we type and receive the following:

$ svn import ./helloWorld http://server.csueastbay.edu:10000/svn/lab/mySubPath/helloWorld -m "initial import of helloWorld"
Adding         helloWorld/hello.cpp

Committed revision 2601.
  • you can check the contents of directory myProj under the directory this/project in the repository:
    $ svn list file:///var/svn/newrepos/this/project
  • NOTE: after import, you do NOT yet have a working copy! Use svn checkout to checkout a working copy.

initial checkout of already existing project:

svn checkout http://svn.this.edu/repos/svn/trunk


basic work cycle:

  • update your working copy
    svn update


  • make changes:
    • svn add
    • svn delete
    • svn copy
    • svn move


  • examine your changes
    • svn status
    • svn diff
    • svn revert


  • merge others' changes into your working copy
    • svn update
      • for each updated item, the letter preceding the file means the following:
            A  Added
            D  Deleted
            U  Updated
            C  Conflict
            G  Merged
        
    • svn resolved


  • commit your changes
    • svn commit -m "message describing the changes in this commit"


[top]


tail

[edit]

Very useful for debugging!!! (see tail -f below)

continuously output (i.e. monitor) the last 10 lines of a file:

tail -f foo.txt
  • Use ctrl-c to quit

continuously output (i.e. monitor) the last 4 lines of a file:
tail -fn 4 foo.txt
  • Use ctrl-c to quit

output the last 10 lines of a file:
tail foo.txt


output the last 4 lines of a file:
tail -n 4 foo.txt


[top]


tailf

[edit]

like tail -f but it does not access the file when it is not growing

[top]


tar / gzip

[edit]

create a tar zipped archive:

% tar -czvf archive.tar.gz file1 file2 dir/

extract an archive:
% tar -xzvf archive.tar.gz

list contents of the archive file:
% tar -tzvf archive.tar.gz

[top]


telinit

[edit]

telinit - change runlevel

[top]


tr

[edit]

simple character replacement and/or removal

[top]


uname

[edit]

prints system info

  • print all info:
    uname -a
    example:
    [user@acc ~]$ uname -a
    Linux acc.csueastbay.edu 2.6.25.14-69.fc8 #1 SMP Mon Aug 4 14:20:24 EDT 2008 i686 i686 i386 GNU/Linux
  • print kernel release:
    uname -r
    example:
    [user@acc ~]$ uname -r
    2.6.25.14-69.fc8

[top]


useradd

[edit]

add a new user

useradd jane


then you can change password...
passwd jane


to reset a password for a user who forgot, login as root and then try...
passwd username


[top]


w

[edit]

shows uptime, who is on the server, what they are doing and system load averages for the last 1, 5, and 15 minutes.

example:

$ w
 18:27:19 up 150 days,  4:07,  2 users,  load average: 0.09, 0.07, 0.02
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
joeyn   pts/0    adsl-22-111-222- 18:18    0.00s  0.00s  0.00s w
joeyn   pts/1    adsl-22-111-207- 18:19   14.00s  0.00s  0.00s man w

[top]


wc

[edit]

word counting

pass a file, and receive the number of lines, words, and bytes in a file:

$ wc myfile


example:
$ wc README
  48  290 2343 README


to count the number results from some command (i.e. grep), pipe the output to wc and use -l option to only provide the number of new lines
$ grep rob /var/log/secure | wc -l 

[top]


wget

[edit]

You can use wget to download files from the web

wget http://www.apache.org/dist/httpd/apache_1.3.41.tar.gz

If you are running Mac OS X, you probably don't have wget installed. If you have gcc installed then you should be able install wget as follows:

  • download the latest version from gnu.org here
  • extract the contents of the tar archive: tar -xzvf wget-latest.tar.gz
  • go inside the newly created directory
  • You can install wget as follows, but you probably should take a look at the README and INSTALL and any other files that catch your eye
    • ./configure
    • make
    • sudo make install

[top]


whois

[edit]

client for whois service

  • can query a domain name:
    whois csueastbay.edu
  • can query an IP address:
    whois 134.154.1.5
for more info, see the man page for your particular distribution

[top]


xargs

[edit]

build and execute command lines from standard input

[top]


yum

[edit]

interactive, automated update program

[edit resources]

[top]


[see also WebHome]



-- RobertSajan - 24 Apr 2008

Notes

1 : httpd.apache.org


r27 - 11 Dec 2008 - 00:15:54 - RobertSajan
Guests are welcome to view our materials. To subscribe, edit, view raw markup, etc., you'll need to register for an account. Accounts are free (and will always be free) - your involvement helps us directly and indirectly (by demonstrating that our work matters to our funders...) StartingPoints has more info.
This site is powered by the TWiki collaboration platformCopyright 1999-2009 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Ahatwiki? Send feedback Syndicate this site RSSATOM