Examples of Shell Scripting

Linux Commands -

  • a2p - translation utility from awk to Perl
  • a2ps - translation utility from ``any'' to Postscript
  • ac - print statistics concerning user connect time
  • access - determine whether a file can be accessed
  • adduser - used by root to add user to system
    usage: adduser userid 
  • afio - utility to copy or restore files to an archive file. This utility is not part of the basic internal and external programs under Red Hat Linux. It is available as an rpm on the rpmfind.net site.
  • alias - assign name to specified command list. This is actually a shell builtin. On my Red Hat system, I have global alias commands in my /etc/bashrc file and in my  /.bashrc file. On my Slackware box, I have made the rm command a little bit safer with
  • apropos - display command names based on keyword search
    usage: apropos keyword 
  • arch - print machine architecture type
  • at - executes a shell script at specified time. Use atq to show pending jobs, and atrm to remove jobs from the queue.
    usage: at time or: at -f file time 
  • atq - shows pending jobs queued by at. If run by root, shows everybody's pending jobs.
  • atrm - removes pending jobs queued by at. Use atq to determine the identities of various jobs.
    usage: atrm job
  • awk - searches for and process patterns in a file
  • banner - print banner to standard output. Syntax is
    banner [option] [characters] 
  • bash - Bourne again shell. This is the default shell in the Red Hat installation.
  • batch - queue, examine, or delete jobs for later execution. See at.
  • bc - a language (compiler) similar to C, with unlimited precision arithmetic
  • bg PID - send process with pid ``PID'' to the background. This is the same as executing [Ctrl]z while interacting with the running process. This is a shell builtin.
  • cal - displays a 12-month calendar for the given year or a one-month calendar of the given month
    usage: cal month year 
  • cat - combine, copy standard input to standard output. Used to join or display files.
  • cd - change working directory. This is a shell builtin in bash, tcsh and zsh.
  • chgrp - changes group associated with file. Can be used to change the group associated with subdirectories and files of a directory.
    usage: chgrp group file OR chgrp -R group files 
  • chkconfig - Query or update system services/daemons for different runlevels. Manipulates the various symbolic links in /etc/rc.d. This utility is included with many rpm-based distributions such as RedHat and Mandrake. It is designed to work with System V initialization scripts. Graphical tools for configuring system services include ntsysv, tksysv and ksysv (the latter is a KDE utility).
  • chmod - set permissions (modes) of files or directories. A value of 4 is used for read permission. A value of 2 is used for write permission. A value of 1 is used for execute permission. See umask for default file permissions upon file creation. Chmod can also be used to change the suid bit on files. The syntax for the symbolic version is
    chmod [options] who operation permission file-list
    The syntax for the absolute version is
    chmod [options] mode file-list 

    To set the uid to the owner's permissions, use : chmod u +s file-name
    To set the uid to the group's permissions, use : chmod g +s file-name
  • chown - changes ownership of a file. Can be used recursively.
    usage: chown userid files OR chown -R userid files 
  • clear - clear screen command
  • cmp - compares two files for differences
    usage: cmp file1 file2
  • cp - copies on or more files. Recursive copying is one simple way of archiving part of a directory structure. Use the command as follows: cp -r /sourcedirectory /targetdirectory
  • cpio - direct copy of files to an output device. Allows creation of archive file spanning multiple diskettes. Allows one directory structure to be mirrored elsewhere on the partition or on another partition. In order to back up an entire directory structure on diskettes, cd to the directory and use the following command: find . -depth -print | cpio -ov > /dev/fd0
    To restore from diskettes, use: cpio -iv < /dev/fd0
  • crontab - schedules command to run at regularly specified time
  • cut - selects characters or TAB-separated fields from lines of input and writes them to standard output
  • date - displays or sets date and time, usage: date OR date date
  • dd - direct copy of file from one device to another. Can be used to make copies of boot or root diskettes for installing Linux. It can be used, for example, to make and exact copy of a floppy disk, as follows. First, place the diskette to be copied in the floppy drive. Then, dd if=/dev/fd0 ibs=512 > floppy.copy
    Replace the diskette with a fresh diskette.dd if=floppy.copy bs=512 of=/dev/fd0
  • df - displays capacity and free capacity on different physical devices such as hard drive partitions and floppy drives that are mounted on the file system. Gives free space in blocks. With the (undocumented) option -h, the program gives free space in Mb or Gb. This is useful for those accustomed to thinking of the capacity of a high-density 3.5 inch diskette as 1440k.
  • diff - displays differences between two files, usage: diff file1 file2
  • dmesg - displays messages from /var/log relative to the most recent boot
  • du - displays information on disk usage. The command: du / -bh | less
    will display detailed disk usage for each subdirectory starting at root, giving files sizes in bytes.
  • e2fsck - check an ext2 filesystem. The syntax is e2fsck /dev/devicename
  • egrep - search files for lines that match regular expressions. Runs faster than grep and fgrep.
  • exit - exit a shell. This is a shell builtin.
  • expect - a program that ``talks'' to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.
  • fc - views, edits, and executes commands for the history list. This is a shell builtin.
  • fdisk - used to partition hard drives, usage: fdisk device
  • fg PID - bring a background or stopped process with pid ``PID'' to the foreground. This is a shell builtin. If only one process is running in background mode, fg with no argument is sufficient to bring it to the foreground
  • fgrep - search for patterns in files
  • find - find files according to a large variety of search criteria. The find command that I use the most is, find . -name filename -print,
    In order to find files matching a particular name on the working directory and all subdirectories. Find can be incredibly powerful, but it is incredibly obscure.
  • finger - display information about a specified userid or userids
  • free - gives used and free memory on system along with other useful information
  • fromdos - takes a DOS text file from stdin and sends a UNIX file to stdout.
  • fsck - file system check and repair
  • grep - used to find a string within a file. The -i option returns matches without regard to case. The -n option means that each line of output is preceded by file name and line number. The -v option causes non-matched lines to be printed. usage: grep pattern files
    or: grep -i pattern files
    or: grep -n pattern files
    or: grep -v pattern files
  • groupadd - create a new group on the system
  • groups - shows which groups you are in
  • gunzip - used to uncompress files compressed with gzip
  • halt - shut down system as root, without reboot, immediately
  • head - displays first part of a file
  • history - command for viewing and manipulating the shell command history list
  • host - look up host names using domain server
  • hostname - used to get or set hostname. Typically, the host name is stored in the file /etc/HOSTNAME.
  • hwclock - used to query and set the hardware clock
  • id - display userid and groupid
  • inetd - daemon which starts up other daemons on demand. Configured in /etc/inetd.conf.
  • ifconfig - display (as root) information on network interfaces that are currently active. First ethernet interface should be listed as eth0, second as eth1, etc. First modem ppp connection should be listed as ppp0, etc. The ``lo'' connection is ``loopback'' only.
  • ifdown - shut down the network interface
  • ifup [interface_name] - start up the interface
  • info - display system information. This is the GNU hypertext reader.
  • init - the mother of all processes, run at bootup, executes commands in /etc/inittab. Can be used (with root privileges) to change the system run level. usage: init run_level
  • insmod - used (by root) to install modular device drivers
  • jobs - displays list of current jobs in the background. This is a shell builtin.
  • kill - sends a signal to (especially to terminate) a job or process. This is a shell builtin in bash, tcsh and zsh.
  • killall - kill processes by name. Kill all processes which are instances of the speciffied program. Also used to send signals to processes or restart them.
  • last - generate a listing of user logins
  • ldconfig - creates the necessary links and cache (for use by the run-time linker, ld.so) to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/usr/lib and /lib). Ldconfig checks the header and file names of the libraries it encounters when determining which versions should have their links updated. Ldconfig ignores symbolic links when scanning for libraries.
  • less - Linux alternative to ``more'' command. Displays text files, one screenful at a time. When less pauses, there is a large number of available commands to tell it what to do next. One can scroll both forwards and backwards.
  • linuxconf - interactive tool for configuring Linux system. Uses X if loaded. This is a Gnome tool. It comes with my Red Hat distribution, and is not included with Slackware. It would seem to be the easiest way to configure Linux under Slackware. Version 1.15 is available for Slackware. There is a pretty good introduction to the use of linuxconf in the Red Hat 5.2 installation manual, which is available online at their web site.
  • ln - creates a link to a file. Used to create hard links and, with the -s option, symbolic links which can link files on different disk partitions.     The syntax is: ln [options] source [dest]
  • logout - execute logout as individual user and bring up login: prompt
  • lpr - send file to be printed
  • lprm - cancel a job from print queue
  • ls - list directory contents. To get colored directory listings under Red Hat, Mandrake, etc., use
    ls -color
  • lsattr - list attributes of files in ext2 file system
  • lsmod - used (by root) to show kernel modules currently loaded
  • mkdir - create a directory
  • mkfs - create a file system (format) on a device or partition. Should be invoked after lowlevel formatting of the disk using fdformat. It has several versions which are all links to the basic program, such as mkfs.ext2 and mkfs.msdos.
  • mkswap - creates a Linux swap space on the specified hard disk parition (root privileges neede)
    usage: mkswap device
  • more - list file contents, stopping after each full screen
  • mount -t [fstype] [device] [mountpoint] - mount device using filesystem of type [fstype] with device name [device] at the location [mountpoint] in the filesystem directory tree
  • mount -a - mount all filesystems according to the specifications in /etc/fstab
  • mv - moves (renames) files
  • netconf - used (as root) to set up network
  • nice program_name - sets the priority of the program ``program_name''.
  • passwd - change login password
  • paste - joins corresponding lines from files
  • pg - display data one screenful at a time
  • ping - check if Internet computer is responding. Can also measure the time it takes the queried computer to respond.
  • ps - displays status of processes. Use the -a option for processes for all users. Use the -x option to include processes not attached to a terminal.
  • pstree - display processes in the form of a tree structure. Killing a parent process will also kill all the children and their descendants.
  • pwd - print absolute path of working directory. This is a shell builtin.
  • pwchk - checks the integrity of password and shadow files
  • pwconv - converts passwords to the shadow password format
  • rcp - copy one or more files to or from remote computer. The syntax is poorly explained in the documentation that I have, including the man pages. Usage is:
    rcp filename username@remotehost:path
    The user's home directory on the remote system must contain the file .rhosts with a list of users (preceded by the full domain name or exact IP address of their machine) with access privileges. localhostname username
  • reboot - in Slackware, reboots the system. Seems to be equivalent to shutdown -r now in generic Linux.
  • renice program_name - resets the priority of process ``program_name''.
  • rm - remove files or directories. With the -r (recursive) option (very dangerous!), can be used to remove the contents of a specified directory including all subdirectories.
  • rmdir - remove empty directories
  • rpm - invokes the Red Hat package manager in command line mode. I often use this command in query mode to query packages about what files they contain and to find out which package owns a particular file. Examples are
    rpm -qil foo. Gives package information and a file list for the package foo.
    rpm -qfil foo. Gives package information and a file list for the package that owns the file foo. Foo must be in the working directory, or the full path to foo must be specified.
  • rusers - list who is logged on local machines
  • rwall - write to all users over a network
  • showmount - show information about an nfs server
  • shutdown - reboot or shut down system as root, after specified amount of time. With the -r option, reboot. With the -h option, halt the system. usage: shutdown -r minutes
  • sleep - creates process that sleeps for specified interval
  • sort - sorts and/or merge files
  • split - split file into specified number of segments
  • ssh - secure shell. Apparently has many of the same functionalities as rlogin, telnet, ftp, rsh, etc., with better security and encryption features. We may want to learn how to set this up and use it.
  • startx - front end to xinit in Linux. This is a script which starts up X clients and shuts down the X server on exit from the window manager.
  • su - log in as another user, including root
  • sudo - allows individual users to have root permission to perform specified tasks
  • swapoff - disables swap disk
  • swapon - enables swap disk
  • symlinks - provide list of and information about symbolic links
  • systat - query host for system information
  • tac - print file in reverse
  • tail - displays the last part of a file
  • tar - file compression and archiving utility. I find the syntax of this command to be frustratingly opaque. The following works for me. To use this command to unzip gzipped tarballs in verbose mode, use
    tar -xvzf filename.tgz
    To create a tarball from files in a given directory and its subdirectories, use - tar -cvzf filename.tgz sourcename
    Sourcename can be the name of a single file, a wildcard such as *, or the name of a subdirectory. There seem to be two different conventions concerning gzipped tarballs. One often encounters .tar.gz. The other popular choice is .tgz. Slackware packages use the latter convention. The command can also be used to archive a file, a group of files, or a directory (with its subdirectories) on tape or onto floppies. 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. Use the following command to back up to floppies:
    tar -cvf /dev/fd0 filename(s) or directoryname(s)
    The backup can be restored with tar -xvf /dev/fd0
    Tar can be used for other things. To mirror all the files and subdirectories in from-stuff to to-stuff, use the commands : cd from-stuff
    tar cf - . | (cd ../to-stuff; tar xvf -)
    No tar file is ever written to disk. The data is sent by pipe from one tar process to another. This example is taken from Running Linux, p.177. To list the table of contents of a tar archive, use : tar tvf tarfile
    To extract individual files from a tar archive, use - tar xvf tarfile files where files is the list of files to extract. When extracting files, tar creates missing subdirectories underneath the current directory in which the cammand is invoked.
  • tee - copy standard input to standard output and one or more files
  • telinit - used to change run level. Exact run level that corresponds to single-user, multi-user, and X levels depends on distribution.
  • time - displays times for the current shell and its children. This is a shell builtin. Strange, because there is also a /usr/bin/time program on my Red Hat system.
  • top - dynamically displays process status
  • touch - update access and modification times of a file. If the file does not exist on disk, an empty file is created.
  • tr - translation utility that can be used, for example, to replace specified characters in a text file
  • umask - establishes the file-creation permissions mask. Usage is umask xyz.  The system subtracts x, y and z from the owner, group and other file permissions that it would otherwise assign to new files. This is a shell builtin.
  • umount [device] - finish writing to the device and remove it from the active filesystem. The command umount -a will (re)mount all file systems listed in /etc/fstab.
  • uname - displays information about the system. With no arguments, it displays the name of the operating system. With the -a option, it displays information about the operating system, the host name, and hardware.
  • uniq - displays lines of a file that are unique
  • updatedb - update file database used by locate command
  • uptime - shows the time, how long the system has been up, the number of users, and average load.
  • useradd - same as adduser
  • userdel - remove an account (as root). The user's home directory and undelivered mail must be dealt with separately.
  • w - display info about userids and active processes
  • wc - displays number of lines, characters and words in a file
  • whereis - use to find utilities in standard locations
  • which - used to find utilities in search path. Will return the absolute directory path of the named utility program.
  • who - display information about currently logged in userids
  • xadm - display advanced power management BIOS information
  • xargs - converts standard output of one command into arguments for another. This is one of those powerful but obscure commands. Xargs reads arguments from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by arguments read from standard input. Blank lines on the standard input are ignored.
  • xf86config - graphical configuration tool for X
  • XF86Setup - graphical configuration tool for X
  • xinit - start X Window. The command startx is a front end to xinit in Linux, including Slackware.
  • xload - displays a graphic of the system load

/**/