Top

A Linux User Reference

Search tips
  • search ignores words that are less than 4 characters in length
  • searches are case insensitve
  • if a search does not return anything try it in Boolean mode then Query expansion mode by checking the appropriate radio button e.g. searching for 'cron' in just the Administration category returns nothing - presumably because the 50% threshold is reached. Boolean mode ignores this threshold so a search for 'cron' returns several hits
  • in Boolean mode preceding a word with a '+' means the result must include that word, a '-' means it must not
  • in Boolean mode '+crontab -anacron' means match articles about crontab that DO NOT mention anacron
  • to match a phrase e.g. 'manage system' check the Boolean mode radio button and enclose the phrase in quotes "some phrase ..."
  • in Query expansion mode the search context is expanded beyond the keywords you entered - relevancy of hits may well be degraded

FILES AND DIRECTORIES

Commands

  • Symbolic links

    There are two types of link.

    Hard Link

    • Another directory entry for an existing file. The directory entries have different names but point to the same inode (same data and file properties).

    • When deleting a hard link only the inode reference to it is deleted/decremented. The original file is only deleted when the inode link count = 0. When creating a hard link the target must exist.

      Limitations:

      • Hard links can only be created in the same filesystem since they all use the same inode.
      • Hard links cannot point to directories.

    Symbolic or Soft Link

    • Is a pointer to another file. This file can be local, remote or on another filesystem.

    • Can point to a non-existent file in which case they are known as broken or stale links.

    • Deleting a soft link deletes the link, the original/physical file remains untouched.

    • A link itself has no file permissions, the permissions of the file it points to are used.

  • Create a symbolic link
    /bin/ln

    The default action is to create a hard link.

    ln [options] [-T] target link-name
    ln [options] target
    ln [options] target directory
    ln [options] -t <directory> target
    
    Common options:
     -s                     Create a symbolic link
     -b                     If link-name exists backup before creating link
     -T                     Treat link-name as a normal file - not a directory
     -t <dir>               Directory in which to create the links
     -f                     If link-name exists delete it and create link
    

    View a file's link status

    # stat file1 | grep Links
    Device: 808h/2056d   Inode: 115458   Links: 1        (file1 exists on its own - link count=1)
    

    Create a hard link from file1 calling it file3

    # ln file1 file3
    # stat file3 | grep Links
    Device: 808h/2056d   Inode: 115458   Links: 2        (file3, file1 use the same inode, link count incremented)
    
    # stat file1 | grep Links
    Device: 808h/2056d   Inode: 115458   Links: 2        (file1's inode)
    
    # ls -al file1 file3
    -rw-r--r-- 2 mark mark 0 2009-03-31 14:41 file1
    -rw-r--r-- 2 mark mark 0 2009-03-31 14:41 file3
    
    # echo "12345678" > file3                            (Changing data in file3/link changes data in file1)
    # ls -al file1 file3
    -rw-r--r-- 2 mark mark 9 2009-03-31 15:52 file1
    -rw-r--r-- 2 mark mark 9 2009-03-31 15:52 file3      
    
    # rm file3                                           (file1 still remains after file3/link is deleted)
    # ls -al file[13]
    -rw-r--r-- 1 mark mark 9 2009-03-31 15:52 file1
    

    Create a link directory and a link for file1 in directory

    # mkdir linkdir
    # ln -t linkdir file1
    

    Create a soft link file2 pointing to file1

    # ln -s file1 file2
    $ stat file1
      File: `file1'
      Size: 9            Blocks: 8          IO Block: 4096   regular file
    Device: 808h/2056d   Inode: 115458   Links: 2
    Access: (0644/-rw-r--r--)  Uid: ( 1000/    mark)   Gid: ( 1000/    mark)
    .....
    
    $ stat file2                    (Note the symbolic link has its own inode)
      File: `file2' -> `file1'
      Size: 5            Blocks: 0          IO Block: 4096   symbolic link
    Device: 808h/2056d   Inode: 115569   Links: 1
    Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
    .....
    
  • Copy files and directories
    /bin/cp

    Command usage

    cp [options] [-T] source dest
    cp [options] source ... directory
    cp [options] -t directory source ...
    
    Common options:
     -a | --archive                    Same as -dpR
     -d                                Same as --no-dereference --preserve=links
     -i | --interactive                Prompt before overwrite
     -l | --link                       Link files instead of copying
     -L | --dereference                Always follow symbolic links in source
     -P | --no-dereference             Never follow symbolic links in source
     -p                                Same as --preserve=mode,ownership,timestamps
     -R | -r | --recursive             Copy directories recursively
     -s | --symbolic-link              Make symbolic links instead of copying
     -t | --target-directory=DIR       Copy all source arguments into directory
     -T | --no-target-directory        Treat dest as a normal file
     -u | --update                     Copy if source file is newer or destination
                                       file is missing
    

    Copy all .php files to directory test

    # cp -p *.php test
    

    Copy files file1,file2, file3 to directory test

    # cp file1 file2 file3 test
                                        (or)
    # cp -t test file1 file2 file3
    

    Create symbolic links in /home/test1

    # cp -t /home/test1 -s /user/file1 /user/file2
    

    Recursively copy directories

    # cp -R test test1
    

    Copies all files including '.' special files.

  • Preserving symbolic links
    /bin/cp

    Many commands have options to preserve symbolic links e.g.

    $ cp -d
    $ cp --no-dereference --preserve=links
    

    Where '-P | --no-dereference' means never follow a link to its source.

    '--preserve=links' means keep the link attribute.

    • When these flags are used it means that the link i.e. it's inode and NOT the file that it is pointing to, is copied.
    • If these flags are not used the file it points to is copied resulting in multiple copies of the source file.
    • Just using '-p | --preserve' is not sufficient as the default preserve modes are: mode,ownership,timestamps.

    Syntax to preserve a link when copying

    $ cp -P --preserve=links <symlink> <destination>
                                                      (or)
    $ cp -d <symlink> <destination>                        
    
  • List directory contents
    /bin/ls

    Command usage

    ls [OPTION] ... [FILE] ...
    
    Some common options:
     -a | --all                 Do not ignore entries starting with .
     -A | --almost-all          Do not list implied . and ..
     -c                         With -lt, sort by and show ctime (last modification time)
                                With -l, show ctime and sort by name else sort by ctime
     -C                         List entries by columns
     -d | --directory           List directory entries instead of contents
     -h | --human-readable      With -l print sizes in human readable format (1K 234M 2G)
     -i | --inode               Print the index number of each file
     -l                         Use a long listing format
     -R | --recursive           List subdirectories recursively
     -s | --size                Print the size of each file, in blocks
     -S                         Sort by file size
     -t                         Sort by modification time
    

    Some examples

    $ ls -al | head -n 5
    total 6016
    drwxr-xr-x 63 mark mark   12288 2010-01-22 16:57 .
    drwxr-xr-x  4 root root    4096 2009-10-27 16:08 ..
    -rw-r--r--  1 mark mark     290 2009-12-26 18:11 .alienblaster_highscore
    drwxr-xr-x  3 mark mark    4096 2009-11-04 13:36 Aptana Studio Workspace
    
    $ ls -AlC | head -n 5
    .alienblaster_highscore  dbview-py-28.gif   .gnash
    Aptana Studio Workspace  dbview-py-28.png   .gnome2
    .aptitude        dbview-py-29.gif   .gnome2_private
    .asc             dbview-py-29.png   .gnupg
    .bash_history        dbview-py-2.gif    .gstreamer-0.10
    
    $ touch fred
    $ ls -Alt | head -n 5
    total 6000
    -rw-r--r--  1 mark mark       0 2010-01-22 17:55 fred
    -rw-r--r--  1 mark mark    4901 2010-01-22 17:26 .xsession-errors
    drwx------  5 mark mark    4096 2010-01-22 16:04 .gconf
    drwx------  2 mark mark    4096 2010-01-22 16:04 .gconfd
    
  • Make directory(s)
    /bin/mkdir

    Command usage

    mkdir [options] directory
    
    Common options:
     -m | --mode=MODE             Set file mode (as in chmod), not a=rwx - umask
     -p | - --parents             Create parent directories
    

    Create a new directory and set access permissions

    $ mkdir -m 640 private
    

    Create the full directory tree if they do not exist

    $ mkdir -p ~/dir1/dir2/dir3
    

    Create three same level directories

    $ mkdir dir{1,2,3}
    $ ls dir[123]
    dir1:
    dir2:
    dir3:
    $ rmdir dir{1,2,3}
    
  • Remove empty directories
    /bin/rmdir

    Command usage

    rmdir [OPTION]...  DIRECTORY...
    
    Options:
     --ignore-fail-on-non-empty       Ignore each failure that is solely because
                                      a directory is non-empty
     -p | --parents                   Remove DIRECTORY and its ancestors
                                      e.g. rmdir -p a/b/c is similar to 
                                           rmdir a/b/c a/b a
    

    Remove nested directories

    $ mkdir -p test/test1/test2
    $ tree -d test*
    test
    `-- test1
        `-- test2
    
    $ rmdir -p test/test1/test2
    
    $ tree -d test
    test [error opening dir]
    0 directories
    

    Delete same level directories dir1, dir2 and dir3

    $ rmdir dir{1,2,3}
    
  • Move files and directories
    /bin/mv

    Command usage

    mv [options] [-T] source dest
    mv [options] source ... directory
    mv [options] -t directory source ...
    
    Common options:
     -b                                Like --backup but does not accept an argument
     -f | --force                      Do not prompt before overwriting
     -i | --interactive                Prompt before overwrite
     -t | --target-directory=DIR       Move all source arguments into directory
     -T | --no-target-directory        Treat dest as a normal file
     -u | --update                     Move if source file is newer or destination
                                       file is missing
    

    Move/rename file1 to file2

    $ mv file1 file2
    

    Move file1 and file2 to directory dir1

    $ mv file1 file2 dir1
                                       (or)
    $ mv -t dir1 file1 file2           
    

    Move/rename directory dir1 to directory dir2

    $ mv dir1 dir2
                                       (or)
    $ mv -t dir2 dir1                  
    
  • Remove files and directories
    /bin/rm

    Command usage

    rm [options] file ...
    
    Common options:
     -f | --force                   Ignore non-existent files, never prompt
     -i                             Prompt before every removal
     -r | -R | --recursive          Remove directories and their contents recursively
    

    Remove the directory private along with all its contents

    $ rm -r private
    

    Prompt before removing each file

    $ rm -i file1 file2 file3
    
  • Update file access and modification times
    /usr/bin/touch

    Creates an empty file if does not exist.

    touch [options] file ...
    
    Common options:
     -a                             Change only the access time
     -m                             Change only the modification time
     -r | --reference=FILE          Use this file's times instead of current time
     -t STAMP                       Use [[CC]YY]MMDDhhmm[.ss] instead of current time
    

    Create files if not exist, set access time to current time

    $ touch -a file1 file2
    

    Change modification time on file2 to 18/06/08, 1715 hrs

    $ touch -t 0806181715 -m file2
    
  • Determine a file's type
    /usr/bin/file

    See man pages for full explanation.

    file    [-bchikLnNprsvz] [options ...] file
    file -C [-m magicfile]
    file    [--help]
    

    Display the file type

    $ file dbview-py-12.png
    dbview-py-12.png: PNG image, 1024 x 768, 8-bit/color RGB, non-interlaced
    
    $ file chklinks.py
    chklinks.py: a python script text executable
    
    $ file $(which xargs)
    /usr/bin/xargs: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), \
     dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
    
    $ file ~/linuxref
    /home/mark/linuxref: directory
    
  • Determine how a file will be interpreted as a command
    type
    • Bash builtin command.
    • With no options, indicate how each name would be interpreted if used as a command name.
    type [-aftpP] name [name ...]
    
    Options:
     -a              Prints all of the places that contain an executable named name
     -f              Suppresses shell function lookup, as with the command  builtin
     -t              Prints type - alias, keyword, function, builtin or file
     -p              Returns the name of the disk file that would be executed
     -P              Forces a PATH search for each name
    

    Some examples

    $ type chmod
    chmod is /bin/chmod
    
    $ type -t ./env_t.sh
    file
    
    $ type echo
    echo is a shell builtin
    
    $ type -t chmod
    file
    
    $ type -p chmod
    /bin/chmod