GNU
GNU
Linux
Linux
Welcome to my Linux Documentation Centre. Here you'll find links to most of the Linux software that I use on a regular basis. Most of the pages are fairly sparse, consisting primarily of a list of useful links along with brief descriptions of them. The two images above link to pages about the GNU software project and the history of Linux.
Top Ten (11) Linux Commands*
  1. find
  2. rsync
  3. sed
  4. foreach
  5. pushd/popd
  6. awk
  7. paste
  8. split
  9. !$
  10. alias
  11. wget
* This is a list of relatively unknown linux commands and does not include some useful commands like grep, diff, more, cat, etc. that most linux users probably already know about.

  1. find
    This is a very useful command not only for finding files but for executing commands on them as well. A basic example looks something like this:
    find <start_directory> -name lostfile.txt
    This will search all directories below start_directory for file lostfile.txt and, if found, print its location relative to start_directory.
    To find all files ending in ".txt" in the current directory and all directories below, and execute an ls -dils on each one, try:
    find . -name "*.txt" -ls
    Let's say you've completely forgotten the name of a file, and know only that it contains the word "loststring":
    find . | xargs grep loststring
    Note that xargs, another very useful command, takes the output produced by find and executes grep on it.
    Here is a final, more complicated, but very powerful example:
    find . -type d -exec chmod a+rx {} \; -o -type f -exec chmod a+r {} \;
    This command does the following:
    • find all files and directories below the current directory
    • check if what was found is a directory (-type d)
    • if yes, chmod a+x (-exec chmod a+x {} \; ) [the {} means insert the name of what was found here, \; means the end of the command to be executed ]
    • if no ( -o ), check if what was found is a normal file (-type f) and if, so, chmod a+r (-exec chmod a+r {} \; )
    This is extremely useful for descending a directory tree and "opening" all directories and contents for read access by other users.

  2. rsync
    Indispensible for making incremental backups of your files. To backup my email directory to from a remote host to my home computer, I run
    rsync -avz <hostname>:<mail_directory> <target_directory>
    This will compare the contents of mail_directory on remote host hostname with the directory of the same name in target_directory and transfer all files and directories in mail_directory that have been modified more recently than the corresponding versions in target_directory. The -a flag causes rsync to preserve the timestamps, permissions, etc. of all transferred files, and the -z specifies the use of compression during transfer. Note that using rsync in -a "archive" mode will copy only symbolic links and not the target files referenced by symbolic links. To override this, use the -L option to rsync.

  3. sed
    A.k.a. "stream editor" is a throwback to the days before full screen text editors like vi and emacs, but is still very useful for quick one-liner text edits that can be executed directly from the command line. The following sed command will replace all occurences of "oldstring" with "newstring" in file "oldfile.txt" save the results in "newfile.txt".
    sed s/oldstring/newstring/g oldfile.txt > newfile.txt
  4. foreach
    This command is available only in C shell, but is yet another reason to use C shell over Bash or Korn shells. The foreach command implements a loop over all elements in a list of items; it can be used to execute a command on a list of files, for example.
    foreach foo (*.txt)
    foreach ? sed s/oldstring/newstring/g $foo > tmp.txt; mv tmp.txt $foo
    end
    In this example, foreach is combined with sed to replace all instances of "oldstring" with "newstring" in all files ending in ".txt" - much faster than opening each file in a text editor and performing a global search and replace on each one!

  5. pushd/popd
    An underused pair of commands that is a timesaving alternative to the standard cd command for changing directories. To change directories, use
    pushd <dir_name>
    This adds the current directory to the top of a directory stack. To return to that directory, use
    popd
    Trust me, this will save you many hours of typing out long directory pathnames!

  6. awk
    Awk is a programming language that is great for simple programs of a few lines or less that process text files and perform basic arithmetic operations. The basic syntax is
    awk 'script' file
    For example, to print the first and third fields of every line of a file:
    awk '{print $1 $3}' foo.txt
    There is a nice list of useful awk one-liners courtesy of Greg Wirth here.

  7. paste
    This is an unsung command that adds the lines of one file onto the end of the lines from another - sort of a horizontal cat command. This is especially useful if you have two files which consist of columns of data and you want to make one file containing all columns.
    paste file1.dat file2.dat > file3.dat

  8. split
    This command is useful for dealing with files that are too big to store in one place. I used to use split a lot when floppy disks were in vogue to split up large files between several floppies, e.g.
    split -b 1400k file.dat
    This creates files named xaa, xab, xac, etc. 1400 kB in size (just small enough to fit on a 3.25" floppy disk). To put the file back together again simply use:
    cat xaa xab xac > file.dat
    Pretty neat, huh? This works on binary files as well as text files so you can use it on word processor documents, binary executables, or whatever else. Nowadays this could be used to split up extremely large files between multiply CDs, although who uses files are larger than 700 Mb?

  9. !$
    This isn't really a command, but a command substitution syntax available in C shell. There are several variations on command substitution, all of which begin with a "bang," !. For example,
    !!
    executes the previous command. I find
    !$
    to be the most useful - this substitutes the last argument of the previous command. A practical example of this might be
    > ls -l file.txt
    > pico !$
    I.e. list a file then edit that same file. This can save you a lot of typing, especially if you tend to use long filenames like me.

  10. alias
    Most people know about this one, but just in case... Instead of typing the same command and options over and over again with a different argument, try defining an alias for that command to save yourself some typing. For example, instead of typing the following sequence:
    > sed s/oldstring/newstring/g oldfile.txt > newfile.txt
    > mv -f newfile.txt oldfile.txt
    every time you want to replace a string in a file, put the following line in a .alias file in your home directory:
    alias repltxt "sed s/\!:1/\!:2/g \!:3 > temp.txt; mv -f temp.txt \!:3"
    Now, after you source .alias, all you have to do is type:
    repltxt oldstring newstring oldfile.txt
    No need to type in all the /'s and filenames more than once! Most people have a source .alias line in their .cshrc file so that all their aliases are read in at login.
  11. wget
    A great little command that is virtually unheard of. Use wget to download files from a URL on the command line. Very useful for retrieving files in a directory available by http. But use this command for good, never for evil!


[Welcome] [Astronomy] [Linux] [Me] [Links]

Last modified: Fri Feb 4 05:51:54 2005