Using mv

It sometimes strikes people as odd that mv is a command both for moving and for renaming files. Actually it's quite logical: If you move a file from the current directory to somewhere else, what happens is that the file appears over there and disappears here. If you rename the file, the copy with the new name appears, and the copy with the old name disappears. And of course you can copy the file to a new location and a new name with just one invocation of the mv command. For example:

moves afile to the / tmp directory and [email protected]:~> mv afile bfile renames afile to bfile in the current directory, whereas [email protected]:~> mv afile /tmp/bfile moves afile to the /tmp directory and renames the file to bfile at the same time.

Take care. The rm command removes files and doesn't (by default) give you any second chances, so it is dangerous. There are various ways to use rm to make it less final, but none of them are totally satisfactory.

You may choose to use rm -i in place of rm; this makes it interactive, and you will be prompted before the file is actually removed:

[email protected]:~/directory> Is afile [email protected]:~/directory> rm -i afile rm: remove regular empty file 'afile'? y [email protected]:~/directory>

If you like this, you can create an alias in the file .alias in your home directory (or in your .bashrc file) to make rm always behave like rm -i. You can add a line like this:

to ~/.alias. When you log in the next time, you will see this behavior:

[email protected]:~/directory> ls afile [email protected]:~/directory> rm afile rm: remove regular empty file 'afile'? y [email protected]:~/directory>

So rm is behaving like rm -i . If you don't want to be prompted, you will now need to use rm -f.

| f - - r The only problem with doing this is that it gives you a false sense of security. If you ' - ~ ■"■> ■ are logged in on a system where you have not set up the alias, you may remember too late that this safety blanket was not available. In some ways the best advice is to always think hard about what you're doing before you press the Return key (and always keep regular backups).

Some people use other more elaborate solutions to take the sting out of rm, such as aliasing it to a command that moves the files to a trash directory somewhere. This kind of solution suffers the same disadvantage (you may get used to it too much). There is also the possible problem that when you delete more than one file with the same name you are not sure which one still exists in the trash directory.

You can use rm recursively with the -r option. The rm -rf command recursively removes a directory and everything in it. The rm - rf / (as root) command removes everything on your system.

Was this article helpful?

0 0

Post a comment