Learn GNU/Linux Commands (4): Move/Rename/Delete Directories/Files - mv, rm
Commands we mentioned can be achieved with the mouse in a desktop environment, but they are worth remembering. With these commands, we can easily write scripts to do tasks automatically.
Move Directory(ies)/Files
mv SOURCE... DIRECTORY # or
mv -t DIRECTORY SOURCE...
Move SOURCE(s) to DIRECTORY.
move the SOURCE, no matter it is a file or a directory under DIRECTORY. If SOURCE is a directory, the files and directories under it will be moved too.
The pathname following "-t" will be treated as of DIRECTORY.
Options
- -n, --no-clobber: do not overwrite an existing file.
- -u, --update: move only when the SOURCE file is newer than the destination file or when the destination file is missing.
Rename Directory(ies)/Files
mv SOURCE DEST
"mv" can also rename files or directories.
If DEST doesn't exist, SOURCE will be renamed to DEST.
If both SOURCE and DEST are files, DEST will be removed and SOURCE will be renamed to DEST.
If both SOURCE and DEST are directories, we should use the option "-T", so that DEST will be removed and SOURCE will be renamed to DEST. Otherwise, SOURCE will be moved under DEST.
Delete Files
rm [FILE]...
Remove the FILE(s).
Multiple files can be removed with one command.
Delete Directories
rm -r [DIRECTORY]...
Remove the DIRECTORY(ies).
-r, -R, --recursive: remove directories and their contents recursively.
Multiple directories can be removed with one command.
"rm -r" removes directories no matter they are empty or not. By the way, "rm -d" and "rmdir" can also remove empty directories.
Comments
Post a Comment