<<

CS1101: Systems Organisation Basic Manipulation II Lecture Overview CS1101: Lecture 5 Basic File Manipulation II

Dr. Barry O’Sullivan ¡ Basic File Manipulation Commands [email protected]

¡ Two Abbreviations – ? – * – Example using Wildcards

¡ More Basic File Manipulation Commands – file Course Homepage – more http://www.cs.ucc.ie/˜osullb/cs1101 – – lpr

– lpq

¢ Department of Computer Science, University College Cork ¢ Department of Computer Science, University College Cork 1

CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II Basic File Manipulation Commands Making New Directories

Command Action ¡ The mkdir command creates directories; moves you to another

tells you directory you are in ¡ It syntax is: mkdir where mkdir creates a directory is the of the directory you rmdir removes a directory want to ; lists the contents of a directory

file ¡ displays the of a file Suppose your home-directory is /home/fred. more displays the contents of a file You want to create a directory there called cp copies a file/directory cs1101: mv moves a file/directory rm removes a file/directory wisdom.ucc.ie:%> cd lpr prints a file wisdom.ucc.ie:%> mkdir cs1101 lpq checks the printer status wisdom.ucc.ie:%>

Please note that UNIX is case-sensitive!!! ¡ Alternatively, you could simply use the absolute path without changing directories: wisdom.ucc.ie:%> mkdir /home/fred/cs1101 wisdom.ucc.ie:%>

¡ Alternatively, you could simply use the abbreviation

for your home-directory:

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 2 Department of Computer Science, University College Cork 3 CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II wisdom.ucc.ie:%> mkdir ˜/cs1101 wisdom.ucc.ie:%> Removing Directories

¡ rmdir does the opposite of mkdir – it removes directories;

¡ Its syntax is rmdir where is the path to the directory you want to remove;

¡ Example, to remove a directory called cs1101 in your home-directory you could issue the following command:

wisdom.ucc.ie:%> rmdir ˜/cs1101 wisdom.ucc.ie:%>

¡ Directories must be empty before they can be

deleted using this command.

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 4 Department of Computer Science, University College Cork 5

CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II What Files are in This Directory? Seeing Hidden Files

¡ The ls command tells you what files (and

directories) are in a directory. ¡ Files starting with . are hidden; ¡ The syntax of ls is ls where ¡ Hidden files are not normally displayed – directory is the (optional) name of the however, ls -a does (the a stands for “all”); directory you want to look ;

¡ Example: ¡ Example:

wisdom.ucc.ie:%> ls /var wisdom.ucc.ie:%> ls adm dt opt mail cs1101 letters audit log news preserve wisdom.ucc.ie:%> lp nis sadm wisdom.ucc.ie:%> ls -a wisdom.ucc.ie:%> . .. .cshrc mail cs1101 letters

¡ If you don’t specify a directory, ls lists the wisdom.ucc.ie:%> contents of the current directory:

wisdom.ucc.ie:%> ls mail cs1101 letters

wisdom.ucc.ie:%>

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 6 Department of Computer Science, University College Cork 7 CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II

Full Information on Files Two More Abbreviations: Wildcards ¡ ¡ ls -l (lowercase “L”) provides more information There are two wildcard characters: * and ? about your files – contents of the directory,

owns a file, its size, when it was last modified, ¡ These can be used to describe a pattern for

etc file/directory names that you want to access; ¡ Example: ¡ * represents any number of characters in a file/directory name wisdom.ucc.ie:%> ls -l total 3 drwxr-x--- 6 osullb system 512 Oct 5 16:33 bin ¡ ? represents a single character in a -rw-r--r-- 1 osullb system 873 Feb 18 2000 iimb drwxr-x--- 2 osullb system 512 Aug 10 1999 nsmail file/directory name

wisdom.ucc.ie:%>

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 8 Department of Computer Science, University College Cork 9

CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II Examples: Wildcards What of Files Are These?

¡ states is a directory containing files with ¡ names of US states: The file command tells you information about the “type” of a file

¡ Example 1: ¡ Its syntax is file where is wisdom.ucc.ie:%> ls ˜/states/m* the name of the file whose type you want to maine michigan mississippi montana know; maryland minnesota missouri massachusetts wisdom.ucc.ie:%> ¡ Example:

¡ Example 2: wisdom.ucc.ie:%> ls ˜/states/m*a wisdom.ucc.ie%> file mytextfile.txt minnesota montana mytextfile.txt: ascii text wisdom.ucc.ie:%> wisdom.ucc.ie%> wisdom.ucc.ie%> file myprogram

¡ Example 3: myprogram: Sun demand paged SPARC executable wisdom.ucc.ie:%> ls ˜/states/a?????a wisdom.ucc.ie%> arizona alabama wisdom.ucc.ie:%>

¡ Note Alaska wasn’t found!

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 10 Department of Computer Science, University College Cork 11 CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II

Viewing the contents of files Copying Files and Directories ¡ The more command displays the contents of ¡ The cp command is used; files:

¡ The syntax is: ¡ Its syntax is more where is the name of the file whose contents you want cp to see; cp cp ¡ Example:

¡ After using cp both the and wisdom.ucc.ie%> more mytextfile.txt exist; This is the first line of the file

This is the second line of the file ¡ Examples: wisdom.ucc.ie%> cp georgia georgia. cp ˜/myletter ˜/myletter.2

cp ˜/myletter ˜/letters

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 12 Department of Computer Science, University College Cork 13

CS1101: Systems Organisation Basic File Manipulation II CS1101: Systems Organisation Basic File Manipulation II Moving Files and Directories Removing Files

¡ The rm command is used to remove files; ¡ The mv command is used;

¡ The syntax is: ¡ The syntax is: rm

mv ¡ mv is a list of files; mv ¡ Examples

¡ After using mv the does not rm myletter

exist any longer; rm myletter myletter2 myletter3 ¡ Examples: ¡ Once a file is removed, you cannot get it back!!! mv georgia ukraine mv ˜/myletter ˜/mynewletter ¡ Use the following version for prompts: mv ˜/myletter ˜/letters rm -i

¡ Directories can also be removed;

¢ ¢ ¢ ¢ Department of Computer Science, University College Cork 14 Department of Computer Science, University College Cork 15 CS1101: Systems Organisation Basic File Manipulation II Printing Files

¡ Use lpr to out files;

¡ Syntax:

lpr

¡ This command can be used to print postscript files to a postscript-enabled printer;

¡ To print to a named printer:

lpr -P

¡ lpq tells you what jobs are on the printer;

¡ Usage:

lpq

lpq -P ¢ ¢ Department of Computer Science, University College Cork 16