ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.8 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/s1-managing-working-with-files.htm |
| Last Crawled | 2026-03-25 04:24:37 (22 days ago) |
| First Indexed | 2013-08-21 05:03:49 (12 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Copying, Moving and Renaming Files and Directories |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | By now, you've learned a little about the structure of the filesystem;
and you've learned how to create files and directories.
But just because you know how to create files and directories doesn't
mean that you're stuck with the changes you've made. What if you want to
rename and/or move files and directories?
Let's start with the copy command.
Copying Files
Like so many Linux features, you have a variety of options from which
to choose when you want to manipulate files and directories. You can
also use wildcards when you're copying, moving, or deleting files and
directories.
Basically, the copy command is not much more complex than typing:
cp
<source>
<destination>
so to copy the file
sneakers.txt
to the directory
tigger
in your login directory, just type:
cp sneakers.txt tigger
Notice that you also used relative pathnames to copy the file. You can
use both relative and absolute pathnames with
cp
.
Our login directory is the parent of the directory
tigger
; meaning that
tigger
is one directory down from ours.
Read the
cp
man page (
man cp
)
for a full list of the options available with
cp
. But among the options you can use with
cp
are:
-i
-- interactive. Prompts you to confirm if
the file is going to overwrite a file in your destination. This is
a handy option because it can help prevent you from making
mistakes.
-r
-- recursive. Rather than just copying all
the files and directories, copies the whole directory tree,
subdirectories and all, to another location.
-f
-- force. Copies without prompting you for
confirmation that the file should be overwritten. Unless you're
sure you want to force the copy, you probably don't want to make
friends with this option right now.
-v
- verbose. Will show the progress of the
files being copied.
Just by using
cp
alone, you won't see much when the
command is executed. Using an option, such as
-i
,
can make the process a little more useful, because if you want to copy
a file to a location that already has a file with the same name,
you'll be asked first if you really want to overwrite -- meaning
replace -- the file that's already there.
Don't be too "forceful"
Remember that among your options is
-f
(force),
which can overwrite files without asking you if you're certain. Make
sure, when you use the force option, that you
really
want to overwrite a file.
Now that we have the file
sneakers.txt
in the
tigger
directory, let's use
cp
-i
to copy the file again to the same location.
[newuser@localhost newuser]$
cp -i sneakers.txt tigger
cp: overwrite 'tigger/sneakers.txt'?
To overwrite the file that's already there, press
Y
and then
Enter
. Don't want to
overwrite the file? Now is the time to press
N
and
Enter
.
Moving Files
To move files, use the
mv
command (
man
mv
), which is similar to the
cp
command,
except that with
mv
the file is physically moved
from one place to another, instead of being duplicated, as with
cp
.
Common options available with
mv
include:
-i
-- interactive. Will prompt you if the file
you've selected will overwrite an existing file in the destination
directory. This is a good option, because like the
-i
option in
cp
, you'll be
given the chance to make sure you want to replace an existing
file.
-f
-- force. Overrides the interactive mode and
moves without prompting. Unless you know what you're doing, this
option doesn't play nice; be very careful about using it until you
become more comfortable with your system.
-v
-- verbose. Shows a list of the files being
moved.
If you want to move a file out of your home directory and into another
directory, you would type:
mv sneakers.txt tigger
or,
mv sneakers.txt /home/newuser /home/newuser/tigger
using absolute pathnames.
Renaming Files
Actually, we've already covered half of renaming, because when you
copy or move files, you can also rename.
To copy the file
sneakers.txt
from our login
directory to our
tigger
subdirectory, just type:
cp sneakers.txt tigger
To copy and rename that file from
sneakers.txt
to
piglet.txt
, type:
cp sneakers.txt tigger/piglet.txt
To
move
and rename the file, just substitute
mv
for
cp
in the above example.
If you
cd
to
tigger
and use
ls
, you'll see the file
piglet.txt
.
If you just want to rename the file and keep its location, just
mv
in your current directory:
mv sneakers.txt piglet.txt
Deleting Files and Directories
We talked about creating files with the
touch
command and by using redirection in
Chapter 13
. And we created the directory
tigger
using
mkdir
.
But we haven't discussed how to delete files and directories.
Deleting files and directories with the
rm
command
(
man rm
) is a straightforward process.
Let's take our new file
piglet.txt
, and delete it
from the
tigger
directory with the
rm
command:
rm piglet.txt
What happens if we didn't really want to get rid of it? Too late!
Again, that's where the
-i
(interactive) option
comes in handy, because it gives a second chance to think about
whether we really want to toss the file.
[newuser@localhost newuser]$
rm -i piglet.txt
rm: remove 'piglet.txt'?
You can also delete files using the wildcard
*
, but
be careful, because you can easily delete files you didn't intend to
throw away.
To remove a file using a wildcard, you would type:
rm pig*
You can also remove more than one file in one command, as in:
rm piglet.txt sneakers.txt
Options for removing files -- and directories -- include:
-i
-- interactive. Prompts you to confirm the
deletion. This is good.
-f
-- force. Overrides interactive mode and
removes the file(s) without prompting. This might not be good,
unless you know exactly what you're doing.
-v
-- verbose. Shows a listing of files as
they're being removed.
-r
-- recursive. When removing directories,
will remove all of the files and the subdirectories of the
specified directory. This can also get rid of an empty directory.
To remove directories with
rm
, you must specify the
-r
option.
For example, if you want to recursively remove the directory
tigger
you would type:
rm -r tigger
And if you want to combine options, such as forcing a recursive
deletion, you can type:
rm -rf tigger
Be careful!
The
rm
is a powerful command, and can delete your
entire system! If you're root and you type the simple command
rm -rf /
you're sunk -- like a snake eating its
tail, the command will recursively remove everything on your system.
A safer alternative to using
rm
for removing
directories is the
rmdir
command. With this
command, you won't be allowed to use recursive deletions, so a
directory which has files in it won't be deleted.
Read the
rmdir
man page by typing
man
rmdir
to find out more about the command. |
| Markdown | | Red Hat Linux 6.2: The Official Red Hat Linux Getting Started Guide | | |
|---|---|---|
| [Prev](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/s1-managing-file-types.htm) | Chapter 14. Working with Files and Directories | [Next](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/p7405.htm) |
***
# [Copying, Moving and Renaming Files and Directories]()
By now, you've learned a little about the structure of the filesystem; and you've learned how to create files and directories.
But just because you know how to create files and directories doesn't mean that you're stuck with the changes you've made. What if you want to rename and/or move files and directories?
Let's start with the copy command.
## [Copying Files]()
Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you're copying, moving, or deleting files and directories.
Basically, the copy command is not much more complex than typing:
so to copy the file sneakers.txt to the directory tigger in your login directory, just type:
Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with **cp**. Our login directory is the parent of the directory tigger; meaning that tigger is one directory down from ours.
Read the **cp** man page (**man cp**) for a full list of the options available with **cp**. But among the options you can use with **cp** are:
- **\-i** -- interactive. Prompts you to confirm if the file is going to overwrite a file in your destination. This is a handy option because it can help prevent you from making mistakes.
- **\-r** -- recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location.
- **\-f** -- force. Copies without prompting you for confirmation that the file should be overwritten. Unless you're sure you want to force the copy, you probably don't want to make friends with this option right now.
- **\-v** - verbose. Will show the progress of the files being copied.
Just by using **cp** alone, you won't see much when the command is executed. Using an option, such as **\-i**, can make the process a little more useful, because if you want to copy a file to a location that already has a file with the same name, you'll be asked first if you really want to overwrite -- meaning replace -- the file that's already there.
| | |
|---|---|
|  | **Don't be too "forceful"** |
| | Remember that among your options is **\-f** (force), which can overwrite files without asking you if you're certain. Make sure, when you use the force option, that you *really* want to overwrite a file. |
Now that we have the file sneakers.txt in the tigger directory, let's use **cp -i** to copy the file again to the same location.
To overwrite the file that's already there, press **Y** and then **Enter**. Don't want to overwrite the file? Now is the time to press **N** and **Enter**.
## [Moving Files]()
To move files, use the **mv** command (**man mv**), which is similar to the **cp** command, except that with **mv** the file is physically moved from one place to another, instead of being duplicated, as with **cp**.
Common options available with **mv** include:
- **\-i** -- interactive. Will prompt you if the file you've selected will overwrite an existing file in the destination directory. This is a good option, because like the **\-i** option in **cp**, you'll be given the chance to make sure you want to replace an existing file.
- **\-f** -- force. Overrides the interactive mode and moves without prompting. Unless you know what you're doing, this option doesn't play nice; be very careful about using it until you become more comfortable with your system.
- **\-v** -- verbose. Shows a list of the files being moved.
If you want to move a file out of your home directory and into another directory, you would type:
or, **mv sneakers.txt /home/newuser /home/newuser/tigger** using absolute pathnames.
## [Renaming Files]()
Actually, we've already covered half of renaming, because when you copy or move files, you can also rename.
To copy the file sneakers.txt from our login directory to our tigger subdirectory, just type:
To copy and rename that file from sneakers.txt to piglet.txt, type:
To *move* and rename the file, just substitute **mv** for **cp** in the above example.
If you **cd** to tigger and use **ls**, you'll see the file **piglet.txt**.
If you just want to rename the file and keep its location, just **mv** in your current directory:
## [Deleting Files and Directories]()
We talked about creating files with the **touch** command and by using redirection in [Chapter 13](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/ch-navigating.htm). And we created the directory tigger using **mkdir**.
But we haven't discussed how to delete files and directories.
Deleting files and directories with the **rm** command (**man rm**) is a straightforward process.
Let's take our new file piglet.txt, and delete it from the tigger directory with the **rm** command:
What happens if we didn't really want to get rid of it? Too late! Again, that's where the **\-i** (interactive) option comes in handy, because it gives a second chance to think about whether we really want to toss the file.
You can also delete files using the wildcard **\***, but be careful, because you can easily delete files you didn't intend to throw away.
To remove a file using a wildcard, you would type:
You can also remove more than one file in one command, as in:
Options for removing files -- and directories -- include:
- **\-i** -- interactive. Prompts you to confirm the deletion. This is good.
- **\-f** -- force. Overrides interactive mode and removes the file(s) without prompting. This might not be good, unless you know exactly what you're doing.
- **\-v** -- verbose. Shows a listing of files as they're being removed.
- **\-r** -- recursive. When removing directories, will remove all of the files and the subdirectories of the specified directory. This can also get rid of an empty directory.
To remove directories with **rm**, you must specify the **\-r** option.
For example, if you want to recursively remove the directory **tigger** you would type:
And if you want to combine options, such as forcing a recursive deletion, you can type:
| | |
|---|---|
|  | **Be careful\!** |
| | The **rm** is a powerful command, and can delete your entire system! If you're root and you type the simple command **rm -rf /** you're sunk -- like a snake eating its tail, the command will recursively remove everything on your system. |
A safer alternative to using **rm** for removing directories is the **rmdir** command. With this command, you won't be allowed to use recursive deletions, so a directory which has files in it won't be deleted.
Read the **rmdir** man page by typing **man rmdir** to find out more about the command.
***
| | | |
|---|---|---|
| [Prev](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/s1-managing-file-types.htm) | [Home](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/index.htm) | [Next](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/p7405.htm) |
| Identifying and Working with File Types | [Up](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/ch-shellmanage.htm) | Q & A | |
| Readable Markdown | By now, you've learned a little about the structure of the filesystem; and you've learned how to create files and directories.
But just because you know how to create files and directories doesn't mean that you're stuck with the changes you've made. What if you want to rename and/or move files and directories?
Let's start with the copy command.
## [Copying Files]()
Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you're copying, moving, or deleting files and directories.
Basically, the copy command is not much more complex than typing:
```
cp <source> <destination>
```
so to copy the file sneakers.txt to the directory tigger in your login directory, just type:
```
cp sneakers.txt tigger
```
Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with **cp**. Our login directory is the parent of the directory tigger; meaning that tigger is one directory down from ours.
Read the **cp** man page (**man cp**) for a full list of the options available with **cp**. But among the options you can use with **cp** are:
- **\-i** -- interactive. Prompts you to confirm if the file is going to overwrite a file in your destination. This is a handy option because it can help prevent you from making mistakes.
- **\-r** -- recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location.
- **\-f** -- force. Copies without prompting you for confirmation that the file should be overwritten. Unless you're sure you want to force the copy, you probably don't want to make friends with this option right now.
- **\-v** - verbose. Will show the progress of the files being copied.
Just by using **cp** alone, you won't see much when the command is executed. Using an option, such as **\-i**, can make the process a little more useful, because if you want to copy a file to a location that already has a file with the same name, you'll be asked first if you really want to overwrite -- meaning replace -- the file that's already there.
| | |
|---|---|
|  | **Don't be too "forceful"** |
| | Remember that among your options is **\-f** (force), which can overwrite files without asking you if you're certain. Make sure, when you use the force option, that you *really* want to overwrite a file. |
Now that we have the file sneakers.txt in the tigger directory, let's use **cp -i** to copy the file again to the same location.
```
[newuser@localhost newuser]$cp -i sneakers.txt tigger
cp: overwrite 'tigger/sneakers.txt'?
```
To overwrite the file that's already there, press **Y** and then **Enter**. Don't want to overwrite the file? Now is the time to press **N** and **Enter**.
## [Moving Files]()
To move files, use the **mv** command (**man mv**), which is similar to the **cp** command, except that with **mv** the file is physically moved from one place to another, instead of being duplicated, as with **cp**.
Common options available with **mv** include:
- **\-i** -- interactive. Will prompt you if the file you've selected will overwrite an existing file in the destination directory. This is a good option, because like the **\-i** option in **cp**, you'll be given the chance to make sure you want to replace an existing file.
- **\-f** -- force. Overrides the interactive mode and moves without prompting. Unless you know what you're doing, this option doesn't play nice; be very careful about using it until you become more comfortable with your system.
- **\-v** -- verbose. Shows a list of the files being moved.
If you want to move a file out of your home directory and into another directory, you would type:
```
mv sneakers.txt tigger
```
or, **mv sneakers.txt /home/newuser /home/newuser/tigger** using absolute pathnames.
## [Renaming Files]()
Actually, we've already covered half of renaming, because when you copy or move files, you can also rename.
To copy the file sneakers.txt from our login directory to our tigger subdirectory, just type:
```
cp sneakers.txt tigger
```
To copy and rename that file from sneakers.txt to piglet.txt, type:
```
cp sneakers.txt tigger/piglet.txt
```
To *move* and rename the file, just substitute **mv** for **cp** in the above example.
If you **cd** to tigger and use **ls**, you'll see the file **piglet.txt**.
If you just want to rename the file and keep its location, just **mv** in your current directory:
```
mv sneakers.txt piglet.txt
```
## [Deleting Files and Directories]()
We talked about creating files with the **touch** command and by using redirection in [Chapter 13](http://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/ch-navigating.htm). And we created the directory tigger using **mkdir**.
But we haven't discussed how to delete files and directories.
Deleting files and directories with the **rm** command (**man rm**) is a straightforward process.
Let's take our new file piglet.txt, and delete it from the tigger directory with the **rm** command:
```
rm piglet.txt
```
What happens if we didn't really want to get rid of it? Too late! Again, that's where the **\-i** (interactive) option comes in handy, because it gives a second chance to think about whether we really want to toss the file.
```
[newuser@localhost newuser]$rm -i piglet.txt
rm: remove 'piglet.txt'?
```
You can also delete files using the wildcard **\***, but be careful, because you can easily delete files you didn't intend to throw away.
To remove a file using a wildcard, you would type:
```
rm pig*
```
You can also remove more than one file in one command, as in:
```
rm piglet.txt sneakers.txt
```
Options for removing files -- and directories -- include:
- **\-i** -- interactive. Prompts you to confirm the deletion. This is good.
- **\-f** -- force. Overrides interactive mode and removes the file(s) without prompting. This might not be good, unless you know exactly what you're doing.
- **\-v** -- verbose. Shows a listing of files as they're being removed.
- **\-r** -- recursive. When removing directories, will remove all of the files and the subdirectories of the specified directory. This can also get rid of an empty directory.
To remove directories with **rm**, you must specify the **\-r** option.
For example, if you want to recursively remove the directory **tigger** you would type:
```
rm -r tigger
```
And if you want to combine options, such as forcing a recursive deletion, you can type:
```
rm -rf tigger
```
| | |
|---|---|
|  | **Be careful\!** |
| | The **rm** is a powerful command, and can delete your entire system! If you're root and you type the simple command **rm -rf /** you're sunk -- like a snake eating its tail, the command will recursively remove everything on your system. |
A safer alternative to using **rm** for removing directories is the **rmdir** command. With this command, you won't be allowed to use recursive deletions, so a directory which has files in it won't be deleted.
Read the **rmdir** man page by typing **man rmdir** to find out more about the command. |
| Shard | 40 (laksa) |
| Root Hash | 6413155587347916440 |
| Unparsed URL | tw,edu,kh!ftp,/Linux/Redhat/en_6.2/doc/gsg/s1-managing-working-with-files.htm h80 |