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.



Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

OBS Studio settings for best quality (loseless) recording

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

Install VMAF on Fedora, CentOS/Red Hat (RHEL)