Table of Contents
1.1 Introduction to Unix Commands………………………………………………………………………………………………4 1.1 – Section 1: Introduction to Unix/Linux………………………………………………………………………………….4 Unix & Linux Operating Systems…………………………………………………………………………………………….4 Shells………………………………………………………………………………………………………………………………… 4 1.1 – Section 2: Basic Unix/Linux Commands………………………………………………………………………………4 pwd – Print Working Directory……………………………………………………………………………………………….4 Listing Files…………………………………………………………………………………………………………………………4 Changing Directories…………………………………………………………………………………………………………….5 whoami Command………………………………………………………………………………………………………………5 who Command……………………………………………………………………………………………………………………5 date Command……………………………………………………………………………………………………………………5 cal Command………………………………………………………………………………………………………………………5 1.1 – Section 3: Review Exercise………………………………………………………………………………………………..5 1.2 Basic Unix Commands…………………………………………………………………………………………………………….6 1.2 – Section 1: Unix Command Structure…………………………………………………………………………………..6 Unix Command Structure……………………………………………………………………………………………………..6 Type of Arguments……………………………………………………………………………………………………………….6 Positional Arguments………………………………………………………………………………………………………..6 Simple Options…………………………………………………………………………………………………………………6 Options with a Value…………………………………………………………………………………………………………7 Spaces Between Arguments………………………………………………………………………………………………….7 Using Options……………………………………………………………………………………………………………………..7 1.2 – Section 2: Entering and Editing Commands…………………………………………………………………………7 1.2 – Section 3: Using the Online Manual……………………………………………………………………………………8 Online Manual Command……………………………………………………………………………………………………..8 Finding Commands………………………………………………………………………………………………………………8 1.2 – Section 4: Managing Files…………………………………………………………………………………………………8 Creating Empty Files…………………………………………………………………………………………………………….8 Hidden Files………………………………………………………………………………………………………………………..8 Copying Files……………………………………………………………………………………………………………………….8 Renaming (Moving) Files………………………………………………………………………………………………………9 1.2 – Section 5: Accessing Files………………………………………………………………………………………………….9
Displaying File Contents………………………………………………………………………………………………………..9 cat Command…………………………………………………………………………………………………………………..9 more Command……………………………………………………………………………………………………………….9 head and tail Commands…………………………………………………………………………………………………..9
Modifying Display………………………………………………………………………………………………………………..9 sort Command…………………………………………………………………………………………………………………9 uniq Command………………………………………………………………………………………………………………..9 diff Command………………………………………………………………………………………………………………..10 grep Command………………………………………………………………………………………………………………10
1.2 – Section 6: Review Exercise………………………………………………………………………………………………10 1.3 Directory Management…………………………………………………………………………………………………………11 1.3 – Section 1: Unix Filesystem Hierarchy…………………………………………………………………………………11 Unix Filesystem Hierarchy……………………………………………………………………………………………………11 Directory Terminology………………………………………………………………………………………………………..12 Important things to note about Unix file hierarchies……………………………………………………………….12 Naming Files (and Directories)……………………………………………………………………………………………..12 1.3 – Section 2: Pathnames (Absolute, Relative, Relative-to-Home)………………………………………………12 Pathnames……………………………………………………………………………………………………………………….. 12 Absolute Pathnames……………………………………………………………………………………………………….13 Relative Pathnames………………………………………………………………………………………………………..13 Relative-to-Home Pathnames…………………………………………………………………………………………..14 1.3 – Section 3: Ambiguous Pathnames…………………………………………………………………………………….14 Ambiguous Pathnames……………………………………………………………………………………………………….14 The Asterisk *………………………………………………………………………………………………………………..15 The Question-Mark ?………………………………………………………………………………………………………15 The Character-Class………………………………………………………………………………………………………..15 Globbing………………………………………………………………………………………………………………………. 16 Demonstration………………………………………………………………………………………………………………. 16 1.3 – Section 4: Directory Management Commands…………………………………………………………………..17 Create Directories………………………………………………………………………………………………………………17 Change Directory……………………………………………………………………………………………………………….17 Check Current Directory……………………………………………………………………………………………………..17 Remove Directories……………………………………………………………………………………………………………17
1.3 – Section 5: Review Exercise………………………………………………………………………………………………18 1.4 Practice Commands to Create a Directory Structure……………………………………………………………..19 1.5 Create a Directory Structure………………………………………………………………………………………………24 1.6 Practice Specifying Pathnames…………………………………………………………………………………………..26
1.1 Introduction to Unix Commands 1.1 – Section 1: Introduction to Unix/Linux
Unix & Linux Operating Systems
All computer systems require an operating system to control the computer hardware, to schedule important routine tasks, and to allow the user to interact with the computer system.
The Unix & Linux operating systems has many features. Unix accommodates multiple users, provides for user communication, allows for sharing of files, and provides easy access to the Internet.
Unix is a family of operating systems, (unofficially) including Linux. You will find slight differences but many similarities between the various versions of Unix and Linux.
Shells
One method to communicate with the UNIX and LINUX operating systems is to enter commands (or program names) at a ‘shell prompt’.
The shell is an interface between the Unix/Linux operating system and the user. When the shell is in operation, it provides a prompt and waits for the user to type in a command and press the
o The shell will not run or “execute” a command that the user types until the
You can backspace at a shell prompt by pressing one of these keys: o
o
o
o Note: Different ones will work on different computers. (Sometimes
1.1 – Section 2: Basic Unix/Linux Commands
Unix commands are generally entered in lowercase.
o E.g.UsingPWDinsteadofpwdwillresultintheerrormessage:“PWD:commandnot
found”
pwd – Print Working Directory
The output of the ‘pwd’ (Print Working Directory) command shows the ‘directory’ (or ‘folder’) which you are currently in.
Listing Files
You can view a list of files (in your current directory) with the ‘ls’ (list) command. o Youcanuse‘ls-R’tolistdirectorytreerecursively
Listing Files with Detail:
o The ‘ls’ command does list the names of your files, but it doesn’t tell you anything
about them. In order to provide more information regarding each file contained in your home directory, issue the command ‘ls -l’.
‘ls -l’ and ‘ls’ are the same command, but the addition of the ‘-l’ has changed the way that ‘ls’ works.
o Any additional information that you give to a command is called an argument; an argument that changes the way a command works is a special type of argument called an option.
Changing Directories
You can change directories to make another directory your current (or ‘working’) directory. The command to do this is ‘cd’.
o If you don’t specify the directory when using the ‘cd’ command, your home directory will be made the current directory.
cd command doesn’t produce any output if it is successful. Most Unix commands are designed not to produce unnecessary output, so they will not produce a message if it is not required and no error occurred.
whoami Command
‘whoami’ (or ‘who am I’) displays your user ID.
who Command
The ‘who’ command shows the names of users that are currently logged in, and depending on which Unix/Linux system you are using, may also show the terminal they’re logged in on, and the time they logged in.
date Command
The ‘date’ command will display the current time and date.
cal Command
The ‘cal’ command will display a calendar for just the current month.
o To display an entire calendar for the year 2018, enter ‘cal 2018’
o To display the calendar for a specific month of a year use the command “cal month
year”
E.g. ‘cal 8 1963’ displays the calendar for August 1963
1.1 – Section 3: Review Exercise
Question 1: Issue a UNIX command to display your Login username. whoami
Question 2: Issue a command to display the current time and date. date
Question 3: Issue a command to display the calendar for the month of February 2025. cal 2 2025
Question 4: Issue a command to display a list of files in your current directory. ls
Question 5: Issue a command to display the users currently logged into the system. who
1.2 Basic Unix Commands
1.2 – Section 1: Unix Command Structure
Unix Command Structure
command [ argument1 ] [ argument2 ] … [ argument n ] NOTE:
o An argument (or parameter) is additional information given to a command program, such as command options, filenames, strings of text, numbers or other elements that the command needs.
o One or more spaces or tabs must appear between arguments and between the command and the first argument.
o Indocumentation,square-brackets[]indicatethatanargumentisoptional.
Type of Arguments
There are three types of arguments: o PositionalArguments
o SimpleOptions
o OptionswithaValue
Positional Arguments
Positional arguments are expected to appear in a certain sequence. For example, the ‘copy’ command is written:
cp original destination
o The first file listed is the original file (that you are copying FROM) and the second file is the destination (that you are copying TO). If you change the position of the arguments, you change their meaning.
Positional arguments generally tell the command what it is to operate upon (for example, the file to be managed).
Simple Options
Options are arguments which modify the way in which a command operates. Options usually start with a dash or a double-dash.
o Forexample,the’ls’commandacceptstheoption’-l’.Whenthatoptionis entered, ‘ls’ changes its operation to give a longer output.
Generally, in Linux, options may be specified anywhere on a command line, and do not interfere with positional arguments. In Unix, options must often be specified before positional arguments.
Options with a Value
Some options need an additional value. This lets you specify details of how you want a command’s operation to be changed. This is like an option combined with a positional argument.
o As an example, the ‘sort’ command has an option ‘-k’ which lets you specify the number of the field to sort on. The ‘-k’ option must be immediately followed by the number of the field.
For example, ‘sort -k 5’ would sort a file by the fifth field instead of the entire record.
Spaces Between Arguments
Having multiple spaces have no effect on the operation of the command. You can leave as much space as you want between arguments.
Using Options
You can display detailed information about a file by issuing the command ‘ls -l’. You can also add a positional argument specifying the directory to be displayed.
ls -l /home/yczhao1
o Noticetheadditionalinformationcomparedtothe’ls’commandwithoutthe’-l’option. o An option is not a positional argument, so the ‘ls -l /home/yczhao1’ command with the
arguments switched produces the same result. You can combine options or use separate options.
o For example, you could get a detailed listing (-l) of both hidden and non-hidden files (-a) with any of the following commands:
ls -al ls -la ls -a -l ls -l -a
1.2 – Section 2: Entering and Editing Commands
There are a number of keys that may be used to enter and edit commands on Unix servers. Unfortunately, the keys you issue will depend upon your account configuration and which shell you are currently using.
o OnLinux,usingbash:
o AdditionalKeystrokes
1.2 – Section 3: Using the Online Manual
Online Manual Command
There is an online manual command in Linux and Unix called ‘man’. You can get help about a command by entering: man command
o Example:Issuethecommand’manls’todisplayonlinehelpregardingthels command
o Important: When you are in the man command, press
Finding Commands
‘man’ is fine if you know which command you want to use, it’s sometimes difficult to remember (or find) the right one to use.
‘man’ can also locate a command if you give it a keyword, using the -k option: man -k keyword
o A brief description of all the commands with the keyword in their summary will be shown.
1.2 – Section 4: Managing Files
Creating Empty Files
You can use the touch command to create empty files.
o Example: “touch file1 file2 file3” will create empty files with the specified names in
the current directory
o Example: “touch a_directory/file1” will create the empty file with the specified
name in the a_directory which is inside the current directory
Hidden Files
Files with names that start with a period ‘.’ are considered hidden (Hidden files) and not usually displayed by ‘ls’. This feature is used to hide files that you don’t want to see all the time, such as configuration files.
There is an option ‘-a’ (for ‘all’) to view both hidden files (filenames beginning with a period ‘.’) and non-hidden files.
o There is a file named Dot ‘.’ which represents the current directory, and double-dot ‘..’ represents the parent directory.
Copying Files
The cp command is used to copy files. Command structure: cp source-file destination-file
o Example: “cp file1 file4” will create a copy of file1 named as file4 in the current directory
Renaming (Moving) Files
The ‘mv’ (‘move’ to a new name) command is used to move and rename files. Command Structure:
mv [source-file] [destination-file]
or
mv [old file-name] [new file-name]
o Example:
“mv file2 file2a” will rename file2 in the current directory to file2a
“mv file2 directory” will move file2 in the current directory to the directory
in the current directory
1.2 – Section 5: Accessing Files
Displaying File Contents
cat Command
The ‘cat’ command will dump the contents of the file on to the display.
more Command
To view files larger than the terminal window, you can issue the ‘more’ command. This command allows the user to move throughout the file using movement keys that are similar to the man command.
o When using ‘more’, press
head and tail Commands
Use the ‘head’ and ‘tail’ commands to view a number of lines at the beginning and end of files respectively.
o Specify the number of lines that will be displayed from the top or bottom of the file by using an option -# where # represents the number of lines to be displayed.
Modifying Display
sort Command
The sort command is used to sort contents of a file. Command Structure: sort [options] filename
uniq Command
The uniq command is used to display only one occurrence of identical adjacent lines. If you want to prevent duplication of identical lines that are NOT adjacent, the file should be sorted prior to using this command (or use sort -u). Command Structure:
uniq [options] [filename]
o E.g.Thefilewiththefollowingcontents: This is a line
This is a line
This is NOT the same line This is a line
This is a line
Using “uniq filename” produces:
This is a line
This is NOT the same line This is a line
diff Command
The diff command is used to display differences between two files.
There are greater-than signs ‘>’ and less than signs ‘<' before text, these signs are used to
indicate:
o '>‘ – Text in the second file that is not in the first file o ‘<' - Text in the first file that is not in the second file
grep Command
The grep command is used to display matching patterns within a file. Command structure:
grep [options] [pattern] [filename]
o Example: “grep ‘chevy’ cars” will display all lines containing the pattern ‘chevy’ in the file called cars
1.2 – Section 6: Review Exercise
A data file has been created named 'poem'. It contains these lines: This is my file
It is not your file So hands-off! So hands-off!
Question 1: Enter a command to display the contents of 'poem' all-at-once on the screen. cat poem
Question 2: Enter a command to display the first 3 lines of ‘poem’ on the screen. head -3 poem
Question 3: Enter a command to display the last 2 lines of 'poem' on the screen. tail -2 poem
Question 4: Enter a command that will display the lines from 'poem' in sorted order. sort poem
Question 5: Enter a command that will display the unique adjacent lines from 'poem'. You are not required to sort the file.
uniq poem
Question 6: Issue a Linux command to create the following empty files in your current directory:
a_text a_memos a_reports
touch a_text a_memos a_reports
Question 7: Issue a Linux command to copy the file 'a_text' to 'a_text.bak' in your current directory.
cp a_text a_text.bak
Question 8: Issue a Linux command to rename the file 'a_reports' to 'a_announcements'.
mv a_reports a_announcements
Question 9: Issue a Linux command to compare differences between the files 'poem' and 'a_text'.
diff poem a_text
1.3 Directory Management
1.3 – Section 1: Unix Filesystem Hierarchy
Unix Filesystem Hierarchy
The Linux OS has a 'hierarchical' file system, which means that directories can contain other directories or files. This is also known as a 'tree diagram', which is usually shown with the root directory at the top:
/ (root) |
-------------------------------- ||||
bin dev usr
etc
| --------------- |||
ABC
o In the Linux and Unix OS, the storage device such as the hard disk contains
directories. Directories are files that are used to store other files. This allows the
storage device to be organized and prevent confusion.
o The root directory (denoted by a slash '/') is the beginning (or master) directory.
o All other directories are related to the root directory -- they are contained, directly
or indirectly, within the root directory.
Directory Terminology
Directory: A 'directory file' used to store other files.
Sub-directory: A directory file DIRECTLY contained within a 'parent' directory. Also called a
'child directory'.
o Example: In the diagram, 'usr' is a child of '/', the root.
Parent Directory: A directory that contains one or more children. o Example: In the diagram, '/' is the parent of 'bin', 'dev'
Important things to note about Unix file hierarchies:
There are no drive letters. All the disk drives are 'mounted' into a single hierarchy.
In Unix, file names are used for many types of resources that you would not usually think
of as files, such as directories, devices, communication between programs, status
information, and network connections (remember that 'everything is a file').
Files do not have to have an 'extension', though you may use one to indicate the type of file
content (for example, '.html' to indicate HTML web files).
Naming Files (and Directories)
The maximum length of a file name, whether ordinary files or directories, varies from system to system. The sizes can range from 14 to 255 characters.
Most characters can be used in filenames, but you should stick to:
o Uppercaseorlowercaseletters o Numbers
o Underscore'_'
o Period'.'
o Comma',' Note:
o A period BEFORE a filename 'hides' the file. o Allfilenamesarecasesensitive.
1.3 – Section 2: Pathnames (Absolute, Relative, Relative-to-Home)
Pathnames
A pathname is used to specify the location of a file or directory. Pathnames are used when issuing Linux commands when working with directories and files.
There are three categories of pathnames:
o AbsolutePathname-locationreferencestartingfromroot'/'
o RelativePathname-locationreferencestartingfromthe'currentdirectory'
o Relative-to-HomePathname-locationreferencestartingfromyour'homedirectory'
Absolute Pathnames
An absolute pathname specifies how to find a file, starting at the root directory and working your way down to a specific file. Absolute pathnames always start with a slash '/' character.
An absolute pathname consists of a list of directories separated by slashes '/' and ending with the particular file or directory to which you are referring.
o Forexample,theabsolutepathname: /etc/system/config/ejb.conf
specifies the file 'ejb.conf' stored within the 'config' directory, which is within the 'system' directory, which is within the 'etc' directory, which is within the root directory.
Relative Pathnames
Unix systems employ the concept of a current directory, which you can set to any directory in the hierarchy using 'cd'.
A relative pathname specifies how to find a particular file or directory starting from the current directory.
o Example: If the current directory is set to '/etc/system', then these pathnames are equivalent:
Relative-to-Home Pathnames
Relative Pathname
foo.txt conf/current old/red/blue
Absolute Pathname
/etc/system/foo.txt /etc/system/conf/current /etc/system/old/red/blue
When using relative pathnames, these symbols are available: o . THIS (current) directory
o .. Parent directory
o Example: If the current directory is set to '/etc/system', then these pathnames
are equivalent:
Relative Pathname
Absolute Pathname
.
foo.txt
./foo.txt
../foo.txt
../../home/joe/text /home/joe/text
/etc/system /etc/system/foo.txt /etc/system/foo.txt /etc/foo.txt
Each user of a Unix system is assigned a home directory by the system administrator. Home directories may be located anywhere in the hierarchy.
o The home directory is provided for you to store your personal files and directories.
o Note that your home directory is NOT necessarily the same as your current directory. Your current directory changes as you move around the file hierarchy, but your home directory stays the same.
Relative-to-Home pathnames start with a tilde '~'.
o If you specify a tilde followed by a slash, the pathname is relative to your home directory.
o You can also specify a pathname relative to another user's home directory by starting your pathname with '~userid/'.
o Example: Your home directory is /home/yczhao1, so these pathnames are equivalent:
Relative-to-Home
~/lastyear.txt ~/unix/notes
Absolute Pathname
/home/yczhao1/lastyear.txt /home/yczhao1/unix/notes
o Example: If your friend's userid was 'jstudent' and her home directory was '/home/jstudent', these pathnames would be equivalent:
Relative-to-Home
~jstudent/test ~jstudent/2018/diagram
1.3 – Section 3: Ambiguous Pathnames
Ambiguous Pathnames
Absolute Pathname
/home/jstudent/test /home/jstudent/2018/diagram
Unix provides the ambiguous pathnames' feature to permit you to specify a number of files at once.
There are three 'wildcard' symbols used in ambiguous pathnames:
o * matches zero or more of any character
o ? matches exactly one of any character
o [class] matches exactly one character from the class
The Asterisk *
The asterisk matches zero or more characters, like this:
Pattern Matches Doesn't Match
* Matches all non-hidden filenames
a*b ab
a2314234b able
Pattern
?
Matches Doesn't Match
Matches all one-character filenames except . (dot) a aa
b
A12 all_the_tab resub
*txt file.txt
file_txt bastxtyot
texttxt .file.txt txt
The Question-Mark ?
The question-mark matches exactly one of any character, like this:
txtfile
a?b a2b ab
axb acesdb
a_b
?txt atxt notes.txt 3txt txt
qtxt .txt
The Character-Class
A character-class is enclosed in square brackets [ ] and may include a list of characters or a range of characters separated by a dash. The character class matches any one character in the list or ranges.
Ambiguous Pathname
[abc]
notes[127-9]
Matches
a b c
notes1 notes2 notes7 notes8 notes9
Does not match
A aligator
aaaaa
notes3 notes notes.txt notes127
You can invert the meaning of a character class by placing the symbol '!' at the start of the character-class. That will make it match any of the characters which are not in the character-class.
Globbing
Ambiguous Pathname
[!a-c]*
Matches Does not match
drift aligator 4234run bowl
cat
a[!b]c a2c abc alc allc
The process of converting an amiguous filename into a list of matching file names is called 'globbing' and is performed by the shell. On other operating systems, the equivalent operation is performed by the command and not by the shell.
Be aware that because globbing is performed by the shell, globbing is applied to all arguments, whether they are supposed to represent files or some other information.
o For example, the 'echo' command displays information on the screen. If you type 'echo *', the asterisk will be replaced by a list of all of the files in the current directory. To avoid this, use quotes around the argument.
Demonstration
These are the files in the current directory:
a1 a11 a123 a1234 a23 a22 a3 1 11 123
o 'echo a*' will display all filenames that begin with the letter 'a' a1 a11 a123 a1234 a22 a23 a3
o ‘echo a*3’ will display all files names that begin with ‘a’ and end with ‘3’ a123 a23 a3
o ‘echo a?3’ will display all files names that begin with ‘a’, has exactly one of any character in the middle, and end with ‘3’
a23
o ‘echo a??3’ will display all files names that begin with ‘a’, has any two characters in the middle, and end with ‘3’
a123
o Character classes behave like the '?' wildcard, except you can specify what characters '?' can or cannot represent.
‘echo [a1]*’ will display filenames that begin with either the lowercase letter 'a' (case matters) or begin with the number '1'.
1 11 123 a1 a11 a123 a1234 a22 a23 a3
‘echo *[1-3]’ will display filenames that end with the number '1' or '2' or '3'
111123a1a11a123a22a23a3
You can also combine ranges and/or lists.
Example: ‘echo [a-zA-Z]*’ will display all filenames that begin
with a letter (whether upper or lowercase). a1 a11 a123 a1234 a22 a23 a3
You can invert a character class using '!'.
Example: ‘echo [!a]*’ will display all filenames that do not start
with 'a'. 111123
1.3 – Section 4: Directory Management Commands
Create Directories
You create directories by issuing the linux command: mkdir directory-name
o The command above is using a relative pathname; therefore, the directory will be created as a "child" directory of the current directory. Example of using an absolute pathname to create a directory:
mkdir /students/username/directory-name
o To save time, you can create more than one child directory under the current
directory by placing multiple directory names within the same Linux command: mkdir d1 d2 d3
o YoucanissueaLinuxcommandtocreatedirectorieseveniftheir'parent' directories do NOT exist. To do this, add the '-p' (parent) argument to the mkdir command.
Example: ‘mkdir -p testing/1/2/3’ will create a ‘testing’ directory which contains a ‘1’ directory which contains the ‘2’ directory which contains the ‘3’ directory
Change Directory
You can change your current directory using the 'cd' command.
o If you do not specify any arguments for the 'cd' command, you will set your
current directory back to your home directory. Check Current Directory
We can check our current directory with the 'pwd' command (print working directory).
Remove Directories
Directories may be removed with the command 'rmdir', but:
o You cannot be in the directory that you're trying to remove (or one of its child
directories).
o The directory must be empty.
There is a more powerful command available to remove entire directory paths. The command 'rm -r' will remove non-empty directories which contain files or child directories, grandchild directories, etc.
o Use the option -i to prompt whether each directory or file is to be removed.
1.3 – Section 5: Review Exercise
Question 1: Enter the Linux command to create both a parent directory called 'systems' and it's child directory called 'part3' at the same time. Assume that directory 'systems' will branch-off your home directory and that you are in your home directory to start. Use a relative pathname.
mkdir -p systems/part3
Question 2: Enter a Linux command to create the empty file called 'test1' in the directory 'systems' (you
are still in your home directory). touch systems/test1
Question 3: Assume that your current directory contains the files 'labtest', 'labtest1', 'labtest2', 'labtest2a', 'labtest3', and 'labtest4'. Issue a command to delete only the files 'labtest1' and 'labtest2' using one ambiguous pathname.
rm labtest[1-2]
Question 4: Assume that you are NOT currently in your home directory. Enter a command to copy all
files in your home directory beginning with the letter 'a' to the current directory. cp ~/a* .
Question 5: Enter a command to delete all files that have filenames starting with 'labtest', except 'labtest' itself, from the current directory (Delete all files starting with 'labtest' followed by one or more characters).
rm labtest?*
Question 6: Enter a Linux command to remove a directory called 'assignments' and all of it's contents, and allow the system to prompt the user to remove each file or directory. Assume you are in the home directory, which happens to be the parent directory of the 'assignments' directory.
rm -ir assignments
Question 7: Here are two inverted-tree diagrams. Issue a command to change the left tree structure to be the same as the tree structure on the right. Assume that you are in your home directory and use relative pathnames. [home] is your home directory:
[home] [home] ||
+----------+----------+ +-------------+
||||| systems ideas courses ideas courses ||
notes systems |
mv systems courses
Notes
Question 8: Issue a command to delete all ordinary files in your current directory with 2-character names.
rm??
Question 9: Issue a command to delete the directory 'courses' (in your home directory) and all of its children. Use an absolute pathname (remember that your home directory is /home/yczhao1).
rm -r /home/yczhao1/courses
Question 10: Enter a command to make the root directory your current directory.
cd/
1.4 Practice Commands to Create a Directory Structure
Question 1: You are currently in your home directory. Within your home directory, create a directory called a1 using the shortest possible path name.
mkdir a1
Question 2: Copy the file called cars that resides in the directory ~uli101/2018c to your a1 directory.
cp ~uli101/2018c/cars a1
Question 3: Make a1 your present working directory.
cda1
Question 4: Without changing working directories, list only the names of files in your home directory.
Use the shortest possible pathname, and be sure that hidden files are also listed. ls-a~
Question 5: Change to your home directory using the fewest possible keystrokes. cd
Question 6: While in your home directory, create another directory called a1a. mkdir a1a
Question 7: Move your a1 directory (including its contents) into a1a. mv a1 a1a
Current Directory Tree: a1a
`---- a1
`---- cars
Question 8: Make a1 your present working directory:
cd a1a/a1
Question 9: Within your current directory (a1), view the complete contents of the file called cars.
cat cars
Question 10: Copy the file /etc/passwd to your current directory (a1).
cp /etc/passwd . Current Directory Tree:
a1a `---- a1
|---- cars `---- passwd
Question 11: Create a subdirectory under your current directory (a1) called a1_sub. mkdir a1_sub
Current Directory Tree:
a1a `---- a1
|---- a1_sub |---- cars `---- passwd
Question 12: Copy the file /etc/group to your a1_sub. Your current directory is a1. cp /etc/group a1_sub
Current Directory Tree:
a1a `---- a1
|---- a1_sub `---- group
|---- cars `---- passwd
Question 13: Copy your local copy of passwd (from a1, your current directory) to a1_sub, but make sure this newest copy is named passwd.copy.
cp passwd a1_sub/passwd.copy
Current Directory Tree:
a1a `---- a1
|---- a1_sub |---- group
`---- passwd.copy |---- cars
`---- passwd
Question 14: Your current directory is a1. Copy /etc/group to a1_sub, naming the copy group.copy.
cp /etc/group a1_sub/group.copy Current Directory Tree:
a1a `---- a1
|---- a1_sub |---- group
|---- group.copy `---- passwd.copy
|---- cars `---- passwd
Question 15: Your current directory is a1. Create a subdirectory under a1_sub called a1_sub.copy. mkdir a1_sub/a1_sub.copy
Current Directory Tree:
a1a `---- a1
|---- a1_sub
|---- a1_sub.copy
|---- group
|---- group.copy
`---- passwd.copy |---- cars
`---- passwd
Question 16: Your current directory is a1. List detailed info about the non-hidden files in the a1_sub directory.
ls -l a1_sub
Question 17: Your current directory is a1. Copy ~/.bash_history to a1_sub, calling it myHistory.
cp ~/.bash_history a1_sub/myHistory Current Directory Tree:
a1a `---- a1
|---- a1_sub
|---- a1_sub.copy
|---- group
|---- group.copy
|---- myHistory
`---- passwd.copy |---- cars
`---- passwd
Question 18: Copy myHistory to your home directory.
cp a1_sub/myHistory ~
Question 19: Your current directory is a1. Copy ~/.bash_history to a1_sub.copy.
cp ~/.bash_history a1_sub/a1_sub.copy Current Directory Tree:
a1a `---- a1
|---- a1_sub
|---- a1_sub.copy
`---- .bash_history
|---- group
|---- group.copy
|---- myHistory
`---- passwd.copy |---- cars
`---- passwd
Question 20: Your current directory is a1. List detailed info about all the files in the a1_sub.copy directory.
ls -la a1_sub/a1_sub.copy
Question 21: Your current directory is a1. Using rmdir, try to delete a1_sub.copy, and note what
happens.
rmdir a1_sub/a1_sub.copy (Doesn’t work since there is a file in a1_sub.copy)
Question 22: Your current directory is a1. Delete .bash_history within a1_sub.copy. rm a1_sub/a1_sub.copy/.bash_history
Question 23: Your current directory is a1. Using rmdir, try to delete a1_sub.copy again. It should work this time.
rmdir a1_sub/a1_sub.copy Current Directory Tree:
a1a `---- a1
|---- a1_sub
|---- group
|---- group.copy
|---- myHistory
`---- passwd.copy |---- cars
`---- passwd
Question 24: Change to your home directory again, using the fewest possible keystrokes.
cd
1.5 Create a Directory Structure
Question 1: Create the following directory structure within the home directory. Directories are in blue. Assume you start at the home directory.
sample_dir1 `---- sample_dir
|---- admin |---- cambridge
|---- cafeteria |---- library |---- security
|---- annex |---- building |---- parking
|---- faculty |---- history.exe |---- markham
|---- annex |---- building1 |---- parking
|---- oxford
|---- outline.doc
|---- programming |---- report.pdf
|---- security |---- stenton
|---- gen_ed |---- lib_arts
|---- english.txt
|---- match.doc Steps:
1. mkdir -p sample_dir1/sample_dir
2. cd sample_dir1/sample_dir
3. mkdir admin cambridge faculty markham oxford stenton
4. touch history.exe
5. cd cambridge
6. mkdir cafeteria library security
7. cd security
8. touch annex building parking
9. cd ../../markham
10. touch annex building1 parking
11. cd ../oxford
12. touch outline.doc
13. mkdir programming security
14. touch programming/report.pdf
15. cd ../stenton
16. mkdir gen_ed lib_arts
17. cd lib_arts
18. touch english.txt match.doc
1.6 Practice Specifying Pathnames
Question 1: Change to your home directory. cd
Question 2: sample_dir1 is a subdirectory of your current (home) directory. Move to sample_dir using a relative pathname.
cd sample_dir1/sample_dir
Question 3: Your current directory is sample_dir. Move to security (within cambridge) using a relative
pathname.
cd cambridge/security
Question 4: Your current directory is security. Move to cambridge using a relative pathname. cd..
Question 5: Your current directory is cambridge. Move to your home directory using a relative pathname (don't use cd by itself for this move).
cd ../../..
Question 6: Your current directory is your home directory. Move to stenton using a relative pathname.
cd sample_dir1/sample_dir/stenton
Question 7: Your current directory is stenton. Make a copy of the file named parking (from the security
directory), name it parking2, and place it in your current directory, using relative pathnames. cp ../cambridge/security/parking parking2
Question 8: Your current directory is stenton. Remove the security directory (within cambridge) including its contents (no prompting), using a relative pathname.
rm -r ../cambridge/security
Question 9: Your current directory is stenton. Make a copy of the file annex (in markham), called annex2,
and place it in markham.
cp ../markham/annex ../markham/annex2
Question 10: Your current directory is stenton. Move to oxford using a relative-to-home pathname. cd ~/sample_dir1/sample_dir/oxford
Question 11: Your current directory is oxford. Make a copy of the file named outline.doc and place it into the sample_dir directory, using relative pathnames.
cp outline.doc ..
Question 12: Your current directory is oxford. Display an 'ls -l' listing of the stenton directory, using an absolute pathname. Note that your home directory is /home/yczhao1.
ls -l /home/yczhao1/sample_dir1/sample_dir/stenton
Question 13: Your current directory is oxford. Create a new empty regular file named dir_practice inside
the library directory, using a relative pathname. touch ../cambridge/library/dir_practice