LINUX
COMMANDS
Contents | Previous
| Next
File management
$ cp - copy
cp a.txt backup
Copy the file a.txt across to the directory backup.
cp a.txt backup/a_bk.txt
Copy the file a.txt across to the directory backup
and rename it a_bk.txt.
cp -r a b
Copy the directory a into the directory b.
The -r means recursive (i.e. the files and
subdirectories contained in the directory, plus the files
and subdirectories contained in the subdirectories, and
so on).
cp -i *.txt textfiles
Interactively (-i) copy all text (*.txt)
files from the current directory to the textfiles
directory, asking for confirmation before overwriting any
existing files.
$ mv - move
The following examples move files, but could just as
easily be directories (although you can't overwrite a directory).
mv a.txt work/reports
Move the file a.txt across to the directory work/reports.
mv a.txt b.txt
When you move a file to its current location, you rename
it. In this case the file a.txt is renamed b.txt.
mv a.txt work/b.txt
Move the file a.txt into the work directory,
and rename it b.txt.
mv a.txt ..
Move the file a.txt up one directory.
mv -i *.txt textfiles
Interactively (-i) move all text (*.txt)
files from the current directory to the textfiles
directory, asking for confirmation before overwriting any
existing files.
$ rm - remove
rm a.txt
Remove the file a.txt.
rm -r work/reports
To remove a directory (even an empty one) you need to add
-r. Here the directory work/reports is
removed.
rm -rf work/reports
Normally when deleting a directory, you're asked for confirmation
before deleting each write-protected file contained within.
The -f (force) removes this precaution.
rm -i *.txt
Interactively (-i) remove all text (*.txt)
files in the current directory, i.e. confirm each file to
be removed.
$ mkdir - make directory
mkdir work
Create the directory work in the current directory.
mkdir -p work/reports/2001/oct
Create the directory work/reports/2001/oct, creating
parent (-p) directories (work, work/reports,
work/reports/2001) as required without danger of
overwriting existing directories.
$ rmdir - remove directory
rmdir work
Remove the directory work.
Note:
Because rmdir only removes empty directories,
it usually better to use the rm -r dir,
rm -rf dir commands.
$ ln - link
A link is a special file that points to another
file. There are two types of link: hard links and
symbolic links (also known as soft links or
symlinks).
A hard link allows you to give another filename to a file,
to allow more than one user share a file or to gain more
convenient access to a file. Modifying one "file" modifies
the other, since it's the same file; or more accurately,
shares the same inode. Deleting one "file", doesn't delete
the other, since a link still exists to the inode.
Symbolic links are like hard links but allow you to do
more with them. Unlike a hard link, a symbolic link can
link a directory without being root, link to a
different disk partition, or on a network, and are indicated
in light blue in listings making them far simpler to manage.
Deleting the file the symbolic link points to, turns the
symbolic link into an orphan file, indicated with
a red background and blinking white text.
ln ~gary/work/report.txt rep.txt
Create the hard link, rep.txt, in the current directory,
pointing to the file, ~gary/work/report.txt.
Note:
With the rep.txt part absent in the above, the
link would have been called, report.txt.
ln -s ~gary/work/report.txt rep.txt
The same as above (including the note), but instead a symbolic
link (-s) is created.
$ touch
touch report.txt
Create an empty file (in this case report.txt),
or if it already exists, update its access and modification
time.
Contents | Previous
| Next