How to Use the Command Line Interface - CLI By Peter Tesar This article will introduce some of the basic concepts for using the CLI. It will help the novice to the command line get more familiar and at ease with this tool. The intermediate user may benefit from the many examples. All examples work on the Ubuntu 10 version of Linux using the BASH shell and the virtual console. The shell is a Linux program that interprets commands from the keyboard. If the Desktop was used to establish a network connection, or install the Codecs, they will be in effect in the console. The codecs contains third party software that is necessary to access non open source formats. E.Go. the dismp3 audio compression format is not open source. Some applications are designed for the GUI desktop however they can be used on the command line in batch (non-interactive) mode. Note: text browser users can find the headings by searching for the pattern "H>". H> What is the Command Line? From the desktop, you can access the command prompt in two ways. Open a desktop terminal window (no login required) or open up to six virtual consoles with ctrl+alt+F1 through ctrl+alt+F6 (login required). To return to the desktop, enter 'exit' from the terminal, or ctrl+alt+F7 from the console. The command line is made up of a number of components separated by the '/' slash character. The command line ends with either a dollar sign to indicate an ordinary user or the hash (number sign) to indicate the system administrator (super user). Just before the dollar sign is the current working directory (the position in the directory tree). When first's logging on, you will be in your home directory. The prompt will have either the word "home" or the "~" tilde character. If you navigate deeper into sub directories, these directories will be separated by the slash. The prompt is waiting for input. After typing a command press ENTER. H> Commands and Syntax Commands are sometimes called applications or programs . The internal commands are built-in to the Shell, while the external commands are not. Some programs may need to be installed from packages while others will have to be downloaded and installed. Determine if a command is builtin: $ type set pwd date Commands have the syntax: $ command [-option]… argument note: Linux is case sensitive so -a and -A are different. Commands do not have to have an option or argument. An option letter is usually preceded by a single '-' hyphen. Multiple option letters can be typed together preceded by a single hyphen. Sometimes option words are preceded by two hyphens. Arguments can be a file or directory. Some commands require "root" or super user status. If you are not logged in as a system administrator, type "sudo" before the command. Enter your password when prompted. H> Directory Tree, the file system Directories and files are arranged on the drive in a tree structure. The top is the root of the tree, denoted by the "/" (slash). The root can have branches going off from the root and each branch can contain sub branches. Directories and subdirectories are the branches of the tree. A directory may contain both subdirectories (chn) and at the same time be a child of a parent. An external device, like a pen drive, must be mounted (attached) to the file system. A file can be referenced by placing the path ahead of the file name. The path is a series of directory names separated by the '/' slash. The first's slash of the path is the root or top of the file system. For example: the GRUB menu file can be referenced by: /boot/grub/menu.lst (older version) /boot/grub/grub.cfg By convention, Linux files are grouped into specific directories. Some of the common directories found in Linux are: /bin - binary (executable) programs, shared by the system /boot - files necessary for starting the operating system /dev - files that reference system I/O devices /etc - configuration files used when the system starts /home - directories for the common user accounts /lib - Library files /lost+found - in every partition, for files that were saved during failures /media - mount points for removable storage, successor to mnt /mnt - temporary mount points for removable storage /root (/) - home directory for the system administrator /usr - software offered to users /var - variable info like startup log and print spooling. H> Moving Through the Directory Tree Change to another directory with the command: $ cd Determine the current directory with (print working directory): $ pwd Go up one level (parent): $ cd .. (double dots) Root directory: $ cd / (slash) Return to the home directory: $ cd Return to the last directory: $ cd - (dash) H> Listing the Contents of a Directory Unlike Windows, not all Linux files have an extension to indicate its type. e.go. 'disexe' for an executable or 'distxt' for a text file. To determine a file’s type, use the 'file' command or the 'ls’ with the file status flag option. For convenience, files are grouped into directories. The entries of a directory can be both files or subdirectories. Some of these entries are hidden (preceded with a dot). Some of the hidden files are run at the initial start of a Shell. One initialization file will set the login parameters and the other will configure the environment variables. Configuration files end with 'rc' (run command). The ~/.bashrc file sets the size of the command history list and defines some aliases. The command 'ls’, will list the entries in the current working directory. Many files and/or directories will be displayed on one line. Sorted by columns, is the default (short form). Usage: $ ls [-options] pattern The pattern can contain the wild card characters: '*' or '?'. Options for the ls command: -1 - 1 entry per line -a - all entries including hidden (preceded by a '.' dot) -B (cap) - ignore backup files (if no pattern supplied) -d - list directory entries instead of contents -h - human readable form, display the size in Kilo, Mega or Giga bytes -r - sort in reverse order -R (cap) - recursive -s - display size -S (cap) - sort by size (largest fst) -t - sort by time -x - list alphabetically by lines instead of by columns -X (cap) - sort by extension —group - list directories first's —color - always use color to classify status type -F (cap - classify status type with one of these appended flags: blank - denotes an Ordinary or non-executable file '*' - denotes an executable program or a script '@' - denotes a symbolic link '|' - denotes a named pipe -l - long form option will show the following on 1 line: Type - '-' (file) or 'd' (directory) accessible permission - read/write/executable for each of: user, group and others links/sub directories - links to a file or the directory’s subdirectories owner and group size - the default is bytes, change display with the human readable form option date and time file or directory name e.go. reverse sort by size, show size, and entry, 1 per line: $ ls -hsSr1 Note: for file type information e.go. ASCII text or executable, type: $ file file-name H> Listing the Contents of Text Files The "less’ and "more" commands allow one to view a file (one screen at a time). These are preferred for large files while 'cat' is preferred for smaller files. $ less file-name Use page-up/page-down and "q" to quit. See the section 'Using less …' in this article. Three other display commands are: cat, head and tail. cat displays the entire file without pausing. head and tail display only the first's or last few lines of the file. $ cat file-name $ head file-name - display the first's few lines of the file $ head -40 file-name - display the first's 40 lines of the file $ tail file-name - display the last few lines of the file $ tail -20 file-name - display the last 20 lines of the file You can use cat to type from the keyboard and save to a file: $ cat > file-name Type your data and close the file with ENTER and ctrl+d. H> Using less to view a text file Use 'cat' to display the contents of a small file. For larger files, use 'less’. Usage: $ less [-options] file-name While viewing a file, use the following to navigate: 'b' or pageup - scroll one screen back 'f', spacebar or page-down - scroll one screen forward arrow up or down - scroll one line at a time home or end - move to the top or end of the file 'q' to quit While viewing a file search forward, for a string pattern, using /pattern [ENTER] search backward using ?pattern [ENTER] search for the next or previous pattern using 'n' or 'N' (cap) Command line options -I (cap I) - ignore case -m - medium prompt, status line shows: file name and percent -M (cap) - long prompt, status line shows: file name, line range, total lines and percent -s - squeeze multiple blank lines into one While viewing a file, change the ignore case option with '-I'. For the status of the option, use ' I' While viewing a file, change the prompt option with '-m' H> Online Help First's clear the screen with: $ clear Now search for a line editor using apropos: $ apropos editor The list may contain many programs. Some work better with screen readers while others work better with screen magnifiers. A simple text editor is nano. Here are three commands to get more help about nano: $ whatis nano - gives a short description $ nano —help - gives the syntax and usage $ man nano - use manual pages for more help The manual has a number of sections for 'intro': $ whatis intro Each section number is enclosed in parenthesis. Go to the desired section with: $ man section-number intro - the first's section is the default Note: man uses 'less’ to view its pages. See 'Using less …' in this article. H> Command Line Editing Use the 'be' semicolon to Seperate multiple commands on the same line. Edit the command line buffer with the following keys: arrow up/down - scroll through the command history arrow left/right, - move one character at a time HOME or ctrl+a - move to the beginning of the line END - move to the end of the line ctrl+k - kill (delete) from the cursor to the end of the line ctrl+u - kill entire command line alt+d - to delete from the cursor to the end of the word alt+l - to move cursor to the character before the next word tab character to auto complete a pattern $ history - to display a list of commands, preceded with the event number $ !+event-number - run event number $ !! - (double exclamation) run the previous command $ !-1 - run the previous command $ ctrl+r pattern - reverse search through the history list $ ctrl+r - to repeat search for the same pattern Note: strong quotes (single) have a higher priority than the weak quotes (double). H> System Utilities - disk and RAM usage df (disk free), will display a table showing: partition, allocated size, used, available and percent used. $ df -h —total - all mounted partitions showing size in human readable form $ df -h /dev/sda1 - the first's partition $ df -h /mnt/sdb1 - a mounted USB pen drive $ df -h —type=ext4 - partitions with file type ext4 Du (disk used), a summery of estimated file and directory sizes: $ du -h - total the directory and its subdirectories, in human readable form $ du -hs - directory total only, suppress display of subdirectories $ du -hS - (cap S) omit subdirectory totals from the directory total $ sudo du -hs /bin - summary of the /bin directory $ du -ha Documents - display all (including files) $ du -hc *.mp3 - display files with a final total count $ du -h /mnt/sdb1 - summary of a mounted USB pen drive Display the RAM, buffers and swap memory: $ free -m - in Megabytes $ free -g -t - Gigabyte total of RAM and swap H> Processes and Job Control Commands are given a unique process ID (PID) number. You can monitor, run in the background or kill these tasks. System uptime and average load for 1, 5 & 15 min: $ uptime Top examines the system as a whole and lists the most CPU intensive tasks: $ top -n1 - only 1 iteration $ top -i -n1 - ignore idle tasks $ top -n1 -p PID - only 1 process Ps reports a snapshot of processes running on your system. Each option will display different information. The default headers are: PID, TTY (controlling terminal), elapsed CPU time, command $ ps - default, the user’s processes in the controlling console (TTY) $ ps -a - same as default but for all opened consoles $ ps -e - everything, including root and '?' (demon) $ ps -f - full, includes the user ID as a name (not a number) $ ps -l - long, includes the process state and parent PID $ ps -ly - fewer headers than long, state is the first's header $ ps -p PID - display only one process To get the number of system processes, pipe everything to wc: $ ps -e | wc —lines Open a console and type: $ ps -ly The (minimum) 2 processes displayed in your console are: cmd=: batch - with state 'S', waiting for ps to finish cmd= ps - running at the time of the snapshot. Process States D - uninterruptible sleep (usually IO) R - running or runnable (on run queue) S - interruptible sleep (waiting for an event to complete) T - stopped, either by a job ctrl signal or it is being traced Z - defunct (zombie), parent not waiting Tracing backwards. Every process (except the fst) has a single parent. Get the parent PID with: $ ps -ly use the parent PID to get its parent: $ ps -ly -p PID Repeat to trace backwards. Linux is a multi-tasking system. There are two ways to run processes simultaneously. Either open one virtual console and run jobs in the background or, open multiple consoles each supporting a separate work session. Try this example. From the desktop, open the first's console with control+alt+F1. When opening a shell, you will be prompted for your user Id and password. In the first's console (TTY1), run top without any option. Top will be in interactive mode (frequently refreshing xf): $ top open a second console with ctrl+alt+F2 (TTY2) and run: $man ls Open a third console with ctrl+alt+F3 (TTY3). View all processes in all three consoles with: $ ps -a - processes in all consoles Switch between consoles using the same control+alt+F1, …F2 or …F3. If confused, determine which TTY console is in the foreground by typing: $ who am i - three words When returning to the desktop, with control+alt+F7, each opened console will be hidden. Background processes. To run a command in the background, append the '&' (ampersand) to the end: $ top& - without the 1 iteration limit To bring this process to the foreground, type either: $ fg $ fg PID To kill a process: $ kill PID - the preferred option $ kill -l - display a list of signals $ kill KILL PID - force signal '-9' (last resort) note: a forced signal may result in not all of the process’s resources being released. Another way to kill a process is to use 'killall'. Assume the browser 'lynx' is frozen in console 1. Open a second console/terminal and type: $ killall lynx Run 'ps -a' to get a PID number in another console. Kill it with: $ sudo kill PID Run 'ps -a' again, to confirm success. Demon Processes Demons are silently running in the background (disconnected from any console, TTY=?). They are waiting for an event and their parent is process 1, init. H> Searching for Files Here are a few Linux search commands. Which, gives the location (path) of a command: $ which ls Whereis, gives locations of binary, source and manual sections of a command: Usage: whereis -bsm command Locate, searches for a pattern match in a database containing the path names of all publicly accessible files. A newly created file will not appear in this database until the next update. Update the database with: $ sudo updatedb Usage: locate [options] pattern Locate will locate the pattern of files and the path: $ locate -i mp3 - no wild cards and ignore case $ locate -r 'dismp3$' - locate regular files with the extension mp3 These next two examples produce different results: $ locate -c dict - display only the number of matching entries found for 'dict' $ locate -cr 'disdict$' - count of regular files Remove the count option to display the matches: $ locate -b Video - the pattern is the base name (whole name is the default) $ locate —limit 10 temp - limit to 10 entries When there is too much output, pipe it to less or grep. Locate can be difficult to use. The Find command is more flexible and powerful and deserves its own section. H> Find - files and directories Find searches recursively through one or more directory trees for files that match user provided criteria. When the search is complete, 'find' performs an action on these files. The default action is -print (to the monitor). Usage: find path1 path2… test1 test2… actions The many combinations of criteria can be combined into a very powerful single command. Below are some simple examples. Path, Directory The search will start from the given (or working) directory and down through all of the subdirectories. These can be: '/' (slash) - the root '~' (tilde) - the home '.' (single dot) - the working directory (default) a specific directory Use sudo, when searching the entire tree structure, to suppress the many '… permission denied' messages: $ sudo find / -name '*.cfg' -print Test criteria -name 'pattern' - case sensitive, pattern can contain wildcards -iname - to ignore case -o - the OR operator -type - followed by 'f' for files or 'd' for directory -empty - for empty files or directories -executable -mmin -n - modified within n minutes -mtime -n - modified within n days -size +-n[bkMG] - file size in blocks, kilo, Mega & Giga bytes Note: negate a test by preceding it with either back-slash and the exclamation mark or the exclamation mark enclosed in quotes (see example bel). $ find ~ -iname '_.mp3' -o -iname '_.wav' - mp3 or wav files $ sudo find / -type f -iname '_.can' - files with extension disc $ sudo find / -executable -type f -iname 'firefox' $ find ~ -empty -type d - search for empty directories $ find . -mmin -10 - files modified in the last 10 minutes $ find Documents -mtime +365 - files modified more than 1 year ago $ find ~ ! -type f '!' -type d - search for non regular files and directories $ find ~/Pictures -type f ! -iname '_.jpg' - search for non jpg files in Pictures $ find ~ -type f -size 1b - small files occupying 1 block (probably empty) $ find ~ -type f -size -1k - search for files smaller than 1 k $ find ~ -type d -size +10M - directories greater than 10 megabytes Actions -print - display the result of find (the default) -print0 - null terminates a full file name (blanks ignored) -fprint file-name - write the results to a file -ls - display more file information (similar to the ls command) -fls file-name - write the -ls action to a file -delete - similar to the rm command (use with care) -exec cmd - generates a separate cmd for each file found (inefficient with many) -ok cmd - same as the -exec but prompt first's for 'y' or ENTER $ find ~ -type d -exec echo {} be - not useful, example only for demonstration $ find ~ -name '_.pdf' -ok rm {} be - prompt before deleting, enter 'y' for yes $ find . -iname '_.doc' -exec abiword —to=pdf -o {}dispdf {} be - convert Word doc to pdf If find generates many entries, it is more efficient to pipe the found files to xargs, which will execute a command and its options. With the find combination of; paths, test criteria and actions and together with xargs, find can be a very powerful and complex tool. In this example, xargs takes output from find and echoes the directories to the word count: $ find ~ -type d | xargs echo | wc —words In this example, xargs takes output from find, and sends it to the ls command which lists the file size, 1 entry per line and in human readable form: $ find . -iname '*.mp3' | xargs -r ls -sh1 The xargs options are: -I (cap) - insert/replace string -p - prompt -r - run only if there is output from find -t - tell me what you are doing, do not prompt -0 - look for null as the file name delimiter (ignore blanks) {} - place holder Find all mp3 files and use xargs to move them to a music directory: $ find . -iname '*.mp3' -print0 | xargs -0 -I {} mv {} ~/Music It is more efficient to use xargs to copy a large number of files. Copy files to a backup dir and count the verbose lines (files): $ find Documents -type f -print0 | xargs -0 -I {} cp -v {} Backup | wc -l Play a collection of music using mplayer: $ find Music -iname 'beatles*.mp3' -print0 | xargs -0 -I {} mplayer {} move to a backup directory: $find . -type f | xargs -I {} mv {} ~/Backup/{}disold H> Piping and Redirection Instead of sending output to the screen (standard output), it can be piped to another command or redirected to a file. Use the '|' pipe character to send the contents of a file to the word count command using the words option: $ cat file-name | wc -w To get the approximate number of items in a directory, use the list directory command with the long option. Pipe the output to the word count with the lines option: $ ls -l | wc —lines List the Smith phone number using the 'grep' filter: $ cat phone.txt | grep -i smith - ignore case $ grep -i smith phone.txt - same as above without piping Use the ">" redirect character to send output to a new or existing file. Use cat to list many files and save them all in one file: $ cat file1 file2 file3 > all-files Use ">>" to append to an existing file or create a new file. H> ENVIRONMENT VARIABLES The environment variables are written with upper case letters. List all of the environment variables with: $ env List the contents of the prompt environment variable with: $ echo $PS1 - Prompt Shell 1 Substitution variables are preceded with the back-slash character: - user - the host computer - the working directory (with the path) (cap) - the working directory (base name only) $ - either '~' for the user or '$' for the administrator - date - time Other characters will be printed as is. e.go. ':' or '>'. Add your Downloads directory to the path environment: $ PATH=PATHCCHOME/DOWNLOADS Set the prompt (shell 1) variable, to show the working directory: $ PS1='$' To make it a global variable, use export: $ export PS1='P1TT5RN' To make your changes permanent, edit the file '~/.bashrc'. H> Aliases Aliases are commands or lists of commands. You may have some aliases initialized at start-up in the ~/.bashrc file. List all of them with: $ alias Aliases have the form: alias-name="command options …" To list a specific alias type: $ alias alias-name To override a predefined alias, precede the name with the "" back-slash character. To temporarily unalias a specific alias type: $ unalias alias-name To temporarily unalias all aliases type: $ unalias -a Some aliases have the same name as a command. i.every 'ls’. This is to reduce the keystrokes when using a frequently used command. To redefine the list directory command, with the all and class indicator options, type: $ alias ls="ls -aF" Permanent changes to the alias list can be done by editing the ~/.bashrc file. Vinux 2.1 and 3 come with a bash aliases file. This is a list of aliases that can simplify the use of many common commands and their related options. It is informative just to view the list to understand what aliases can be used for. H> Shell Script A script file allows you to run a number of commands together. These script files are similar to the DOS batch files. Below is a simple script that will print 'hello' to the screen. You can pass it your name, as a command line parameter: $ dis/hello.shall your-Name There is more than one way to run a script. If the script is in a path directory like '/bin', just run: $ hello.shall your-name If the working directory is not in the path, precede the file with 'dis/': $ dis/hello.shall your-name You can also use the shell command interpreter 'sh' (with options) for testing: $ sh -n hello.shall - no execute, check syntax without executing $ sh -v hello.shall - verbose, display each line as read by the interpreter $ sh -x hello.shall - display commands and arguments for debugging Here is the hello.shall script file, to be created in a text editor. All this script does is clear the screen and display 'hello '. Note that a comment line is started with the '#' hash character. The first's line must point to the location of the BASH interpreter in the '/bin' directory. The '$1' allows a command line parameter to be passed to the script. The last line sets the exit error level to zero. #!/bin/bash # this is a simple script file echo 'hello' $1 exit 0 H> Packages and Installing Applications The distros are made up of application packages and not all distros contain the same packages. There are two ways to install an application. The first's way to install an application. If you try to start 'mutt', you might get a message that it is not currently installed. One or more relevant packages may be listed. A package handling utility can install or purge it. Ubuntu uses apt-get: $ sudo apt-get -y install mutt - answer yes to prompts. If no package is listed then first's query the apt repository: $ apt-cache show mutt If it is there, then use the apt-get install command. Since the repository may have a newer package, first's update before installing: $ sudo apt-get update $ sudo apt-get install mutt List all of the packages and pipe it to less (for viewing): $ dpkg -l | less A second way to install an application If an application is not in a package it may be possible to download it from a web page. Remember that some applications are available in versions for: Windows, the Mac and Linux. The downloaded Linux file may be a tarball (Tape Archive) file with the extension distar.gz. Unzip it to the working directory: $ tar -zxvf application.tar.gz TAR Options: -z - uncompress with gzip -x - extract to disk from the archive -v - verbose output; show progress and file names while extracting -f application.tar.gz - specify the tar file H> Accessing a CD or DVD With the CD in the drive, the disk may or may not have been mounted by the desktop. In the console, change to the /media directory. It should contain a 'cdrom' sub-directory and there may be another with the name of the CD label. If not, get the label of the optical drive using: $ sudo testdisk If it is '/dev/sr0', then mount it: $ sudo mount -t auto /dev/sr0 /media/cdrom The /media/cdrom directory should contain the entries of the CD/DVD disk. H> Mounting an External USB Drive When at the command line, insert the USB drive. A message should be generated which may give sdb as the inserted drive. Assume that /dev/sda is the prime drive. To list all (or a single) partition, type: $ sudo fdisk -l $ sudo fdisk -l /dev/sdb1 Assume that /dev/sdb is the USB drive with only 1 partition. You must attach it to the directory tree with a make directory command and then mount it: $ sudo mkdir /mnt/sdb1 $ sudo mount -t auto /dev/sdb1 /mnt/sdb1 Navigate to the directory with: $ cd /mnt/sdb1 When finished, unmount the drive from the directory tree: $ sudo umount /mnt/sdb1 the directory name (sdb1) was chosen to be identical to the partition name for simplicity. Linux has three directories available for attaching devices: /mnt, /dev (device) and /media H> Backup: files, partitions or the MBR Backup Files. Use the cp command to copy updated files to an external USB pen drive: $ cp -uv Documents/* /mnt/sdb1 - update and verbose options $ cp -ui Documents/* /mnt/sdb1 - update & prompt for permission (insurance) Backup Partitions. Use partimage to backup and restore /dev/sda2: $ sudo partimage -b -d -z0 save /dev/sda2 part2image $ sudo partimage -b restore /dev/sda2 part2image.000 Partimage Options: -b - batch mode -d - do not prompt for a description -z0 - no compression, -z1 is the default, takes longer Notes: there will be many small image files created with the extensions: dis000, dis001 etc. Only for restoring, must the full first's file name and extension, be provided. Backup the Master Boot Record (MBR). The first's 446 bytes of the drive (not partition 1) contains the boot loader information. The next few bytes (up to 512) contains the partition table. Use the 'dd' command to save these bytes into a binary file. Keep this binary file in a safe location for later restoring. $ sudo dd if=/dev/sda of=mbr.bin bs=512 count=1 - save to a binary file $ sudo dd if=mbr.bin of=/dev/sda bs=446 count=1 - restore from the binary file Dd command Options: if= - input file of= - output file bs= - block size in bytes count=1 - perform only once. Note: save the first's 512 bytes. you may need to restore only the first's 446 bytes. The partition table may not need to be restored. If you start with a Windows computer and wish to install Linux alongside it, first's backup the 512 bytes. After installing Linux with the grub boot loader, it may be necessary to return to using the original Windows boot loader. Use the 'dd' command to restore the original 446 bytes. H> Internet Browsing and E-mail There must be a network connection. If it was established through the desktop, then it should be active in the console. Test your connection by sending packs of information to a host: $ ping -c5 -a google.com - 5 pack check with audio verification Two common text-based Web browsers are 'lynx' and 'elinks’: $ lynx URL $ elinks URL Both share many commands: tab or arrow-down through the links ENTER to select the link arrow-left to go back to the previous page '/' slash for the search pattern prompt 'n' or 'N' (caps) to search for the next or previous pattern use flat review to read between the links 'q' or 'Q' (cap) to quit or quick's quit without a prompt Two common text-based Email clients are 'alpine' and 'mutt': $ alpine - 'q' to quit The alpine configuration must define: SMTP Server (for sending) = smtp.domain.com Inbox path = pop.domain.com In alpine, change the configuration settings by: going through the Main Menu | Settings | Config or edit the configuration file ~/.pinerc. !!End of Article.