cp - Copy your files!

One of the things every file system should have the ability to do is duplicate files and UNIX is no different. cp also has useful functions like copying directory trees recursively, preservation of file permissions.

The basic syntax to copy a file is:

ux8 [34]:cp my_file.txt my_new_file.txt
To copy a file and preserve the file permissions:
ux8 [55]:cp -p my_program.bin my_new_program.bin
One of the more useful things you can do with cp is to copy a whole directory structure. For example, if you wanted to make a backup copy of your public_html directory before you made chances you could do this:
ux8 [69]:cp -r public_html backup_web_stuff
One thing to note about using -r is that it will copy links as the files they are linked to, so you must make sure you have the disk space and that you really want to do that. One way around that is to use the -i flag. -i stands for interactive and will ask you before coping each file:
ux8 [11]:cp -ir public_html backup_web_stuff

mv - move and rename files

mv (move) is the command you'll want to use if you need to rename a file, or if you just want to move it to another directory. You can also combine those functions when moving a file.
ux8 [19]:mv my_flie.txt my_file.txt
ux8 [20]:mv ~/bin/cool_program ~/greedy/no_share/bin/
ux8 [21]:mv phone_list.txt ~/private/info/little_black_book.txt

cd and pwd - move around and see where you are.

For those of you familiar with DOS, cd is a familiar command. cd Changes the working Directory. However, cd under DOS also adds in the functionality of pwd. Under UNIX all it does is change the directory. pwd will tell you what directory you are in. Using these two in tandem you'll never got lost in the directory tree. For example:
ux8 [9]:cd public_html
ux8 [10]:pwd
/homeb/k/k-garner/public_html

rm - remove directory entries

First and most important: never, no matter what anyone tells you, never type these two lines, unless you know what you are doing:
ux8 [888]:rm -rf *
ux8 [889]:rm -rf .*
And now we'll tell you why. rm is the command that you use to delete files you no longer need. For example, if we're finally done with my_file.txt (which really wasn't that exciting to begin with):
ux8 [251]:ls -l
-rw-------   1 k-garner student      261 Aug 23 13:06 my_file.txt
ux8 [252]:rm my_file.txt
ux8 [253]:ls -l
ux8 [254]:
Two useful, but very dangerous flags are the -r and the -f flags. -r causes rm to delete files recursively, so you can delete files from a whole directory tree. -f stands for force which will remove directories and files weather they are write protected or not. Let's say that you one day suddenly decieded you no longer wanted your web page:
ux8 [665]:ls -l
drwx--l---   2 k-garner student      512 Aug 23 08:08 Mail
drwx--s--x   3 k-garner student     2048 Sep 14 14:27 public_html
ux8 [666]:rm -rf public_html
ux8 [667]:ls -l
drwx--l---   2 k-garner student      512 Aug 23 08:08 Mail
ux8 [669]:

Why not "rm -rf *" or "rm -rf .*"?

* is the wildcard character, so an "rm -rf *" would remove all of the files and directory structures in the current directory.

In every directory there are two entries:

ux8 [93]:ls -ld . ..
drwx--s--x  10 k-garner student     1024 Sep  9 20:40 .
drwxr-sr-x 3286 root     student    59904 Sep 17 10:01 ..
The directory .. is the parent directory of the one you are currently in, and the directory .. With an rm -rf .* not only will the files in the current directory be wiped out (because .* will match .) but it will travel up the directory tree from the current directory trying to erase what it can. If you have a need to delete all the files that start with a period, "rm -rf .??*" is a much safer way to go. "?" is the single character matching wildcard.
previous: ls top next: ln
k-garner@uiuc.edu