![]() |
| |||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Issue the following commands: % cd ~ Recap from part one:
You will notice the first two files listed are '.' and '..'. There may be other files that start with a dot too, like '.DS_Store', '.Trash' and '.tcsh_history'. Option '-a' tells 'ls' to list all files, including hidden files. A hidden file is no different to a normal file, other than its name beginning with dot. A hidden file is often used to hold configuration information that does not need to be viewed or changed regularly. Hiding avoids visual clutter in the directory listing. '.DS_Store' is used by the Finder to hold view settings. '.Trash' is used by the Finder to hold the items you have put in the trash before it is emptied. If you do not see this directory (pre Os X 10.2), that's because the trash is empty. Trash something now (using the Finder) - we'll need the directory later. '.tcsh_history' maintains a history of the Unix commands you have issued. You can create your own hidden files: % touch .hidden Touch is an easy way to create a new (empty) file. 'ls' will list just 'not-hidden' while 'ls -a' will list both. What about the files dot and dot-dot? Well, these two files appear in every directory; they link to the current directory (dot) and to its parent directory (dot-dot). A link is the Unix equivalent of a Finder alias. When you type '.', the file system replaces it with the path name of your current directory. When you type '..' the file system replaces it with the pathname of the directory just above your current directory. Try the following: % cd ~ 'ls .' lists the contents of dot, which is the current directory, and is equivalent to a simple 'ls'. The current directory should be your home directory. 'ls ..' lists the contents on the parent directory of (the directory one above) your current directory. From your home directory, you should see a listing of '/Users' Dot and dot-dot can be used anywhere an absolute pathname can. For example: % ls ../.. will list the contents of your system disc (the root directory or '/') given that your current directory is '/Users/you' Going up: % cd .. is a very useful and commonly-used command to move one level up in the directory hierarchy. Shifting Files Around Let's create a biggish text file by telling the 'man' command to write to a file rather than the Terminal. % cd ~ Don't worry what this command is doing, we just need a large text file to play with, and this is an easy way to produce one. % ls will show the new file. Make a copy of 'file' and call it 'file2'. The Unix command 'cp' achieves this. 'file2' is created and 'file' remains unchanged. % cp file file2 It's that easy! Unix commands do not understand Mac resource forks. If you 'cp' a file that does have a resource fork, the copy will have the resource fork stripped and contain only the data fork. The original will not be changed. To move a file use 'mv'. % mv file2 ../Shared/file2 will move 'file2' to directory '/Users/Shared'. The two alternatives are equivalent - the first uses the dot-dot parent directory trick. Understand how these are equivalent before proceeding. and: % ls will show 'file' in the current working directory and 'file2' in '/Users/Shared'. Now move 'file2' back and forth - to the current working directory and back to 'Shared' - with: % mv ../Shared/file2 . In both commands no name was given for the target file: 'mv' assumed you wanted to keep the same name when moving the file. The first command makes use of '.' to represent the current directory and moves 'file2' to the current directory. The second command moves 'file2' from the current directory back to /Users/Shared. Renaming a file is just a form of moving it. It is possible to change the name of a file without moving it into another directory, or to move it and change the name at the same time. For example: % mv ../Shared/file2 ./file-two The first 'mv' moves 'file2' from /Users/Shared into the current directory, renaming to 'file-two'. The second 'mv' renames 'file' to 'file-one' without moving it from the current directory. We should now have 'file-one' and 'file-two' in the current directory, and the Shared directory restored to what it was. 'mv' can also be used to move or rename a directory, as we shall see in Part Three. To delete a file use 'rm'. 'rm' is short for remove. % rm .hidden not-hidden All the files we created should be gone leaving your home directory neat and tidy again. |
Tell Me More...
|
|
'.tcsh_history' Issue the command: % history to list the commands you previously issued. Useful as a reminder of what you did, and how you did it. Touch Me 'touch' is an easy way to create a new file. Its real purpose is to alter modification and access times, default behaviour being to change these to the current date and time. But one can set any date and time by passing the appropriate parameters. Hit the Roof The top-level directory '/' also has a dot-dot file, but it links back to '/' itself because there is no parent directory. The Hidden dot Hidden You may remember from Part One that the Finders' view of the file system is simplified. Compare 'ls /' with the files shown by the Finder for the System disc. The Finder does not display hidden dot files. It also does not display many other files visible in Unix. The hidden file '/.hidden' defines the Unix-visible files that the Finder should hide. Try: % cat /.hidden 'cat' displays a file on the Terminal. Hard Links Files dot and dot-dot are actually Unix hard links to the current and parent-of-current directories. A hard link is where two (or more) directory entries point to the same file. Redirection When a command writes its output to the Terminal, the output can be re-directed to a file by appending '> filename' to the end of the command. For example: % ls -al > listing will create a file called 'listing' containing the output from 'ls' that you would otherwise have seen displayed on the Terminal. List the file with: % cat listing Preserve When copying a file, if you wish to preserve all the file information, such as timestamp, flags, owner, and group, use the '-p' option: % cp -p source target The More the Merrier Move, copy, or remove multiple files at once with: % cp f1 f2 f3 dir Copy and move require a target directory, you can't specify a target file. Auto-completion Remember to use the tab key to auto-complete command, file, and directory names. For example, from your home directory: % ls ../Sh then-hit-tab will auto-complete to: % ls ../Shared/ This assumes that you do not have another user who's name starts with 'Sh'. Tell me More, Man Don't forget to learn more about a command by reading the manual. % man cp |
Command Synopsises:
('source_file ...' means one or more source files.)
cp source_file target_file - copy source_file and call it target_file
cp source_file ... target_directory - copy each source_file and put in target_directory
mv source target - move source (file or directory) to target (file or directory)
mv source ... target_directory - move each source (file or directory) to target_directory
rm file ... - remove (delete) each file
Next Page
Now we know how to copy, move, and delete files. Next we will learn how to view files - more or less, and how we can customise our command line environment
|
|
Part 2 - Playing With Files (page 1 of 2) |
|
| Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved. | ||
| Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. | ||
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |