intro to unix-1-2014 - indiana university...
TRANSCRIPT
Getting started with the Unix Operating System
Unix Introduction
What is an Operating System?
A computer Operating System, or
“OS”
What is a computer operating system?
Where?
Insulation from hardware
Allocation of resources
Utility Functions (often blurring the line between OS and applications)
What does an OS provide?
There are numerous past, present, and future storage technologies
Each of these requires unique procedures for getting data to and from the device.
But, it is unreasonable to expect each application to understand each device.
The problem
The OS provides simple "abstract devices” for storage:
Random access (disks, solid state)
Sequential access (tapes)
Applications talk to the abstract devices, the OS translates and talks to the actual devices. The OS provides a layer of insulation from these details.
The solution
Insulation roles of the OS extends to most aspects of computer hardware, including
User interface devices
Network and communications
Memory
Expansion busses
…
Beyond storage
OS is necessarily closely tied to particular hardware.
OS used to always be supplied by the hardware vendor.
OS-Hardware pairing
Insulation from hardware
Allocation of resources
Utility
What does an OS provide?
The disk abstraction represents a disk as quantity of storage.
An application is insulated from the hardware details of exactly how the data are stored.
An application can read or write from any location in that quantity of storage.
About that “disk” abstraction…
The disk abstraction provides no structure for data stored in the disk.
If multiple applications want to use the same disk, they must agree on a way to structure data.
The disk abstraction is a like file cabinet without file folders.
The problem
An OS provides higher level abstractions than just devices. For data storage:
File — a named container for data
Directory — a named container for files
The solution
An application manages data inside a file however it likes.
Things an application can do with a file:
create, open, seek, read, write, close, rename, remove.
The OS manages the storage of files on the disk for all applications.
Files and directories
The OS arbitrates application access to all system resources.
CPUs
Memory
Input and output devices
…
Beyond storage
Insulation from hardware
Allocation of resources
Utility
What does an OS provide?
Insulation from hardware and allocation of resources are core OS features, but alone don’t make for a useful system. This does not provide for a user interface.
Practicality leads real-world OS's beyond the core functions.
The boundary between operating systems and applications is open to interpretation.
Gray areas at the edge
Insulation from hardware
Allocation of resources
Utility
What does an OS provide?
Layers of softwareOS and other software are often depicted as functional layers.
Software in one layer can only use services provided by the immediately lower layer.
Graphic from The Design of the Unix operating System by Maurice J. Bach, Prentice-Hall, 1986
Introduction to Unix
Logging In
• Unix is a multi-user operating system. If you want to use its resources, you must first identify yourself to the system by logging in. This is called authentication.
Logging In
! The server we will be using for most of this class is accessible through the network by the name of iris. We use the full DNS name to distinguish it from any other server that may be named "iris". More on DNS later.
iris.ils.indiana.edu
Logging In
• You can login to a Unix system from any location in the world using any client that supports the network protocol "ssh". The secure shell (ssh) is a protocol that is used to create a secure channel over an insecure network.
• Invoking the ssh command will knock on a particular server door which in turn will prompt for a username and password. Once you successfully authenticate you have access to the system resources via this secure channel.
Logging In Practice• We will now use ssh to connect to our host, iris.
• Every modern operating system (Mac/Linux/Windows) has the capability of running a ssh client. Linux and Mac OS X are just different distributions of Unix and therefore have native support for this type of connectivity. Windows users can download one of the many free ssh clients. For OSX/Linux users, just launch the "Terminal" application and use the ssh command.
Logging In Practice
• In a Windows environment, launch your ssh client and enter the DNS name of our server: iris.ils.indiana.edu
• In an OS X or Linux environment, once you launch the terminal application type the command line below.
$ ssh [email protected]
Kerberos Authentication
• You will be asked for your kerberos password, which should be the same password you used to log in at this lab.
• Kerberos is used campus-wide as a secure way to authenticate across an insecure network. You and the computer you connect to prove that you are who you say you are without actually sending passwords across the network.
Logging In
• Once successfully logged in, a Unix system typically tells you when you logged in last and from where, shows you any implemented disk quota, gives you some arbitrary text written by the system administrator, provides you with a command shell prompt, and then waits for your commands.
Logging In• Example: Last login: Mon Aug 4 15:20:54 2008 from chionofuji.ils.indiana.edu
Disk quotas for stevecox (uid 2238):
Filesystem usage quota limit timeleft files quota limit timeleft|-----------------------------------------------------------------| | This system is for the use of authorized users only. | | Individuals using this computer system without authority, or in | | excess of their authority, are subject to having all of their | | activities on this system monitored and recorded by system | | personnel. | | | | In the course of monitoring individuals improperly using this | | system, or in the course of system maintenance, the activities | | of authorized users may also be monitored. | | | | Anyone using this system expressly consents to such monitoring | | and is advised that if such monitoring reveals possible | | evidence of criminal activity, system personnel may provide the | | evidence of such monitoring to law enforcement officials. | | | |SLIS computing accounts expire: | |See: http://www.slis.indiana.edu/technology/help/ for details | |-----------------------------------------------------------------| stevecox@iris:~ $
Logging In• You are left with a prompt at the bottom of
your login screen.
• A command prompt generally ends with either a dollar sign ($) or a percent sign (%). There are two main types of command shells; those based on a Bourne shell (i.e. sh and bash) and those based on a C shell (i.e. csh and tcsh).
Logging In• To keep you straight on which you are using,
the two shells traditionally use different prompts:
$ for Bourne shells
% for C shells.
• Your account on iris uses a Bourne shell derivative (bash to be specific).
Installing your Practice Directory
• During this module you will be using a practice directory structure complete with practice files and directories.
• You will now install this into your home directory on iris.
Getting your Practice Directory• Make sure you are logged into iris and type
these commands:
$ cd (press return)
$ tar -xvf /tmp/Unix_class.tar
• You will see a list of filenames and directories scroll by. You will learn more about the command you just used on your last day of this module.
Unix Commands
• Now you are logged into the system, but you need to know how to talk to the system or issue your commands.
Unix Commands
• There are a number of commands which you need to know in order to tell the system to do various things (show you directories, move files, copy files, search for files, etc.).
Unix Commands
• Unix commands generally have three parts or syntactic elements:
command_name
Example:
$ ls
• Tells the system to show you a list of files
Unix Commands
• Unix Commands generally have three parts:
command_name [options]
Example:
$ ls -l
• List files, in “long format” form (gives more information about the files)
Unix Commands
command_name [options]
Note: In some texts and web links, options can also be referred to as “flags”.
Unix Commands
• Unix Commands generally have three parts:
command_name [options][operands]
Example:
$ ls -l Unix_class
• List files, in “long format” form, in the directory called ‘Unix_class’
Unix Commands
• Unix Commands generally have three parts:
command_name [options][operands]
Note: In some texts and web links, operands can also be referred to as “arguments”
Unix Commands
• Not all commands will need all three parts. In fact, some will only need the command_name. However, others may require two [operands]
Unix Commands
Important to Remember:
• Unix is case-sensitive; old.files is different from Old.Files.
• The spaces between the command_name, the [option] and the [operand] are VERY important.
who - Practice
• One simple Unix command is used to find out who is currently logged in. At the shell prompt (the $), type who.
$ who
who - Practice• Example Output
$ who
herring pts/12 Jun 17 15:36 (herring.slis.indiana.edu)
mnapier pts/4 Jun 14 21:29 (12-222-70-96.client.insightbb.com)
mnapier pts/8 Jun 17 07:39 (dmitrii.slis.indiana.edu)
jfieber pts/5 Jun 17 15:56 (d-1-1.dhcp-149-159.indiana.edu)
htripath pts/3 Jun 17 11:10 (lair4.slis.indiana.edu)
mnapier pts/9 Jun 16 09:00 (12-222-70-96.client.insightbb.com)
stevecox pts/10 Jun 17 16:51 (wireless-73-28.wireless.indiana.edu)
stevecox pts/13 Jun 17 16:34 (chionofuji.slis.indiana.edu)
$
Unix Directories
Directories
• The information stored in the Unix system is hierarchical.
• Like the “folders” on a Mac or Windows operating system, Unix works with “directories.”
Directories
• A directory can contain either files or more directories (often called subdirectories). There are a number of common Unix directories found on most systems.
Directories
Directories
• All files and directories on the system reside under the root directory, the [ / ]. Where other directories have real names, the name of the directory at the very top of the Unix OS is the forward slash (/).
Directories
• The ‘home’ directory is your personal subdirectory under /home. It is where you will store all of your personal data.
Example: /home/jsmith
Directories
The ‘working’ or ‘current’ directory is whatever directory you are in at the moment.
Basic Unix CommandsA Few Commands and
Concepts To Get Us Started
pwd - ‘print working directory’
• Typing pwd will display a list of directories separated by a forward slash (/). This is a pathname. The first slash refers to the root directory, then each subdirectory is listed, and the last item is the name of the directory you are currently in (your working directory).
$ pwd /home/jsmith
pwd - Practice
• What is the pathname of your current directory?
Example: /home/stevecox
ls - ‘list’
• The command_name ls alone will list the names of the files in the current (or working) directory.
$ ls Unix_class
ls - ‘list’
• ls [operand] will list the names of the files in the specific directory asked for.
$ ls /home ajkurtz htripath klabarre cdulude jfieber mbswan herring katy stevecox
ls - PracticeHow many files and subdirectories are in your current directory?
How many files and subdirectories are in /home/userid/Unix_class ?
$ ls
(Unix_class) & (misc. files)
$ ls /home/userid/Unix_class
5 (Shakespeare, Wildcards, dir1, dir2, equine)
cd - ‘change directory’
• When you use the command, ls, you are just looking at the directories, you have not actually ‘moved’ (you stay at the same working directory).
• The command, cd, will let you move your current directory to a new location.
cd - ‘change directory’
• The command_name cd alone will always move you to your personal home directory (on /home).
$ cd
cd - ‘change directory’
• If you supply an operand to the change directory command, it will move you (your current directory) to the specified directory.
$ cd Unix_class
cd - ‘change directory’
• Note: Getting around a hierarchical filesystem is very important to grasp.
• If the directory you wish to move to is a subdirectory of your current directory, then you only need to type a pathname starting from where you are.
$ cd Unix_class/dir2/cats
This is called a relative pathname.
cd - ‘change directory’• But, if the directory you wish to move to is
not a subdirectory of your current directory, then you need to type a pathname starting from the root (/).
$ cd /var/adm/log
This is called an absolute pathname. Some texts will refer to this as a “full” pathname.
The meaning is the same.
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/If you are in jsmith,
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/If you are in jsmith,and you want to go to dir1
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
You only need a relative pathname, because dir1 is ‘under’ you.
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd Unix_class/dir1
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/If you are in jsmith,
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/If you are in jsmith,and you want to go to log
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
You need an absolute pathname, because log is not ‘under’ you.
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd /var/adm/log
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
If you are in Unix_class and you need to access grp_project, how would you get there?
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd /home/kadams/grp_project
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
But, if you are in home and you need to access grp_project, how would you get there?
Pathnames - Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd kadams/grp_project
Pathnames - Example
cd - Practice• Change your current directory to this directory:
/home/userid/Unix_class/dir2
Now go back to your home directory.
References to your home directory
• When you login, Unix always knows where your home directory is.
• There are a few ways to return to the top of your home directory
/home/userid
References to your home directory
~ (tilde) symbol represents your home directory so:
$ cd ~
will always return you to your home.
$ cd ~userid
This will lookup the information for another user and find their home directory location and take you there if you are allowed.
.. - ‘parent directory’
• As we mentioned earlier, the information stored in the Unix system is hierarchical.
• At any time, the directory which is right above you (closer to the root directory) is often called the ‘parent’ directory.
• .. is an [operand] which “signifies” the parent directory of your current directory. It will be used in place of an operand.
.. - ‘parent directory’
bin etc home sbin
bin
bin man
local
man
share
usr var
/
• The parent directory of ‘man’ is ‘local,’ and the parent directory of ‘local’ is ‘usr.’
.. - ‘parent directory’
• Typing cd .. will move your working directory up one level, into the parent directory of where you were.
$ cd ..
.. - ‘parent directory’
• What is the pathname of your home directory?
• What is the pathname of the parent directory of your home directory?
/home/userid
/home
.. - ‘parent directory’
• Typing ls .. lists everything else in your parent directory – files and directories parallel with your working directory.
$ ls ..
Example
sbaker
mail docs temp
jsmith stevecox
home
Example
Suppose ‘jsmith’ is your current directory.
sbaker
mail docs temp
jsmith stevecox
home
Example
$ pwd
/home/jsmith
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls
mail docs temp
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls
mail docs temp
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls
mail docs temp
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls
mail docs temp
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls
mail docs temp
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls ..
sbaker jsmith stevecox
sbaker
mail docs temp
jsmith stevecox
home
Example
$ ls ..
sbaker
mail docs temp
jsmith stevecox
home
sbaker jsmith stevecox
Example
$ ls ..
sbaker
mail docs temp
jsmith stevecox
home
sbaker jsmith stevecox
Example
$ ls ..
sbaker
mail docs temp
jsmith stevecox
home
sbaker jsmith stevecox
Example
$ ls ..
sbaker
mail docs temp
jsmith stevecox
home
sbaker jsmith stevecox
Example
$ cd ..
sbaker
mail docs temp
jsmith stevecox
home
Example
$ cd ..
Moves your current directory to /home.
sbaker
mail docs temp
jsmith stevecox
home
.. - ‘parent directory’
• You can also use ‘..’ to move ‘up, over, and down’ to a new directory somewhere else.
• Otherwise you would have to use an absolute pathname.
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/If you are in jsmith,
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
If you are in jsmith,and you want to go to dir1
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd ..
At this point, you have moved up to home
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd ../kadams
Now you have moved down to kadams
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd ../kadams/grp_projectAnd now you are in grp_project
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/Now that you are in grp_project, you can move to stevecox
.. Example
grp_project
kadams
dir1 dir2
UNIX_class
jsmith stevecox
home bin
log
adm
var
/
$ cd ../../stevecoxNow that you are in grp_project, you can move to stevecox
man - ‘manual’
• Can’t remember what a command does?
• You can type:
$ man command_name
• This will give you Unix’s complete online manual.
man - ‘manual’
• Don’t just rely on the manual though. This would be a very time-consuming way of using Unix, and also the manual can be pretty difficult to understand.
• Note: use the space bar to move forward a page, and the ‘b’ key to move back a page
• A manual page will usually include:
• Title: the page # and the name of the system that the command is part of
• Name: usually the command and a brief description
• Synopsis: how to type the command, and an example of every option available.
man - ‘manual’
• Description: a few paragraphs (or pages) about the command and its options
• See Also: lists names of related manual pages, if any
• Warnings and Bugs: if any are known
• Files: a list of the files this command uses (for specific commands - like mail)
man - ‘manual’
man - PracticeBrowse through the man page for the cd
command. $ man cd Now browse through the man page for the pwd
command. $ man pwd
Notice how much larger the manual is for cd (which is a more complex command).
You can type ‘q’ to leave man
• If you forget how man is laid out, you can always type: $ man man
man - ‘manual’
Review
• pwd - tells you where you currently are
• ls - list the files & subdirectories of your current directory
• cd - changes your current directory
• man - displays the manual pages
• .. - signifies the parent directory of your current directory. It is NOT a command, it is an operand (argument).
Combining Commands
• If you don’t remember the name of the directory you want to move to, then use ls to see all the names of your working directory, then cd to the correct one.
Practice
• What is the absolute pathname to cats? (in dir2)
/home/userid/Unix_class/dir2/cats
Practice
eight $ ls Unix_class/Shakespeare
How many files are in the Shakespeare directory?
Practice
• What option is needed to list the files in a directory, including those that are normally not listed (begins with a . )?
Practice
• $ man ls
-a Lists all entries, including those that begin with a dot (.), which are normally not listed.
Unix Shells
Our Interactive Interface
Shells
• The shell is how you interact with the Unix system.
• This is the means in which you give the system commands.
• You can customize your environment, enter and reenter commands and provide shortcuts to save on typing.
• The shell can also be a flexible scripting language allowing you to create executable programs for you and others to use.
Shells
•While there are several different shells to choose from, the bash shell is an excellent choice for interactive and non-interactive use. It is the default shell here at SLIS.
•This is a public domain shell written by the Free Software Foundation under their GNU initiative.
Bash Shell
Bash shell feature:
• Command Line Completion
A very nice feature of modern shells is command line completion. You type part of your command line and then hit the tab key and the shell will try and complete the command line for you. You will use this most often with operands.
Command Line Completion
While in your home directory, move to your Unix_class directory.
•Instead of typing:
$ cd Unix_class
•You could type:
$ cd U (tab)
The shell’s command line completion takes over.
Command Completion Practice
• For this to work as the example shows you could not have another directory name starting with “U”.
• Command line completion only works if there is only one possible match to the letters or numbers that you enter before you hit the tab key.
Command Line Completion
•Remember, Unix is case sensitive so if you were to type:
$ cd u (tab)
it would not find your directory using command line completion
Command Line Completion
Session History
• Another nice shell feature that you will use frequently is session history.
• This lets you easily recall your frequently used command lines and use them again.
Session History Practice
• Press your (up) arrow one time • Your last command line should appear at your prompt.
• If you would like to use that command line again, just hit enter.
• Do not hit return on a command line if you do not intend to complete the action.
• You can backspace/delete over the command line OR type Ctrl+U (^U).
• You can continue to use your up arrow or down arrow until you find the command line that you would like to reuse and then press enter.
Session History Practice
Session History
Use Session History for:
• Reviewing the command lines you’ve used during your session
• Using previous command lines again, rather than retyping them
• Modifying complex command lines
Metacharacters[ ] < > { } ( ) ‘ “ *
? | / \ ̂ ! # $ % @ & ~ ;
MetacharactersA metacharacter is a single character that is used in place of another character or set of characters.
To the shell, they represent something other than the character or symbol that they are.
Therefore, you cannot use metacharacters in the name of a file or a directory.
Metacharacters [ ] brackets < > angle brackets { } braces ( ) parentheses ' single quotes " double quotes * asterisks ? question marks | pipe symbols / \ slashes
^ carets ! exclamations # pound signs $ dollar signs % percent signs @ at signs & ampersands ; semi-colons ` accent marks ~ tildes
Metacharacters
Other keys also have specific meaning to Unix:
Pressing <return>Pressing <tab>
Naming Files & Directories
When naming a file or directory, you cannot use any metacharacters or spaces.
However, you can use periods (.) and underscores (_).
In fact, if you start a file with a period (.) it will be a ‘hidden’ file which does not show with ls (you must use ls -a).
Manipulating Files and Directories
Making Renaming Copying Removing
Moving
Making A New Directory
You can create a new directory with the mkdir command.
$ mkdir new_directory_name
Making A New Directory
You should start by typing pwd to make sure you are in the right place (you will create a subdirectory in whatever directory you are in).
Then you should type ls to check and make sure your new directory will not conflict with an already existing name.
Making A New Directory
Then enter the command line:
$ mkdir new_directory_name
to add your directory.
Then you should type ls to check and make sure your new directory will not conflict with an already existing name.
Making A New Directory
Make a new directory in Unix_class called dir3
$ cd Unix_class $ pwd $ ls $ mkdir dir3 $ ls
Practice:
Copying a File or Directory
You can copy an existing file or directory with the cp command.
$ cp existingfile newfile
$ cp -r existingdir newdir
• The -r is necessary when copying a directory; we will talk more about these types of options later.
Copying a File or Directory
When copying, moving, renaming, etc., the source will always come first, and the destination second.
Or, they are sometimes referred to as:
$ cp source destination
$ cp -r source destination
Copying a File or Directory
Go to the directory where the file is located:
$ cd pathname
Verify the file is there (and the spelling):
$ ls
Copy the file to a new name:
$ cp existingfile newfile
Copying a File or Directory
Also remember to list your directory (ls) first, to verify that your new file name does not already exist – if it does then your new copy will simply overwrite it!
Copying a File or Directory
Copy the file final.paper (in dir2) and name the new file “gerbils.”
Practice:
$ cd dir2 (or Unix_class/dir2) $ ls $ cp final.paper gerbils $ ls
Moving a File or Directory
You can move a file or directory with mv. Unlike cp, after using mv you will still only have one copy of the file or directory.
$ mv filename newpathname
$ mv directoryname newpathname
Moving a File or Directory
You can move a file or directory with mv. Unlike cp, after using mv you will still only have one copy of the file or directory.
$ mv source destination
$ mv source destination
Moving a File or Directory
For example, if you typed:
$ mv equine dir2
You would be moving the file equine into the dir2 directory.
$ mv filename newpathname
Moving a File or Directory
If you are using mv to move the file or directory to a different location you may want to consider copying it first and then deleting the original.
Moving a File or Directory
Always remember to ls the destination directory to verify there will be no conflict of names. If you attempt to move a directory to another location that does not exist, your directory will be renamed, not moved.
Moving a File or Directory
Go to the directory where the file is located:
$ cd pathname
Verify the file is there (and the spelling):
$ ls
Move the file to its new location:
$ mv filename newpathname
Moving a File or Directory
Move the file gerbils (in dir2) to your new directory (dir3).
Practice:
$ cd dir2 (if not already there)
$ ls $ mv gerbils ../dir3 (or) $ mv gerbils ~/Unix_class/dir3
Moving a File or Directory
All operands are independent of the commands that require them.
We use the ‘ls’ command to list directories but we’ll need to supply an operand. How about the destination directory?
How do you know that it worked?
$ ls ../dir3
Renaming a File or Directory
$ mv oldfilename newfilename
$ mv olddirname newdirname
Renaming a file or directory is really just moving it to a different name. There is no “rename” command.
Renaming a File or
Renaming a file or directory is really just moving it to a different name. There is no “rename” command.
Example: You have a file named costs and you
want to change the name to expenses.
Renaming a File or Directory
$ mv costs expenses
Renaming a file or directory is really just moving it to a different name. There is no “rename” command.
Renaming a File or Directory
Go to the directory where the file is located:
$ cd pathname
Verify the file is there (and the spelling):
$ ls
‘Move’ the file to its new name:
$ mv oldfilename newfilename
Renaming a File or Directory
Rename the file gerbils to furry.rodents
Practice:
$ cd ../dir3 $ ls $ mv gerbils furry.rodents $ ls
Renaming a File or Directory
Another reason to use mv is to correct a misspelled file name.
$ mv apr.bugdet apr.budget
Renaming a File or Directory
You can move and rename a file at the same time:
$ mv oldfilename newpathname/newfilename
Example: $ mv fall_classes ~/oldfiles/fall_2002 (The oldfiles directory has to already
exist)
Renaming a File or Directory
** Important! ** When using mv with directories…
$ mv oldname newname
If there already is a directory with the newname, then the oldname will become a subdirectory of the newname (the “rename” becomes a “move”).
Renaming a File or Directory
** Important! ** When using mv or cp with files…
$ mv oldname newname
If there already is a file with the newname, then the oldname will overwrite the newname.
Removing a File or Directory
You can remove (delete) a file with the rm command.
$ rm filename
You can remove (delete) an empty directory with the rmdir command.
$ rmdir directoryname
Removing a File or Directory
To remove a directory, you must first delete all the files in it:
$ rm filenamesThen move into the parent directory: $ cd .. Then delete the directory: $ rmdir directory
Removing a File or Directory
Remove the directory dir3
Practice:
$ rm furry.rodents $ cd .. $ rmdir dir3 $ ls
Removing a File or Directory
You can also remove more than one file at a time:
$ rm fileone filetwo filethree
Wildcards? *
Wildcards
There are two metacharacters which can act as a substitution for unknown letters or numbers in a filename or directory name.
? *
? Wildcard
The wildcard ? (question mark) can be a substitute for any single letter, number, or character.
? Wildcard Practice
$ ls sonnet? (in Shakespeare dir.)
This will list any filename that starts with sonnet and has exactly one character after sonnet.
The wildcard ? can be a substitute for any single letter, number, or character.
? Wildcard Practice
The wildcard ? can be a substitute for any single letter, number, or character.
$ ls sonnet?
This will not list: sonnet10 sonnet.favorite Sonnet8 (capital S)
* Wildcard
The wildcard * (asterisk) can be a substitute for any number of letters, numbers, or characters.
* Wildcard Practice
The wildcard * can be a substitute for any number of letters, numbers, or characters.
$ ls sonnet*
This will list all names that begin with sonnet (and would include sonnet by itself). It will still not list Sonnet8
Wildcard Practice
$ cd ../Wildcards $ ls let*3 letter3 letterhome.13 lettering.type3 lettuce.file3
Wildcards can specify a group of letters or numbers.
Wildcard Practice
$ ls *13 email_mgs_1to13 letterhome.13 shell.script.113 testfile.8513
Wildcard Practice
$ ls *file* filet.mignon.recipe lettuce.file3 old.file sufiletters testfile.8513
Wildcard Practice
$ ls s* secret.ideas shell.script.113 shell.script.276 stuff sufiletters
(Remember that Unix is case
sensitive, so the file Sam would not
be found)
Wildcard Practice$ ls ???
Sam fl9 h.d kid pix
Wildcard Uses
You can use wildcards with commands other than ls.
Wildcard Uses
To move all files that begin with "assgn" (assgn3, assgn12, etc.) to the old.files directory, you can type:
$ mv assgn* old.files
The directory old.files has to already exist.
Wildcard Uses
To copy all the files from the current directory into a directory called temp, you can type:
$ cp * temp
The directory temp has to already exist.
Wildcard Uses
To remove all files with a certain ending, you can type:
$ rm *.txt
Wildcard Uses
However, before you remove them, type:
$ ls *.txt
This way you can make sure there is nothing there you did not want to delete.
Wildcard UsesHowever, DO NOT TYPE this line!
This is a typo to avoid!
The extra space means you would remove everything in the directory!
$ rm * .txt
↑Very important!!!!
Wildcard Practice
Create another directory in Unix_class called Poems
$ cd ..
$ pwd (verify you are in Unix_class)
$ mkdir Poems $ ls
Wildcard Practice
Copy everything from Shakespeare into Poems.
$ cd Shakespeare
$ cp * ../Poems
* - source (in this case, everything)
../Poems - destination
Wildcard Practice
Or, this is another way to achieve this: $ mkdir Poems
$ cd Poems
$ cp ../Shakespeare/* .
../Shakespeare/* - source
. - destination
(the single period represents your current directory)
Wildcards
Wildcards apply to all commands and are used in place of or in combination with operands.
Ready for more?