Introduction to Linux Commands
Feb 25, 2016
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 Systemroot (/)
bin
dev etc
tmpvar
home
spool
(users’ mail)
log
usr
X11R6
bin
(users’ accounts)
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 : list files• cp : copy files• mv : move files• rm : remove files• mkdir : make directories• cd : change directories• rmdir : remove directories• pwd : print working directory• chmod : change permission mode
ls• Syntax– ls [-Options] [name ...]
• Description– Lists contents of directory.
• Frequently Used Options– -a List all entries, including . and ..– -d Do not list contents of directories– -l Long listing– -F Mark directories with a '/', etc.
• Examples– ls -alF
cp• Syntax
– cp [-Options] file1 [file2 ...] target• Description
– File1 is copied to target.• Frequently Used Options
– -f Force remove existing file– -i Ask before removing existing file – -r 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 Removal of files without prompting the user– -i Interactive removal– -r Recursive removal
• Examples– rm -f p*.o– rm -r mydir
mkdir• Syntax
– mkdir [-m mode] [-p] dirname . . .• Description
– Creates the specified directories.• Options
– -m Specifies the mode to be used – -p 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 Remove the directory dirname and its parent
directories which become empty.– -s 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 ownerchmod 040 file - Read by groupchmod 004 file - Read by world
chmod 200 file - Write by ownerchmod 020 file - Write by groupchmod 002 file - Write by world
chmod 100 file - execute by ownerchmod 010 file - execute by groupchmod 001 file - execute by world
• To combine these, just add the numbers together:chmod 444 file - Allow read permission to owner and group and worldchmod 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 Produce an object file– -03 Invoke optimizer
cc / gcc
– -o outName 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 Every process now running– -e Same as -A– -f Full listing– -l 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 Create– x Extract from the tape– t List files on the tape
tar
• Frequently Used Modifiers– v Verbose– f 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 Case-insensitive search– -l Write file names only – -n 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 Count the number of bytes– -m Count the number of characters– -l Count the number of newline characters– -w Count the number of words
• Examples– wc -l *.h *.c