Introduction to Linux Commands 3 Definitions of Linux • Linux Kernel: The very low-level software that manages your computer hardware and provides a library (POSIX) interface for user-level software. The Linux kernel runs on many platforms (Intel x86 and IA-64, Alpha, MIPS, HP PA-RISC, PowerPC, IBM S/390, SPARC, Motorola 680x0, etc.). • GNU/Linux OS: The Linux kernel plus utility software to provide a useful working environment. • Linux Distributions: The packaging of the Linux Kernel, the GNU/Linux OS and lots of other software to make Linux easy to install, configure, and use (at least for the target audience). Architecture • Kernel: Schedules programs, Manages data/file access and storage, Enforces security, Performs all hardware access. • Init: First program run by kernel on booting. • Shell: Presents each user a prompt, Interprets commands typed by user, Executes users commands, Provides user/programming environment. Architecture File System root (/) bin usr etc dev home var tmp spool (users’ accounts) mail (users’ mail) bin log X11R6 Utilities • Basic Utilities – Directory/File management: cd, ls, pwd, mkdir, rmdir, cp, mv, rm, find, du, file – File viewing/editing: touch, more, less, ed, vi, emacs – User management: passwd, chmod, chown, su, who – Process management: kill, killall, ps – Documentation: man, info, /usr/share/doc • Applications: X11, KDE, Gnome, OpenOffice, Apache, Sendmail, Gimp, Mozilla Firefox • Security Software: gpg, ssh, iptables, ACID, snort, prelude, tcpdump, ethereal, nmap, nessus, tcpspy, tiger, ClamAV, spamassassin Network Commands • telnet • ssh • ftp telnet • Connect to a host machine over the network. • Syntax: telnet [options] {IP or Computer Name} [port number] • Example: telnet 127.0.0.1 telnet 127.0.0.1 25 SMTP may not be running. • Alternative: nc (from the netcat package) • Telnet does not encrypt connections and so is NOT secure. • Use ssh for encrypted secure connections. ssh • Secure Shell • ssh {IP or Computer Name} [username] • Example • ssh mrb.bio.fsu.edu merchant ftp • File Transfer Protocol Syntax: ftp [options] {IP or Hostname} Example: ftp 127.0.0.1 Basic Linux Commands • • • • • • Handling Files and Directories Text Editors Compiling and Linking Handling Processes Archiving and Compressing Files Other Useful Commands Handling Files and Directories • • • • • • • • • ls cp mv rm mkdir cd rmdir pwd chmod : list files : copy files : move files : remove files : make directories : change directories : remove directories : print working directory : change permission mode ls • Syntax – ls [-Options] [name ...] • Description – Lists contents of directory. • Frequently Used Options – – – – -a -d -l -F • Examples – ls -alF List all entries, including . and .. Do not list contents of directories Long listing Mark directories with a '/', etc. cp • Syntax – cp [-Options] file1 [file2 ...] target • Description – File1 is copied to target. • Frequently Used Options – -f – -i – -r Force remove existing file Ask before removing existing file Copy directory trees • Examples – cp p1.c p2.c – cp p1.c p2.c mydir mv • Syntax – mv [-Options] file1 [file2 ...] target • Description – File1 is moved to target. • Frequently Used Options – -f Removes existing files without prompting the user – -i Asks before removing existing file • Examples – mv p*.c mydir rm • Syntax – rm [-f] [-i] file . . . – rm -r [-f] [-i] dirname . . . [file . . .] • Description – Removes files or directories. • Frequently Used Options – -f – -i – -r • Examples Removal of files without prompting the user Interactive removal Recursive removal – rm -f p*.o – rm -r mydir mkdir • Syntax – mkdir [-m mode] [-p] dirname . . . • Description – Creates the specified directories. • Options – -m – -p Specifies the mode to be used Create missing intermediate directories • Examples – mkdir -m 700 letter – mkdir abc – mkdir -p ./abc/def/ghi rmdir • Syntax – rmdir [-p] [-s] dirname . . . • Description – Removes directories. • Options – -p – -s Remove the directory dirname and its parent directories which become empty. Suppress the message when –p is in effect • Examples – rmdir letter cd • Syntax – cd [directory] • Description – Change working directory. – If directory is not specified, the value of shell parameter $HOME is used as the new working directory. • Examples – cd – cd ./abc/def/ghi – cd .. pwd • Syntax – pwd • Description – Prints the path name of the working (current) directory. • Examples – pwd chmod • • • Syntax – chmod [-R] mode file ... – chmod [-R] [ugoa]{+|-|=}[rwxXstl] file ... Description – Changes the permissions mode of a file or directory. Examples chmod 400 file - Read by owner chmod 040 file - Read by group chmod 004 file - Read by world chmod 200 file - Write by owner chmod 020 file - Write by group chmod 002 file - Write by world chmod 100 file - execute by owner chmod 010 file - execute by group chmod 001 file - execute by world • To combine these, just add the numbers together: chmod 444 file - Allow read permission to owner and group and world chmod 777 file - Allow everyone to read, write, and execute file Text Editors • pico - Simple, easy-to-use text editor • vi - Text editor based on an underlying line editor ex • emacs - Powerful and extensible - Hard to learn vi • vi Modes: – Command mode : Normal and initial mode. Other modes return to command mode upon completion. ESC (escape) is used to cancel a partial command. – Input mode : Entered by setting any of the following options: a A i I o O c C s S R . Arbitrary text may then be entered. Input mode is normally terminated with ESC character. – Last line mode : Reading input for : / ? or !.Terminates by typing a carriage return Compiling and Linking • cc - C compiler - Default behavior is ANSI/ISO C • make - Allows programmer to maintain, update, and regenerate groups of computer programs. cc / gcc • Syntax – cc [-Options] ... file ... • Description – *.c are assumed to be C source programs. – *.o are compiled object files. – a.out is the default output program name. • Frequently Used Options – -c – -03 Produce an object file Invoke optimizer cc / gcc – -o out Name the final output file out. – -Dname Define the name to the C macro processor – -Idir Seek dir for include files • Examples – cc p1.c Handling Processes • ps : Prints information about active processes • kill : Sends a signal to a process ps • Syntax – ps [-Options] • Description – Prints information about active processes. • Frequently Used Options – – – – -A -e -f -l Every process now running Same as -A Full listing Long listing • Examples – ps -ef kill • Syntax – kill [-signal] pid . . . – kill –l (the letter ‘L’ in lowercase) • Description – – – – Sends a signal to the specified processes. The value of signal may be numeric or symbolic. Signal 15 is the default signal. kill –l lists the defined signals. • Examples – kill 389 – kill –3 401 402 – kill -HUP 99999 Archiving and Compressing • tar • gzip tar • A ".tar" file is not a compressed file, it is actually a collection of files within a single file uncompressed. If the file is a .tar.gz ("tarball") or ".tgz" file it is a collection of files that is compressed. • Syntax – tar key [arguments] [name...] • Description – Saves and restores multiple files on a single file. – The key argument controls tar's actions. • Frequently Used Functions – c – x – t Create Extract from the tape List files on the tape tar • Frequently Used Modifiers –v –f Verbose Next argument is the name of the archive • Examples – Creating a tarball: tar -cvwf file.tar myfile1.txt mypic1.jpeg – Extracting from a tarball tar -xvwf file.tar Other Useful Commands • grep: search files for a pattern • man : on-line reference manuals • wc : word, line and byte or character count grep • Syntax – grep [-E| -F] [-c| -l| -q] [-insvx] pattern_list [file . . .] • Description – Searches the input files, selecting lines matching one or more patterns • Frequently Used Options – -i – -l – -n Case-insensitive search Write file names only Display line number • Examples – grep -i unix p1.c – grep -n UNIX *.c *.h man • Syntax – man [-Options] [-M path] [-T macropackage] [ -s section] name ... – man [-M path] -k keyword ... • Description – On-line reference manuals • Frequently Used Sections – 1 User commands and application programs – 2 System calls – 3 Library functions • Examples – man -s 1 mkdir – man mkdir – man -k pipe wc • Syntax – wc [ -c|-m ] [ -lw ] [ file . . . ] • Description – Counts lines, words, and characters • Options – – – – -c -m -l -w • Examples Count Count Count Count – wc -l *.h *.c the number of bytes the number of characters the number of newline characters the number of words