Now that you know how to move around the file system on the command line, it's time to start working with files. This section teaches you how to create an example file, copy a file, remove a file, and view and change basic file permissions. In a multiuser operating system like UNIX, it's crucial to understand ownership and permission constructs.
To begin, create an empty file
that you can use for this tutorial. The
touch command can be used to
create an empty file (it's normally used to update the modified date and accessed date of a
file by touching it).
Go back to the TUTORIAL directory in your user's home, and create a file by typing the following command:
$ cd ~/TUTORIAL $ touch example.txt
cp command copies files.
cp followed by the name of the file you
want to copy, followed by the directory you want to copy the
file to (you have the option of specifying a new filename as well). For
instance, try copying the example.txt file to
$ cp example.txt /tmp/ $ ls /tmp/
You should see example.txt in /tmp/. Now, copy the file in /tmp/ back to your current directory, but give it a new name:
$ cp /tmp/example.txt ./example2.txt $ ls
Notice the use of a dot to specify that you want to put this new file in your
current directory. It isn't necessary to include
./ in this case (because the default path for a
copy is your current working directory), but it helps clearly
illustrate what you intend to do. The subsequent
ls command shows that you now have
two example files in your current working directory.
move command is completed with
mv. Most syntax and command-line options for move and copy are the same.
If you want to move your new file, example2.txt, out of the current
directory and into /tmp/, type the following:
$ mv example2.txt /tmp/.
Note again that a dot is used to explicitly call out what you're doing.
Remove the files created in /tmp/ to tidy up your system. The
rm command deletes files from your file
system. This isn't like moving a file into the
Recycle Bin or Trash; the command deletes the file pointer, so use the
rm command with caution. Type the following:
$ rm /tmp/example.txt $ rm /tmp/example2.txt $ ls /tmp/
Both example files in /tmp/ should be gone.
Depending on which
UNIX-like operating system you're using, you might have other delete
commands available, such as
can. Try typing
man srm and
man can to see if you have these commands.
srm is used as a secure version of
rm, which writes random data over deleted
files to keep them from being restored.
can is the opposite of
srm in some ways;
can retains the file but moves it into a
special trash directory similar to the Windows Recycle Bin.