How to move (rename) files and directories in Bash?

To move or rename files and directories in Bash, you can use the mv command. The mv command can be used to move files/directories from one location to another or to simply rename them.

Here are some examples of how to use the mv command in Bash:

  1. Moving a file/directory to another location:

    mv /path/to/source /path/to/destination

    This command will move the file/directory located at /path/to/source to the specified /path/to/destination. If the destination is a directory, the source will be moved into that directory.

  2. Renaming a file/directory:

    mv /path/to/oldname /path/to/newname

    This command will rename the file/directory located at /path/to/oldname to the specified /path/to/newname.

  3. Moving multiple files/directories to a destination directory:

    mv file1 file2 file3 /path/to/destination

    This command will move the listed files (file1, file2, and file3) to the specified /path/to/destination directory.

  4. Moving and combining multiple files into a single destination file:

    mv file1 file2 file3 /path/to/destination/file

    This command will move and combine the content of file1, file2, and file3 into a single file located at /path/to/destination/file. If the destination file does not exist, it will be created. If it already exists, the content of the source files will be appended to the destination file.

  5. Prompting for confirmation before overwriting a file:

    mv -i /path/to/source /path/to/destination

    The -i option prompts for confirmation before overwriting an existing destination file.

Note: The mv command can also be used to move or rename directories recursively.