How to Rename Directories in Linux via the Command Line?

Page content

Command-Line Renaming Directories

Most Linux distributions now come with a GUI and a graphic file management system. On GNOME-based distributions this will usually be Nautilus, and on KDE systems, Konqueror. Both work in a similar way to the Windows File Manager, in that directories can be renamed by clicking on them and selecting ‘Rename’ from the local menu.

The command-line Terminal Window can also be used to rename directories, but users will need to know about the Linux file structure. The Linux file system stems from a single root directory represented by a slash character (Windows uses a backslash). Immediately under this there are usually between fifteen and twenty system directories with names like bin, dev and etc. We are concerned with the directory called home, which contains the individual user folders. If Tom, Sue and I all have accounts on the same PC, there will be folders under the home folder called ‘tom’, ‘sue’ and ‘jon’.

A command-line Terminal window will be accessible from one of the menus; in Ubuntu Linux it is found by default under Applications / Accessories. This will open up a text entry window with a prompt and a flashing cursor. The prompt includes a path string that tells the user whereabouts on the system they are: for instance:

[email protected]:/# – I am located in the root directory

[email protected]:/home# – I am located in the home directory

A tilde character is used to indicate the user’s own subdirectory: e.g.

[email protected]:~# – I am located in my subdirectory under /home i.e. /home/jon

Using cd to navigate

The ‘cd’ (change directory) command allows me to move through the system. To move one level upwards towards the root directory I use the command

cd ..

and to move downwards into a subdirectory I enter ‘cd’ followed by the directory name; e.g. if I am in the root directory then ‘cd home’ will take me into /home and ‘cd /home/jon’ will take me straight into the /home/jon directory.

The ‘cd ~’ (tilde) command will always take me to my home directory, and ‘cd /’ will take me straight to the root directory. Because Linux is case-sensitive – ‘cd’ is not the same as ‘CD’ and ‘newstuff’ is not the same as ‘Newstuff’; you need to watch your use of upper and lower case.

Suppose that I am in the /home/jon/ directory and there is a directory there called ‘Oldname’ which I want to change to ‘Newname’. The basic command for renaming a directory is the move command,‘mv’, followed by the old name, followed by the new name; so the command I need here would be:

mv Oldname Newname

An alternative would be

mv /home/jon/Oldname /home/Jon/Newname

where the complete path names are supplied.

For further information about Linux terminal commands such as cd and mv, type the word ‘man’ at the terminal prompt followed by the name of the command:

man cd

man mv