|
|
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
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 are copying, moving, or deleting files and directories. To copy a file, type the following command: cp <source> <destination> So, to copy the file sneakers.txt to the directory tigger in your login directory, move to your login directory and 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; tigger is one directory down from our login directory. Read the cp man page (man cp) for a full list of the options available with cp. Among the options you can use with cp are the following:
Now that you have the file sneakers.txt in the tigger directory, 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 is already there, press [Y] and then [Enter]. If you do not want to overwrite the file, press [N] and [Enter]. Moving Files To move files, use the mv command. It 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. For more about mv, see the mv man page (type man mv). Common options for mv include the following:
mv sneakers.txt tigger Alternatively, the same command using absolute pathnames looks like mv sneakers.txt /home/newuser /home/newuser/tigger. Renaming Files Actually, we have already covered half of renaming, because when you copy or move files, you can also rename. To copy the file sneakers.txt from your login directory to the 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 then type ls, you will 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 You learned about creating files with the touch command and by using redirection in Chapter 10 . And you created the directory tigger using mkdir. Now you need to learn how to delete files and directories. Deleting files and directories with the rm command is a straightforward process. See the rm man page for more information. Options for removing files and directories include:
rm piglet.txt What happens if you did not really want to get rid of it? Too late! That is where the -i (interactive) option is helpful, because it gives you a second chance to think about whether or not you really want to delete 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 did not intend to throw away. To remove a file using a wildcard, you would type: rm pig* The above command will remove all files in the directory which start with the letters "pig." You can also remove more than one file using one command: rm piglet.txt sneakers.txt Options for removing files and directories include the following:
For example, if you want to recursively remove the directory tigger you would type: rm -r tigger If you want to combine options, such as forcing a recursive deletion, you can type: rm -rf tigger Be Careful When Using rm! The rm command can delete your entire filesystem! If you are logged in as root and you type the simple command rm -rf /, you are in trouble; this 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 will not be allowed to use recursive deletions, so a directory which has files in it will not be deleted. Read the rmdir man page (man rmdir) to find out more about this command. http://www.centos.org/docs/2/rhl-gsg...ith-files.html |
![]() |
| Thread Tools | |
| Display Modes | |
|
|