How to rename a directory using Terminal on Ubuntu/Linux machine?

Use the mv command to rename a folder or directory via command line on a Ubuntu/Linux machine.

The word mv stands for Move.

mv -T dir_name dir_new_name

The option -T generates an error if the folder with the new name already exists.

Use the below command to rename a directory in the current working directory.

mv -T sample sample_new

Use the below command to rename a directory from anywhere on the machine, using its absolute path.

mv -T /var/www/sample /var/www/sample_new

Both these commands will rename the directory as needed, the only difference is the first command used the relative path, and the second command used the absolute path of the directory.

Related Links