Department of Applied Mathematics at the University of Colorado at Boulder
University of Colorado at Boulder Search A to Z Campus Map University of Colorado at BoulderCU Search Links
Print this page

Basic File Manipulation

Basic File Manipulation

  create move or
rename
copy delete
ordinary file: - mv cp rm
directory: mkdir mv cp -r rmdir

 

 

On any computer, it is possible to create many files in a short time, and quickly you have hundreds of old files cluttering your home directory, making it harder to find specific files and generally slowing you down. Save lots of time by developing these good habits early:

  • Name files & directories simply and meaningfully
  • Create and use subdirectories to organize your files
  • Delete old unneeded files and directories

Having made that point, here are the Unix commands for creating, moving/renaming, copying, and deleting files. Ordinary files are created by all sorts of applications, by directing the output of commands into a file, or by copying existing files. For creating directories, or for renaming/moving/copying/deleting, there are standard Unix commands as summarized in the table above/right.

All of the following commands (except mkdir) may delete files, and on Unix computers this usually doesn't mean they go to some Recycle Bin where you can retrieve mistakenly-deleted files -- they will be gone! (If a file is over 24 hours old, then it has been saved on a nightly backup tape, and the systems administrator can retrieve it, but it's a lot of work.) So beware.

Remember that ``.'' is the Unix way of referring to your current working directory (the one where you are currently residing, wherever that may happen to be), and ``..'' refers to the parent directory (one directory level above where you currently reside).

 


mv  Moving a file is the same as renaming it; in Unix, both just amount to changing how we reference the file. To move (rename) a file or directory, use mv:

mv   oldname   newname
Examples:
  • mv a.out myprog     -- changes the file's name to `myprog'
  • mv myprog bin/myprog     -- move `myprog' into existing subdirectory `bin' (not changing its name)
  • mv myprog bin/     -- same as above
  • mv myprog bin/prog2     -- move `myprog' into subdirectory `bin', changing name
  • mv file9 ..     -- move `file9' into the parent directory (i.e., one directory up)
  • mv dirA dirB     -- change the name of directory `dirA' to `dirB'
  • mv *.jpg JPGs/     -- move all of the JPEG files in the current directory into subdirectory `JPGs'
WARNING: If file bbb already exists, and you command   mv aaa bbb,   then the old file bbb will be deleted and replaced by the contents of (ex-)file aaa.

cp  Use cp to copy a file or directory. Examples:

  • cp fileA fileB     -- creates `fileB', identical to `fileA'
  • cp -i fileA fileB     -- safer version: asks for confirmation if there is already a file `fileB'
  • cp -r dirA dirB     -- creates directory `dirB', with the same contents as directory `dirA'
WARNING: If file bbb already exists, and you command   cp aaa bbb,   then the old file bbb will be clobbered, replaced by the contents of file aaa.

rm  Use rm to delete a file or files: Examples:

  • rm fileA     -- deletes that file
  • rm *.jpg     -- deletes all the files named with the suffix ``.jpg''
  • rm bin/myprog junkfile ../dat.tmp /tmp/blah     -- deletes several named files
You won't be able to delete any files that don't belong to you. Or rather, you can delete only those files for which you have ``write permission''.

rmdir  To delete a directory, use rmdir. It won't work if there happen to be any files in that directory; first deal with the files in the directory, then you can delete the directory.

  • rmdir dirA
You won't be able to delete any directories that don't belong to you.

mkdir  To create a new directory, use mkdir. You can only create directories inside your home directory, or in directories where you have ``write permission''.

  • mkdir NewDir