Notes: Unix Lab 06 - Creating, moving, renaming, and deleting files and directories.

  1. The mkdir command
  2. The cp command
  3. The mv command
  4. The rm command
  5. The rmdir command
  6. The wildcard expression
  7. Homework
  1. The mkdir command

    mkdir is used to create directories. The command takes one or more arguments which are directories which are to be created.
     
    mark@platypus:~> mkdir unix_class
    mark@platypus:~> ls -ld unix_class/
    drwxrwxr-x  2 mark users 48 2005-02-02 14:57 unix_class/
    mark@platypus:~>
    

    More than one directory can be created at a time using both absolute and relative paths:
     
    mark@platypus:~> mkdir unix_class ./lab06 /home/mark/lab07
    mark@platypus:~> ls -ld unix_class ./lab06 /home/mark/lab07
    drwxrwxr-x  2 mark users 48 2005-02-02 15:01 ./lab06
    drwxrwxr-x  2 mark users 48 2005-02-02 15:01 /home/mark/lab07
    drwxrwxr-x  2 mark users 48 2005-02-02 15:01 unix_class
    mark@platypus:~>
    

  2. The cp command

    Copying files is done with the cp command. cp requires two arguments: the file to copy and the name or path to copy the file to.
    cp original_file destination_file
    The source and target files may be referenced with relative or absolute paths. An example of copying a single file in the same directory:
     
    mark@platypus:~> ls -l birddog.jpg 
    -rw-r--r--  1 mark users 36423 2004-11-04 18:26 birddog.jpg
    mark@platypus:~> cp  birddog.jpg new_pic.jpg
    mark@platypus:~> ls -l new_pic.jpg 
    -rw-r--r--  1 mark users 36423 2005-02-02 15:11 new_pic.jpg
    mark@platypus:~>
    

    Copying a file to another directory. The original name of the file is retained.
     
    tuckerm@platypus:~> ls -l ./web/term3.png 
    -rw-r--r--  1 tuckerm tuckerm 984 Dec  5  2003 ./web/term3.png
    tuckerm@platypus:~> cp ./web/term3.png /mnt/homes/CLASSES/MET489/lab06/
    tuckerm@platypus:~> ls -l /mnt/homes/CLASSES/MET489/lab06/term3.png 
    -rw-r--r--  1 tuckerm tuckerm 984 Feb  2 15:14 /mnt/homes/CLASSES/MET489/lab06/term3.png
    tuckerm@platypus:~>
    

    To copy a file from another location to the current directory:
     
    tuckerm@platypus:~> ls -l /mnt/homes/CLASSES/MET489/lab06/term3.png 
    -rw-r--r--  1 tuckerm tuckerm 984 Feb  2 15:14 /mnt/homes/CLASSES/MET489/lab06/term3.png
    tuckerm@platypus:~> cp /mnt/homes/CLASSES/MET489/lab06/term3.png .
    tuckerm@platypus:~> ls -l ./term3.png
    -rw-r--r--  1 tuckerm tuckerm 984 Feb  2 15:18 ./term3.png
    tuckerm@platypus:~>
    

    cp can also copy a directory and all of its contents by using the "-r" option. This will retain the same structure for files and subdirectories within the copied directory:
     
    tuckerm@platypus:~> ls ./web/
    gimp_acquire.png  term2.png        xsane_device_scan.png  xsane_view.png
    gimp_start.png    term3.png        xsane_preview.png
    sane.html         term_fill.png    xsane_save.png
    term1.png         xsane_begin.png  xsane_select.png
    tuckerm@platypus:~> cp -r ./web /mnt/homes/CLASSES/MET489/lab06/new_web
    tuckerm@platypus:~> ls  /mnt/homes/CLASSES/MET489/lab06/new_web/
    gimp_acquire.png  term2.png        xsane_device_scan.png  xsane_view.png
    gimp_start.png    term3.png        xsane_preview.png
    sane.html         term_fill.png    xsane_save.png
    term1.png         xsane_begin.png  xsane_select.png
    tuckerm@platypus:~>
    

    There are several options for the cp command which will modify its behavior such as preserving the date/time of the newly created files. See the man pages for more information.

  3. The mv command

    The mv command is used to move or rename files and directories. The syntax for the command is as follows:
    mv source_file destination_file
    Source file is the current name of the file which is to be moved or renamed and destination_file is the name or path where the source_file is to be moved to.

    An example of renaming a file with mv:
     
    tuckerm@platypus:~> ls -l newfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 newfile
    tuckerm@platypus:~> mv newfile oldfile
    tuckerm@platypus:~> ls -l oldfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 oldfile
    tuckerm@platypus:~>
    

    An example of moving a file to another location with mv:
     
    tuckerm@platypus:~> ls -l oldfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 oldfile
    tuckerm@platypus:~> mv oldfile test/movedfile
    tuckerm@platypus:~> ls -l oldfile
    /bin/ls: oldfile: No such file or directory
    tuckerm@platypus:~> ls -l ./test/movedfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 ./test/movedfile
    tuckerm@platypus:~>
    

    Moving a file to a new directory without renaming:
     
    tuckerm@platypus:~> ls -l ./test/movedfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 ./test/movedfile
    tuckerm@platypus:~> mv ./test/movedfile .
    tuckerm@platypus:~> ls -l movedfile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:20 movedfile
    tuckerm@platypus:~>
    

  4. The rm command

    In unix the command to delete, or remove, a file is rm. rm only needs the name of the file(s) to delete as arguments to the command. Using rm with the -i option will prompt the user to confirm each file delete. By default on most systems rm does not ask for any confirmation. In the meteorology lab the student shell is configured to prompt by default. When prompted, entering "y" will confirm the delete. If any other character is entered rm will not delete the file with this option.

     
    tuckerm@platypus:~> rm movedfile 
    rm: remove regular file `movedfile'? y
    tuckerm@platypus:~>
    

    The "-r" option will tell rm to remove all files and directories specified, including any and all subdirectories found in the directories listed. An example of rm with the recursive option:

     
    tuckerm@platypus:~> rm -r test
    rm: descend into directory `test'? y
    rm: descend into directory `test/test2'? y
    rm: remove regular file `test/test2/start-vnc.sh'? y
    rm: remove directory `test/test2'? y
    rm: remove regular file `test/coltbl.xwp'? y
    rm: remove directory `test'? y
    tuckerm@platypus:~> ls -ld test
    /bin/ls: test: No such file or directory
    tuckerm@platypus:~>
    

    rm with the "-f" flag will force rm to delete the file(s) specified. This will override the "-i" option. It will also force deletion as the owner of a file, even if the file permissions do not allow write access.

    An example of removing a file without read permissions:
     
    tuckerm@platypus:~> ls -l somefile 
    -rw-rw-r--  1 tuckerm tuckerm 775 Feb  2 20:30 somefile
    tuckerm@platypus:~> rm somefile 
    rm: remove write-protected regular file `somefile'? y
    tuckerm@platypus:~>
    

    An example of forcing the removal of a file with the same permissions:
     
    tuckerm@platypus:~> rm -f somefile 
    tuckerm@platypus:~>
    

  5. The rmdir command

    rmdir is used to remove one or more directories which are specified as arguements. rmdir will not be able to delete a directory if it contains any files or subdirectories.

     
    tuckerm@platypus:~> ls -ld somedir/
    drwxrwxr-x  2 tuckerm tuckerm 48 Feb  2 20:43 somedir//
    tuckerm@platypus:~> rmdir somedir/
    tuckerm@platypus:~> ls -ld somedir/
    /bin/ls: somedir/: No such file or directory
    tuckerm@platypus:~>
    

  6. Wildcard expressions

    The unix shell is able to match file and directory names based on a couple of different symbols. A commonly used symbol is the "*" character. By itself this will match any set of characters. Some examples of the wildcard used with the ls command:

    Wildcard matching the beginning of a file name:
     
    tuckerm@platypus:/mnt/data/gempak/hds/eta> ls *eta215.gem
    2005012918_eta215.gem  2005013106_eta215.gem  2005020118_eta215.gem
    2005013000_eta215.gem  2005013112_eta215.gem  2005020200_eta215.gem
    2005013006_eta215.gem  2005013118_eta215.gem  2005020206_eta215.gem
    2005013012_eta215.gem  2005020100_eta215.gem  2005020212_eta215.gem
    2005013018_eta215.gem  2005020106_eta215.gem  2005020218_eta215.gem
    2005013100_eta215.gem  2005020112_eta215.gem
    tuckerm@platypus:/mnt/data/gempak/hds/eta>
    

    Using the wildcard to match the ending of a file name:
     
    tuckerm@platypus:/mnt/data/xcd> ls ZZ04*
    ZZ04187.IDX  ZZ04188.IDX  ZZ04322.IDX  ZZ04323.IDX  ZZ04324.IDX
    tuckerm@platypus:/mnt/data/xcd>
    

    The wildcard can be used in the middle of a string as well as at the beginning or the end:
     
    tuckerm@platypus:/mnt/data/gempak/hds/eta> ls 20050202*215.gem
    2005020200_eta215.gem  2005020212_eta215.gem
    2005020206_eta215.gem  2005020218_eta215.gem
    tuckerm@platypus:/mnt/data/gempak/hds/eta>
    

  7. Please also visit this unix wildcards website to see the other two types of wildcards, question marks and brackets.