ls: LiSt files• Used to list files contained in a directory • Can narrow the search using pattern matching • Examples – ls displays ‘all’ the files in the directory – ls cats display
Trang 1Linux Crash Course
Selected snippets from
CIS52
Trang 2Long ago, in a galaxy far away …
• Computing power was costly
– UNIVAC cost $1 million
• CPU time was a premium
– Most mainframes had less computing power than
a calculator on the shelf at Wal-Mart
• Jobs were submitted into a queue
– Only one process at a time – scheduling
nightmare
Trang 3What was needed
• Allow multiple users to access the same data and resources simultaneously
• Service many users more cheaply than
buying each their own machine
• The ability to run multiple processes at once
• And do so while maintaining user
segregation and data integrity
Trang 4Enter Unix, pride of Bell Labs
• Originally written in PDP-7 assembly
language by Ken Thompson
• To make it work on multiple architectures (portable), Thompson rewrote Unix in B
• Dennis Ritchie developed C, and with
Thompson, rewrote Unix in C
Trang 5What was so great about it?
• Multiuser
• Multiprocess
• Non-proprietary
• Economical for business
• Initially given for free to colleges and
universities (great tactic!)
Trang 6What happened?
• UNIX became commercialized
• Proprietary code, specialized distributions
• Costs started to become a hindrance
• So … let’s make our own Unix …
Trang 7• Richard Stallman decides that there should
be a free version of Unix available
• Forms the GNU project – GNU’s Not Unix
• Writes all of the system programs and
utilities to mimic Unix variants
• Everything but a kernel (Hurd)
Trang 8Final piece
• Universities trying to teach Unix and OS
design can’t afford Unix
• Andrew Tanenbaum writes Minix
• Linus Torvalds, dissatisfied with Minix, writes his own – Linux
Trang 9• Torvalds has a perfectly functioning kernel – but no system programs
• Finds a perfect candidate in GNU
• Together, the operating system world was
changed dramatically
Trang 11• Many useful utilities built-in
• Rich networking support
Trang 13Consoles and terminals
• Monitor, keyboard (and maybe mouse) attached
to the box
• The console often locked away in a closet or
server farm somewhere
• Terminals usually found in mainframe
environment
• Recently ‘thin clients’ gaining popularity again
Trang 14Terminal Emulation
• Using a separate system, connect a virtual
terminal to the server
• telnet, ssh, X Windows
• Most common way to interact with a
Linux/UNIX machine
• Ubuntu – Applications->Accessories->Terminal
Trang 15The Shell
• Command interpreter
• Translates commands issued by user into commands sent to the kernel
• Common shells: bash, tcsh, csh, zsh, ksh
• Linux default is bash (Bourne Again Shell)
Trang 16Administrative privleges
• root or superuser
• Full read/write access to filesystem
• Can execute privileged commands and programs
• Use sparingly and with extreme caution
Trang 17Admin Priv, con’t
• We’re using Ubuntu, which treats root in a unique fashion
• Don’t logon as root
• To execute privileged commands:
– sudo [command] or gksudo [command]
– Will prompt for your credentials – are you sure??
Trang 18Getting help
• Most GNU commands and utilities have built in help and usage information
• help (sometimes -h or -help)
• Too much information? Pipe results to less or
more
– ls help | less
Trang 19man Pages
• man program_name
• Displays online documentation, formatted with a pager
• SPACE to advance, q to quit
• Depending on the system sometimes you can also
use PAGE UP/DOWN
Trang 20Typing Commands
• Beware of special characters
• Characters that have special meaning to the shell
• Shell expands, modifies and interprets
special characters before issuing the
command
Trang 21Special Characters
• & ; | * ? ‘ “ ` [ ] ( ) $ < > { } ^ # / \ % ! ~ +
• Plus whitespace (tabs, spaces, newlines)
• Do not use these in filenames unless you
have to
• To use them, either put in single quotes,
or proceed with a backslash
– ls ‘filename with special chars!!’
– ls \[cat\]
Trang 22• Linux & Unix come with thousands of utilities
• Some used explicitly, others implicitly
• Some text-based, some GUI, some both
Trang 23Some tips before we start
• Tab completion
– When typing a filename or command name, you can type the first few letters then hit TAB to auto- complete the command
• Command history
– Use the up and down arrow keys to cycle
through recently executed commands
– You can edit them before issuing again
Trang 24ls: LiSt files
• Used to list files contained in a directory
• Can narrow the search using pattern
matching
• Examples
– ls displays ‘all’ the files in the directory
– ls cats displays the file cats in the directory – ls ca* displays files starting with ‘ca’
Trang 25cat: catenate a file
• Displays the contents of one or more files
• Beware – don’t try with binary files
• Examples
– cat myfile displays contents of myfile
– cat file1 file2 displays contents of file1
followed by contents of file2
Trang 26cp: CoPies files
• Usage: cp sourcefile destinationfile
• Creates a copy, leaves sourcefile intact
• If destinationfile exists, it will be overwritten
– Unless you use –i option
• Example:
– cp myfile myfile.backup
Trang 27mv: MoVe files / change name
• Usage: mv existingfile newfile
• Just like cp, can overwrite with –i option
• Renames a file, which can also move it to another directory
• Examples:
– mv myfile foshizzle
– mv /dir1/myfile /dir2/myfile
Trang 28Editing Files
• gedit filename
– Similar to notepad in Windows
• Do you have rights to edit?
– If a system/service file probably not
– gksudo gedit filename
– We’ll have to edit some config files manually; mostly we’ll try and use Webmin
Trang 29Hierarchical Filesystem
• Directories can contain other directories
and/or ordinary files
• Concept different from reality – in
implementation everything is a file
• Directories, devices, named pipes, ordinary files – all really just files
Trang 30• Root directory
• Subdirectories
• Parents, children
Trang 31• Each file within a directory must have a
wholly unique filename
• Can be up to 255 characters – make them longer to avoid confusion
• Special characters must either be escaped out (using backslash) or in quotes
• Only illegal characters are / and carriage return
Trang 33Hidden Files
• To make a file hidden, start it with a period
– Ex plan
• A normal ls will not show hidden files
• Use ls –a to reveal ALL files
• Startup files, containing configuration
settings for your account, hidden
Trang 34mkdir – create directory
• Syntax: mkdir directory
• Directory can be a relative or absolute
pathname (we’ll get to that in a minute)
• You can use ls –F to show directories with a
forward slash at the end of the name
• If using a color terminal, directories will be a different color than ordinary files
Trang 35Working Directory
• The directory you are currently working in
• pwd will tell you what your working directory
is
• Helpful to know when using relative
pathnames (again, coming up)
Trang 36Home Directory
• Not to be confused with working directory
• The directory you start in when you first logon
• Most users it is /home/username
• For root, it is /root
• Can be changed by system administrator
Trang 37cd – change working directory
• Syntax: cd [directory]
• Again, directory can be absolute or relative
• If no argument given, changes working
directory back to your home directory
Trang 38Absolute Pathnames (finally)
• Absolute pathname for a file gives the file’s location relative to the root of the filesystem
• Sometimes long
• Ex: /home/jhowell/Assignment1/animals
• Shortcut: ~ represents your home directory
• So the above could also be
~/Assignment1/animals
Trang 40and Directories
• is an alias for the working directory
• is an alias for the parent of the current
Trang 41Common Directories
• / (root) – root of the filesystem
• /bin – essential system binaries (commands)
• /boot – files for the bootloader
• /dev – device files
• /etc – system configuration files
• /home – user home directories
• /lib – standard libraries and modules
Trang 42Common con’t
• /mnt – mount point for temporary filesystems
(floppies, CD-ROMs, non-native partitions)
• /opt – optional add-on software
• /proc – kernel and process information
• /root – root’s home directory
• /sbin – essential system binaries
• /tmp – temporary space (not swap)
Trang 43Common con’t
• /usr – common area for data / program users
use frequently
• /var – frequently changing data like system
logs, caches, spools and mailboxes
• That said, all these can reside on the same partition
• Only / and a swap partition required
Trang 44• Use a ls –l (for long view) and you might see
something like this:
drwxr-xr-x 2 jhowell jhowell 4096 Aug 18 15:46 Desktop
-rw-rw-r 1 jhowell jhowell 0 Sep 4 18:08 myfile
drwxrwxr-x 2 jhowell jhowell 4096 Aug 22 15:32 public_html
Type
of file
File Permissions
# of links
Trang 45chmod – CHange MODe
• Changes permissions
• Syntax: chmod [ugoa][+-][rwx]
• Ex: grant everyone (all) read and write
chmod a+rw myfile
• Ex: remove execute permission for other
chmod o-x myfile
Trang 46• root can still read and write to files without
read and write permissions