Using Commands


Using the cd, pwd, ls, mkdir, and cp Commands

Now that we have covered some of these commands in an "isolated" fashion, let's put some of the commands together.

Let's start by viewing the hierarchy of a directory under denise 's home directory, called krsort.dir.old, as shown in Figure 21-7:

 $  cd /home/denise/krsort.dir.old  $  pwd  /home/denise/krsort.dir.old $  ls -l  total 168 -rwxr-xr-x   1 denise   users    34592 Oct 27 18:20 krsort -rwxr-xr-x   1 denise   users     3234 Oct 27 17:30 krsort.c -rwxr-xr-x   1 denise   users    32756 Oct 27 17:30 krsort.dos -rw-r--r--   1 denise   users     9922 Oct 27 17:30 krsort.q -rwxr-xr-x  1 denise   users     3085 Oct 27 17:30 krsortorig.c $ 
Figure 21-7. /home/denise/krsort.dir.old

graphics/21fig07.gif

We can then make a new directory called krsort.dir.new and copy a file to it as shown in Figure 21-8:

 $  mkdir ../krsort.dir.new  $  cp krsort ../krsort.dir.new  $  ls -l ../krsort.dir.new  total 68 -rwxr-xr-x   1 denise   users      34592 Oct 27 18:27 krsort $ 
Figure 21-8. /home/denise/krsort.dir.new

graphics/21fig08.gif

Now let's try the -i option to cp . If we attempt to copy a file to an existing file name, we'll be asked if we wish to overwrite the destination file. We are alerted to the fact that the destination file already exists and we can then select a new name for the file we wish to copy, as shown in Figure 21-9:

 $  pwd  /users/denise/krsort.dir.old $  cp -i krsort ../krsort.dir.new  overwrite ../krsort.dir.new/krsort? (y/n)  n  $  cp krsort ../krsort.dir.new/krsort.new.name  $  ls -l ../krsort.dir.new  total 136 -rwxr-xr-x   1 denise  users    34592 Oct 27 18:27 krsort -rwxr-xr-x     1 denise      users       34592  Oct 27 18:29 krsort.new.name $ 
Figure 21-9. /home/denise/krsort.dir.new/krsort.new.name Added

graphics/21fig09.gif

We can also use a wild card with cp to copy all files in krsort.dir.old to krsort.dir.new as shown in Figure 21-10:

 $  cp * ../krsort.dir.new  $  ls -l ../krsort.dir.new  total 236 -rwxr-xr-x   1 denise   users    34592 Oct 27 18:30 krsort -rwxr-xr-x   1 denise   users     3234 Oct 27 18:30 krsort.c -rwxr-xr-x   1 denise   users    32756 Oct 27 18:30 krsort.dos -rwxr-xr-x   1 denise   users    34592 Oct 27 18:29 krsort.new.name -rw-r--r--   1 denise   users     9922 Oct 27 18:30 krsort.q -rwxr-xr-x   1 denise   users     3085 Oct 27 18:30 krsortorig.c $ 
Figure 21-10. All Files Copied to /home/denise/krsort.dir.new

graphics/21fig10.gif

Using the mv Command

Let's start over at the point where the krsort.dir.new directory is empty, as shown in Figure 21-11:

Figure 21-11. Empty /home/denise/krsort.dir.new Directory

graphics/21fig11.gif

We can now move the file krsort to the krsort.dir.new directory as shown in Figure 21-12:

 $  mv krsort ../krsort.dir.new  $  ls -l ../krsort.dir.new  total 68 -rwxr-xr-x   1 denise   users      34592 Oct 27 18:20 krsort $ 
Figure 21-12. krsort Moved to /krsort.dir.new

graphics/21fig12.gif

If we now attempt to move krsort to the krsort.dir.new directory with the -i option and write over the file krsort , we get the following:

 $  mv -i krsort ../krsort.dir.new/krsort  remove ../krsort.dir.new/krsort? (y/n)  n  $ 

Because we used the -i option to mv , we are asked whether we wish to allow a file to be overwritten with the move. Because we responded n to the question, the file is not overwritten.

We can also use a wild card with the mv command to copy all files from the krsort.dir.old directory to the krsort.dir.new directory. Without the -i option, any files in the krsort.dir.new directory are overwritten by files that have the same name, as shown in Figure 21-13:

 $  mv * ../krsort.dir.new  $  ls -l ../krsort.dir.new  total 168 -rwxr-xr-x   1 denise   users   34592 Oct 27 18:44 krsort -rwxr-xr-x   1 denise   users    3234 Oct 27 18:46 krsort.c -rwxr-xr-x   1 denise   users   32756 Oct 27 18:46 krsort.dos -rw-r--r--   1 denise   users    9922 Oct 27 18:46 krsort.q -rwxr-xr-x   1 denise   users    3085 Oct 27 18:46 krsortorig.c $ 
Figure 21-13. All Files Moved to /home/denise/krsort.dir.new

graphics/21fig13.gif

Down and Dirty with the rm and rmdir Commands

The most feared command in the UNIX world, with good reason I might add, is the rm command. rm removes whatever you want whenever you want, with no questions asked unless you use the -i option.

Want to blow away your system instantly? rm would be more than happy to help you. As an average user , and not the system administrator, you probably do not have the permissions to do so. It is, however, unnerving to know that this is a possibility. In addition, it is likely that you have permissions remove all of your own files and directories. All of this can be easily avoided by simply using the -i option to rm .

Let's assume that krsort.dir.new and krsort.dir.old are identical directories as shown in Figure 21-14:

Figure 21-14. /home/denise/krsort.dir.old and /home/denise/krsort.dir.new Identical

graphics/21fig14.gif

To interactively remove files from krsort.dir.new , you do the following:

 $  rm -i ../krsort.dir.new/*  ../krsort.dir.new/krsort: ? (y/n)  n  ../krsort.dir.new/krsort.c: ? (y/n)  n  ../krsort.dir.new/krsort.dos: ? (y/n)  n  ../krsort.dir.new/krsort.q: ? (y/n)  n  ../krsort.dir.new/krsortorig.c: ? (y/n)  n  $  ls -l ../krsort.dir.new  total 168 -rwxr-xr-x   1 denise   users    34592 Oct 27 18:44 krsort -rwxr-xr-x   1 denise   users     3234 Oct 27 18:46 krsort.c -rwxr-xr-x   1 denise   users    32756 Oct 27 18:46 krsort.dos -rw-r--r--   1 denise   users     9922 Oct 27 18:46 krsort.q -rwxr-xr-x   1 denise   users    3085 Oct 27 18:46 krsortorig.c $ 

This obviously resulted in nothing being removed from krsort.dir.new because we responded n when asked whether we wanted to delete files.

Let's now go ahead and add a file beginning with a "." (period) to krsort.dir.new . The touch command does just that, it touches a file to create it with no contents, as shown in Figure 21-15 ( touch can also be used to update the time stamp of an existing file):

 $  touch ../krsort.dir.new/.dotfile  $  ls -al ../krsort.dir.new  total 172 drwxr-xr-x   2 denise   users    1024 Oct 27 18:54 . drwxrwxr-x   4 denise   users    1024 Oct 27 18:40 .. -rw-r--r--   1 denise   users       0 Oct 27 18:56 .dotfile -rwxr-xr-x   1 denise   users   34592 Oct 27 18:44 krsort -rwxr-xr-x   1 denise   users    3234 Oct 27 18:46 krsort.c -rwxr-xr-x   1 denise   users   32756 Oct 27 18:46 krsort.dos -rw-r--r--   1 denise   users    9922 Oct 27 18:46 krsort.q -rwxr-xr-x   1 denise   users    3085 Oct 27 18:46 krsortorig.c $ 
Figure 21-15. /home/denise/krsort.dir.new with .dotfile

graphics/21fig15.gif

If we now attempt to remove files using the same rm command earlier issued, we'll see the following, as shown in Figure 21-16:

 $  rm -i ../krsort.dir.new/*  ../krsort.dir.new/krsort: ? (y/n)  y  ../krsort.dir.new/krsort.c: ? (y/n)  y  ../krsort.dir.new/krsort.dos: ? (y/n)  y  ../krsort.dir.new/krsort.q: ? (y/n)  y  ../krsort.dir.new/krsortorig.c: ? (y/n)  y  $  ls -al ../krsort.dir.new  total 4 drwxr-xr-x   2 denise   users       1024 Oct 27 18:57 . drwxrwxr-x   4 denise   users       1024 Oct 27 18:40 .. -rw-r--r--   1 denise   users          0 Oct 27 18:56 .dotfile 
Figure 21-16. Only .dotfile Left in /home/denise/krsort.dir.new

graphics/21fig16.gif

The "*" used as a wild card with the rm command does not remove the file .dotfile . The file . dotfile in this directory prevents the rmdir command from removing krsort.dir.new . This file must first be removed before the rmdir command can successfully delete krsort.dir.new.

 $  rmdir -i ../krsort.dir.new  ../krsort.dir.new: ? (y/n)  y  rmdir: ../krsort.dir.new: Directory not empty $  rm ../krsort.dir.new/.dotfile  $  rmdir -i ../krsort.dir.new  ../krsort.dir.new: ? (y/n)  y  $ 

rmdir has now successfully removed krsort.dir.new because .dotfile is gone, as shown in Figure 21-17:

Figure 21-17. rmdir Removes /home/denise/krsort.dir.new

graphics/21fig17.gif



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net