How to Read in a File to Another File in Unix

Copying, Moving and Renaming Files and Directories

By at present, you've learned a lilliputian about the structure of the filesystem; and you've learned how to create files and directories.

But merely because you know how to create files and directories doesn't hateful that yous're stuck with the changes y'all've made. What if you want to rename and/or motility files and directories?

Allow's outset with the re-create command.

Copying Files

Like so many Linux features, y'all have a variety of options from which to cull when you want to dispense files and directories. You tin can likewise use wildcards when yous're copying, moving, or deleting files and directories.

Basically, the copy command is not much more complex than typing:

              cp                                                      <source>                                                                                        <destination>                                                            

so to re-create the file sneakers.txt to the directory tigger in your login directory, just type:

Detect that you also used relative pathnames to copy the file. You lot can use both relative and absolute pathnames with cp. Our login directory is the parent of the directory tigger; meaning that tigger is ane directory down from ours.

Read the cp man page (homo cp) for a full list of the options available with cp. Just among the options you can use with cp are:

  • -i -- interactive. Prompts yous to ostend if the file is going to overwrite a file in your destination. This is a handy pick because information technology can help forbid you from making mistakes.

  • -r -- recursive. Rather than merely copying all the files and directories, copies the whole directory tree, subdirectories and all, to some other location.

  • -f -- force. Copies without prompting yous for confirmation that the file should exist overwritten. Unless you're certain y'all want to strength the copy, y'all probably don't desire to brand friends with this option right now.

  • -5 - verbose. Will testify the progress of the files being copied.

Merely by using cp lonely, you lot won't see much when the command is executed. Using an selection, such every bit -i, can make the process a fiddling more useful, because if you want to copy a file to a location that already has a file with the same proper noun, yous'll exist asked beginning if you actually want to overwrite -- significant supercede -- the file that's already there.

Tip Don't be too "forceful"

Think that amongst your options is -f (strength), which can overwrite files without asking you if y'all're certain. Make sure, when yous use the force option, that y'all really want to overwrite a file.

At present that nosotros have the file sneakers.txt in the tigger directory, let'due south 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'south already there, press Y and then Enter. Don't want to overwrite the file? Now is the fourth dimension to printing N and Enter.

Moving Files

To motion files, utilize the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from i place to another, instead of existence duplicated, as with cp.

Common options available with mv include:

  • -i -- interactive. Will prompt you if the file you lot've selected volition overwrite an existing file in the destination directory. This is a proficient option, because similar the -i pick in cp, you'll be given the chance to brand certain you want to supersede an existing file.

  • -f -- force. Overrides the interactive manner and moves without prompting. Unless you know what you're doing, this option doesn't play overnice; be very careful virtually using information technology until yous become more than comfortable with your system.

  • -5 -- verbose. Shows a list of the files being moved.

If you lot want to motility a file out of your abode directory and into another directory, y'all would type:

or, mv sneakers.txt /home/newuser /home/newuser/tigger using absolute pathnames.

Renaming Files

Actually, nosotros've already covered half of renaming, because when you copy or motion files, you can also rename.

To re-create the file sneakers.txt from our login directory to our tigger subdirectory, just type:

To re-create and rename that file from sneakers.txt to piglet.txt, type:

              cp sneakers.txt tigger/piglet.txt            

To move and rename the file, simply substitute mv for cp in the higher up example.

If you lot cd to tigger and employ ls, you'll encounter the file piglet.txt.

If you just desire to rename the file and keep its location, just mv in your electric current directory:

              mv  sneakers.txt piglet.txt            

Deleting Files and Directories

Nosotros talked about creating files with the touch control and by using redirection in Chapter 13. And nosotros 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 (human being rm) is a straightforward process.

Allow's take our new file piglet.txt, and delete it from the tigger directory with the rm control:

What happens if we didn't really want to get rid of it? Too late! Once again, that's where the -i (interactive) selection comes in handy, because information technology gives a second hazard to recollect about whether we really want to toss the file.

              [newuser@localhost newuser]$                                            rm -i piglet.txt                            rm: remove 'piglet.txt'?            

You can besides delete files using the wildcard *, only be careful, because you lot can easily delete files you lot didn't intend to throw away.

To remove a file using a wildcard, y'all would type:

You can also remove more i file in one control, 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 skillful, 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 case, if you want to recursively remove the directory tigger you would type:

And if you want to combine options, such equally forcing a recursive deletion, you can blazon:

Caution Be conscientious!

The rm is a powerful command, and can delete your entire system! If you're root and you type the elementary command rm -rf / you're sunk -- like a snake eating its tail, the control 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 utilize recursive deletions, so a directory which has files in it won't exist deleted.

Read the rmdir man page past typing homo rmdir to find out more than nearly the command.

jordanrandiall.blogspot.com

Source: https://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/s1-managing-working-with-files.htm

0 Response to "How to Read in a File to Another File in Unix"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel