How to Move and Rename Files and Directories in Linux (Easy Guide)

This blog post will teach you how to move and rename files and directories in Linux using the mv command.

Moving and Renaming Files and Directories

  • mv – move and/or rename files and directories
  • Usage:
    • mv [options] file destination
  • More than one file may be moved at a time if the destination is a directory:
    • mv [options] file1 file2 destdir
  • Destination works like cp

Moving files with mv

mv must always be given at least two arguments. Aside from a couple of switches, mv and cp function identically — the only difference is that cp results in matching identical files; with mv, the source disappears, leaving only the destination file(s).

When two arguments are given:

The first argument is interpreted as the destination. It may or may not be prepended with a relative or absolute path name. if it names an existing directory, the source file is moved to that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and the source file is moved and/or renamed to that destination name.

When more than two arguments are given:

All arguments but the last are interpreted as source files. they may or may not be prepended with a relative or absolute path name.

The last argument is interpreted as a destination directory. It may or may not be prepended with a relative or absolute path name. the source files are moved, with their original file names, to the destination directory.

Example: –

amar@amar-pc:~/Desktop$ ls abcd/
testfile.txt
amar@amar-pc:~/Desktop$ mv abcd/testfile.txt /test/abcd_testfile.txt
amar@amar-pc:~/Desktop$ ls /test/
abcd_testfile.txt
amar@amar-pc:~/Desktop$
move and rename files and Directories

Leave a Comment