Unix - Hour 6

Creating, Moving, Renaming, and Deleting
Files and Directories

1.1 Creating New Directories Using mkdir

6.1.1) Post cursor in the home directory and list files and directories
internet$ cd
internet$ ls
README          cis328          file22          m1feedback      sort.manpage.Z
TEST            cis377          file3           my.new.cmd      test
bugTrack        cis396q         file30          newfile
cis316          file1           file4.doc       prokids
cis325          file2           file40          public-web
cis327          file20          m13com          say.hi
6.1.2) Creates a new directory called newdir
internet$ mkdir newdir
internet$ ls
README          cis328          file22          m1feedback      say.hi
TEST            cis377          file3           my.new.cmd      sort.manpage.Z
bugTrack        cis396q         file30          newdir          test
cis316          file1           file4.doc       newfile
cis325          file2           file40          prokids
cis327          file20          m13com          public-web
6.1.3) Lists the directory just created
internet$ ls -ld newdir
drwxr-xr-x   2 spantaro students      512 May 28 12:47 newdir
internet$ ls -la newdir
total 3
drwxr-xr-x   2 spantaro students      512 May 28 12:47 .
drwx------  15 spantaro students     1536 May 28 12:47 ..
internet$ 
6.1.4) Shows the permissions on the just created directory
internet$ umask
022 
6.1.5) Trying to create a directory with a name of an existing one generates an error
       message as bellow
internet$ mkdir newdir
mkdir: Failed to make directory "newdir"; File exists
internet$
6.1.6) This command create a directory in the specified path
internet$ mkdir /home2/stud-30/spantaro/newdir/anotherdir
internet$ ls /home2/stud-30/spantaro/newdir
anotherdir
internet$
6.2.1) This command copied .login to a file called login.copy. Since the second file
       did not existed a copy of .login was generated.
internet$ cp .login login.copy
internet$ ls -ld .login login.copy
-rw-r--r--   1 spantaro students       90 MMay  9 17:42 .login
-rw-r--r--   1 spantaro students       90 MMay 28 13:04 login.copy
internet$
6.2.2) This command did not go through because a directory cannot be copied
internet$ cp newdir olddir
cp: newdir: is a directory
internet$
6.2.3) This command copied all files that started with "file" to the new directory created
       under the home directory.
internet$ cp file* ~/newdir
internet$ ls ~/newdir
anotherdir  file2       file22      file30      file40
file1       file20      file3       file4.doc
internet$
6.3.1) First list information about file login.copy, then rename it to new.login, then
       when trying to show information on login.copy an error message appears. The new
       name now is new.login
internet$ ls -l login.copy
-rw-r--r--   1 spantaro students       90 MMay 28 13:04 login.copy
internet$ mv login.copy new.login
internet$ ls -l login.copy new.login
ls: login.copy: No such file or directory
-rw-r--r--   1 spantaro students       90 MMay 28 13:04 new.login
internet$
6.3.2) This example showed that all files starting with "file" were moved from a
       subdirectory to a parent one. 
internet$ cd
internet$ rm file* First remove any possible file that starts with "file"
internet$ ls
README          cis325          cis396q         new.login       public-web
TEST            cis327          m13com          newdir          say.hi
bugTrack        cis328          m1feedback      newfile         sort.manpage.Z
cis316          cis377          my.new.cmd      prokids         test
internet$ cd newdir
internet$ ls
anotherdir  file2       file22      file30      file40
file1       file20      file3       file4.doc
internet$ mv file* ..
internet$ ls ..
README          cis328          file22          m1feedback      public-web
TEST            cis377          file3           my.new.cmd      say.hi
bugTrack        cis396q         file30          new.login       sort.manpage.Z
cis316          file1           file4.doc       newdir          test
cis325          file2           file40          newfile
cis327          file20          m13com          prokids
internet$ ls
anotherdir
6.3.3) Directories can be renamed and relocated by using mv. This does not apply for 
       for the .(dot) directory as shown bellow.
internet$ mv . new.dot
mv: . is a directory
internet$
6.3.4) This example showed how dangerous cp and mv can be. It shows one file
       overwriting another. File cshrc.copy overwrites login.copy. Then
      this files is deleted and renamed by login.copy.
internet$ ls -l login.copy cshrc.copy
-rw-r--r--   1 spantaro students        0 MMay 28 13:35 cshrc.copy
-rw-r--r--   1 spantaro students       90 MMay 28 13:35 login.copy
internet$ cp cshrc.copy login.copy
internet$ ls -l login.copy cshrc.copy
-rw-r--r--   1 spantaro students        0 MMay 28 13:35 cshrc.copy
-rw-r--r--   1 spantaro students        0 MMay 28 13:38 login.copy
internet$ mv cshrc.copy login.copy
internet$ ls -l cshrc.copy login.copy
ls: cshrc.copy: No such file or directory
-rw-r--r--   1 spantaro students        0 MMay 28 13:35 login.copy
internet$
6.4.1) File login.copy was renamed with my first name.
internet$ mv login.copy siomara
internet$ ls -l login.copy siomara
ls: login.copy: No such file or directory
-rw-r--r--   1 spantaro students        0 MMay 28 13:35 siomara
internet$
6.4.2) The directory newdir was renamed to new.sample.dir
internet$ ls -ld newdir
drwxr-xr-x   3 spantaro students      512 May 28 13:24 newdir
internet$ mv newdir new.sample.dir

internet$ ls -ld newdir new.sample.dir
ls: newdir: No such file or directory
drwxr-xr-x   3 spantaro students      512 May 28 13:24 new.sample.dir
internet$
          
6.4.3) This showed that a directory can not be renamed with an existing file name.
       The oposite is OK. It moved file siomara to the specified directory.
internet$ mv new.sample.dir siomara
mv: new.sample.dir is a directory
internet$
internet$ mv siomara new.sample.dir
internet$ ls new.sample.dir
anotherdir  siomara
internet$
6.4.4) This showed that directory testdir overwrote directory new.sample.dir.
internet$ mkdir testdir
internet$ ls
README          cis328          file22          m1feedback      public-web
TEST            cis377          file3           my.new.cmd      say.hi
bugTrack        cis396q         file30          new.login       sort.manpage.Z
cis316          file1           file4.doc       new.sample.dir  test
cis325          file2           file40          newfile         testdir
cis327          file20          m13com          prokids
internet$ mv new.sample.dir testdir
internet$ ls
README          cis328          file22          m1feedback      say.hi
TEST            cis377          file3           my.new.cmd      sort.manpage.Z
bugTrack        cis396q         file30          new.login       test
cis316          file1           file4.doc       newfile         testdir
cis325          file2           file40          prokids
cis327          file20          m13com          public-web
internet$
internet$ ls -ld new.sample.dir testdir
ls: new.sample.dir: No such file or directory
drwxr-xr-x   3 spantaro students      512 May 28 13:58 testdir
internet$
6.5.1) Created and removed directory test.
internet$ mkdir test
internet$ ls -l test
total 0
internet$ rmdir test
internet$ ls -l test
ls: test: No such file or directory
internet$
         
6.5.2) This showed that a directory that is not empty cannot be removed with rmdir command.
internet$ mkdir test
internet$ touch test/sample.file
internet$ ls -l test
total 0
-rw-r--r--   1 spantaro students        0 MMay 28 14:04 sample.file
internet$ rmdir test
rmdir: directory "test": Directory not empty
internet$ 
6.5.3) It did not give the proper message as the book suggested but it did not
       allow the remotion of /tmp directory.
internet$ rmdir /tmp
rmdir: directory "/tmp": Directory is a mount point or in use
internet$ ls /tmp
daemonstat.192.251.73.5.80  mpceaGJt
dtdbcache_:0                mpgIaGsr
mp.baaOV                    mpmUaOjE
mp1raOlV                    pgL4aGGO
mp8LaGwJ                    pgTZaWn1
mpC1aaIL                    ps_data
mpCla4KL                    psb_back_socket
mpFSa4za                    psb_front_socket
mpGtaiL0                    sdtvolcheck5629
mpLvaG5z                    speckeysd.lock
mpPVaqZL                    testme
mpP_aqSN                    testme2
mpSuaOKR                    testme3
mpUwaqZr                    testmecl
mpXla4BL                    testy
mp_baiXy
internet$
6.6.1) File1 was listed, then removed and when k\listed again showed message of
       file not found.
internet$ ls -l file1
-rw-r--r--   1 spantaro students       11 MMay 28 13:13 file1
internet$ rm file1
internet$ ls -l file1
ls: file1: No such file or directory
internet$ 
6.6.2) More than one file was removed at the same time
internet$ ls -l file2 file20 file22
-rw-r--r--   1 spantaro students        0 MMay 28 13:13 file2
-rw-r--r--   1 spantaro students       11 MMay 28 13:13 file20
-rw-r--r--   1 spantaro students       80 MMay 28 13:13 file22
internet$ rm file2 file20 file22
internet$ ls -l file2 file20 file22
ls: file2: No such file or directory
ls: file20: No such file or directory
ls: file22: No such file or directory
internet$  
Another way to remove more than one file at the same time.
internet$ rm -i file3*
rm: remove file3 (yes/no)? y
rm: remove file30 (yes/no)? y
internet$ ls -l file3*
ls: file3*: No such file or directory
internet$ ls -l file4*
-rw-r--r--   1 spantaro students        0 MMay 28 13:13 file4.doc
-rw-r--r--   1 spantaro students       10 MMay 28 13:13 file40
6.6.3) The -i flag asked for confirmation when deleting files.
internet$ touch testme
internet$ rm -i testme
rm: remove testme (yes/no)? n
internet$ ls testme
testme
internet$ rm -i testme
rm: remove testme (yes/no)? y
internet$ ls testme
ls: testme: No such file or directory
internet$ 
6.6.4) The first line shows the tree. The -r used with rm deletes recursively the
       whole directory and all its contents. The last line confirms the deletion.
internet$ ls -ld test; ls -lR test
drwxr-xr-x   2 spantaro students      512 May 28 14:04 test
total 0
-rw-r--r--   1 spantaro students        0 MMay 28 14:04 sample.file
internet$ rm -r test
internet$ ls -ld test
ls: test: No such file or directory
internet$
6.7.1) Did not understand this command
internet% grep spantaro /etc/passwd
6.7.2) Created an alias for command rm. Everytime its called it will behave as the alias.
       In this case it asks for deletion confirmation even if theuser uses only rm alone.
       This was for c shell.        
internet% grep spantaro /etc/passwd
internet% echo "alias rm /bin/rm -i" >> ~/.cshrc
internet% source ~/.cshrc
internet% touch testme
internet% rm testme
rm: remove testme (yes/no)? n
internet% 
6.7.3) Created an alias for command rm. Everytime its called it will behave as the alias.
       In this case it asks for deletion confirmation even if theuser uses only rm alone.
       This was for korn shell.
internet% ksh
$ echo 'alias rm=" /bin/rm -i"' >> ~/.profile
$ . ~/.profile
$ touch testme
$ rm testme
rm: remove testme (yes/no)? n
$  
6.7.4) Identified the available shells of the system.
$ ls -l /bin/sh /bin/ksh /bin/csh
-r-xr-xr-x   1 bin      bin        151976 OOct  6  1998 /bin/csh
-r-xr-xr-x   2 bin      bin        192764 OOct  6  1998 /bin/ksh
-r-xr-xr-x   3 bin      root        91668 OOct  6  1998 /bin/sh
$
csh changes to C shell.
internet% chsh
chsh: Command not found
internet% csh
internet%
1