![]() |
| |||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
If you've ever changed directory for a few commands and wished you could simply pop back from whence you came - well you can. Instead of using 'cd' to change directory use 'push', which does exactly as 'cd' and also remembers the way back. For example, we start off in some deeply nested directory and then move to ~ using 'pushd': % cd ~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse/% pwd /Users/melkor/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse ... % pushd ~ ~ ~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse % pwd /Users/Melkor 'pushd' remembers where we came from, and just to prove so displays both the new and old directories. To get back to ~/Sites/..... simply issue a 'popd': % popd~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse % pwd /Users/melkor/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse 'pushd' can be used many times in succession, and each time it stacks another stepping stone to the way home. The command 'dirs' displays the current directory stack. 'popd' moves back down the directory stack one step at a time. |
Tell Me More...
|
|
Stack Tricks To print the directory stack one entry per line type: % dirs -v'pushd' and 'popd' also take option'-v'. If you do not wish 'pushd' and 'popd' to display the directory stack each time, this can be arranged with: % set pushdsilentTo find out more about the directory stack, view the tcsh manual with: % man tcshthen while viewing the man page search for information on a command such as 'pushd' by typing: /pushd |
|
|
Now to get down to the business of playing with directories. First, make sure you are in your home directory: % cd ~Now create a new directory called new-dir: % mkdir new-dir % ls Desktop Documents Movies Pictures Shared new-dir Development Library Music Public Sites osxfaq % ls -al new-dir/ total 0 drwxr-xr-x 2 melkor staff 24 Jul 15 16:09 . drwxr-xr-x 24 melkor staff 772 Jul 15 16:09 .. 'new-dir' will be empty except for the dot and dot-dot entries which are always present and created automatically. That's all there is to creating new directories! Now create a file in new-dir - we'll need it for the next part: % touch new-dir/fileCopy a Directory - cpdir? cp? Let's give it a try: % cpdir new-dir new-dir-2 cpdir: Command not found. Mmmm? % cp new-dir new-dir2 cp: new-dir is a directory (not copied). Oh? The secret to copying directories is to do it recursively. We will consider recursion on page two of this tutorial, and wildcards too. Only then will we be fully equipped to 'Play with Directories'. Directory 'new-dir' is just so last minute. Let's remove it: % rmdir new-dir rmdir: new-dir: Directory not empty Oops! First point to note. A directory must be empty before it can be removed. So let's remove 'file' then try to delete: % rm new-dir/file % rmdir new-dir Having to empty directories before we remove them is going to prove a pain. Again, recursion and wildcards will come to our rescue on page two. Move or Rename a Directory - mv The 'mv' command used to move files also moves directories. A directory can be renamed or moved to within another directory. When a directory is moved, all nested directories are moved too and remain nested. This example shows moving and renaming directories: % cd ~ % mkdir -p one/too/for/three Rename 'too' to 'two' but don't move the directory: % mv one/too one/twoSwitch 'three' and 'for' around, renaming 'for'. First move 'three' from within 'for' to within 'two'. 'for' remains where is was: % mv one/two/for/three one/twoSecond move 'for' from within 'two' to within 'three' and at the same time rename it to 'four' % mv one/two/for one/two/three/fourNow lets see what we have: % ls -R one one: two one/two: three one/two/three: four one/two/three/four: Everything in order. Notice the use of -R, which has caused 'ls' to recursively list the nested directory structure. |
Making a Nest To create a number of nested directories with a single command use option '-p' with 'mkdir': mkdir -p a/b/c/dWithout '-p' it is necessary to make a, and then b, then c, then finally d. Removing the Nest It is possible to remove nested directories, provided that each is empty: rmdir -p a/b/c/dAs With Files Remember that directories behave much as files do. You can use auto completion (hitting tab to complete a path name). Directory names may start with dot and will then be hidden by 'ls'. 'ls -al' Frustrations If you wish to view the details of a directory, and try: % ls -l directoryyou will find yourself listing the contents of the directory, not the details of the directory itself. To prevent this use option '-d': % ls -ld directoryTrailing '/' Frustrations
The 'mv' command seems to be a little fussy when it comes to trailing a '/' at the end of a directory name. You must differentiate between renaming and moving. For example, renaming 'too' to 'two' with: % mv one/too one/two/fails. 'mv' assumes you wish to move the file or directory 'one/too' into the directory 'one/two/' keeping the name 'too', such that 'one/two/too' is formed. The trailing slash says to treat 'one/two/' as a directory. To actually rename it use: % mv one/too one/twoNow 'mv' treats 'one/two' as a file and renames as expected. |
Page two will explain recursion, and how to copy directories and remove non-empty directories
|
|
Part 3 - Playing With Directories (page 1 of 2) |
|