Как в линуксе переименовать папку

How do I rename a directory via the command line?

I have got the directory /home/user/oldname and I want to rename it to /home/user/newname . How can I do this in a terminal?

7 Answers 7

mv /home/user/oldname /home/user/newname 

This will not work if the new name is already an existing directory. Instead, it will move the old directory inside the new one.

If the directory name is the same with capitalization you will get No such file or directory . To avoid this do something like mv /home/user/Folder /home/user/temp; mv /home/user/temp/ /home/user/folder .

To just rename a file or directory type this in Terminal:

with space between the old and new names.

To move a file or directory type this in Terminal.

it will move the file to the desktop.

mv -T /home/user/oldname /home/user/newname 

That will rename the directory if the destination doesn’t exist or if it exists but it’s empty. Otherwise it will give you an error.

mv /home/user/oldname /home/user/newname 

One of two things will happen:

  • If /home/user/newname doesn’t exist, it will rename /home/user/oldname to /home/user/newname
  • If /home/user/newname exists, it will move /home/user/oldname into /home/user/newname , i.e. /home/user/newname/oldname

Doesn’t work if you want to capitalize the directory name in a case-insensitive filesystem (likely on MacOS). mv -T $PWD/analisys $PWD/Analisys returns mv: ‘/Users/sixtykeys/Projects/murphy/tmp/analisys’ and ‘/Users/sixtykeys/Projects/murphy/tmp/Analisys’ are the same file . I worked around this by using an intermediate name (i.e. analisys_ ).

The command may not have been successful due to the limitations of the filesystem, but from another perspective it was successful in interpreting your intentions (renaming a directory, not moving it) 🙂

Источник

Rename a file or folder

As with other file managers, you can use Files to change the name of a file or folder.

To rename a file or folder:

  1. Right-click on the item and select Rename , or select the file and press F2 .
  2. Type the new name and press Enter or click Rename .

You can also rename a file from the properties window.

When you rename a file, only the first part of the name of the file is selected, not the file extension (the part after the last . ). The extension normally denotes what type of file it is (for example, file.pdf is a PDF document), and you usually do not want to change that. If you need to change the extension as well, select the entire file name and change it.

Читайте также:  Manjaro linux восстановить загрузчик

If you renamed the wrong file, or named your file improperly, you can undo the rename. To revert the action, immediately click the menu button in the toolbar and select Undo Rename , or press Ctrl + Z , to restore the former name.

Valid characters for file names

You can use any character except the / (slash) character in file names. Some devices, however, use a file system that has more restrictions on file names. Therefore, it is a best practice to avoid the following characters in your file names: | , \ , ? , * , < , " , : , >, / .

If you name a file with a . as the first character, the file will be hidden when you attempt to view it in the file manager.

Common problems

You cannot have two files or folders with the same name in the same folder. If you try to rename a file to a name that already exists in the folder you are working in, the file manager will not allow it.

File and folder names are case sensitive, so the file name File.txt is not the same as FILE.txt . Using different file names like this is allowed, though it is not recommended.

The file name is too long

On some file systems, file names can have no more than 255 characters in their names. This 255 character limit includes both the file name and the path to the file (for example, /home/wanda/Documents/work/business-proposals/… ), so you should avoid long file and folder names where possible.

The option to rename is grayed out

If Rename is grayed out, you do not have permission to rename the file. You should use caution with renaming such files, as renaming some protected files may cause your system to become unstable. See Set file permissions for more information.

More Information

You can choose the displayed language by adding a language suffix to the web address so it ends with e.g. .html.en or .html.de.
If the web address has no language suffix, the preferred language specified in your web browser’s settings is used. For your convenience:
[ Change to English Language | Change to Browser’s Preferred Language ]

The material in this document is available under a free license, see Legal for details.
For information on contributing see the Ubuntu Documentation Team wiki page. To report errors in this documentation, file a bug.

Источник

How to Rename a Directory in Linux

Renaming a directory is one of the most basic tasks you will perform on any operating system. The Linux terminal offers several different ways to rename directories using commands and scripts.

In this tutorial, we will go over the different methods you can use to rename a directory in Linux through the terminal window.

Читайте также:  Каким приложением занят порт linux

How to rename directories in Linux

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window/command line
  • Access to a text editor, such as Vim or Nano

Renaming Directories With the mv Command

The primary function of the mv command in Linux is moving files and directories from one place to another. It uses the following command syntax:

mv [options] [source] [destination]

Note: To learn more about using the mv command, check out our guide to moving directories in Linux.

If the destination directory does not exist, the mv command renames the source directory instead. In this case, the syntax changes to:

mv [options] [current directory name] [new directory name]

As an example, let’s say we have Directory1, Directory2, and Directory3 in our Home directory:

Checking the content of the Home directory using the ls command

To rename Directory1 into Example_Directory with the mv command, use:

mv Directory1 Example_Directory

There is no output if the command is successful, so we need to use the ls command to verify the name change:

Verifying the name change using the ls command

Renaming Directories With the rename Command

The rename command in Linux is a dedicated command used to change the names of files and directories. Using this command makes it easier to rename multiple directories at the same time.

Note: The rename command is not included in all Linux distributions by default. If your system is missing the rename command, install it with:

  • For Ubuntu and Debian, use sudo apt install rename
  • For CentOS and Fedora, use sudo yum install prename
  • For Arch Linux, use sudo pacman -S rename

Renaming a Single Directory With the rename Command

The rename command uses the following syntax:

rename [options] 's/[expression]/[replacement]/' [file name]

The command renames the file by replacing the first occurrence of the expression with the replacement. For example, if we want to rename Directory1 to Example_Directory:

rename 's/Directory1/Example_Directory/' *

In this example, we can see that the rename command syntax consists of several sections:

  • rename : Invokes the rename command.
  • s : Short for substitute, indicates that we are replacing the expression with the replacement.
  • / Directory1 : Specifies the expression or the part of the old directory name that you want to replace.
  • /Example_Directory /: Defines the replacement or the new directory name.
  • * : Searches the Home directory for names matching the provided expression.

Verifying the Home directory content with the ls command shows that the directory now has a new name:

Verifying the new directory name with the ls command

Renaming Multiple Directories With the rename Command

The rename command provides a way to rename multiple directories at the same time without using bash scripts. For instance, if we want to rename Directory1, Directory2, and Directory3 to Folder1, Folder2, and Folder3:

rename -v 's/Directory/Folder/' *
  • -v : Invokes the verbose output, listing each step of the process.
  • ‘s/Directory/Folder/’ : Replaces Directory in the names of the search results with Folder.
  • * : Searches the Home directory for names matching the provided expression.
Читайте также:  Linux команда перехода директорию

Using the rename command to change multiple directory names

The rename command can also translate file names by using the y argument instead of the s argument. In this case, it translates one string of characters into another, character-for-character.

The command above translates every a character into d, every b into e, and every c into f.

In the example below, we translated blank spaces in directory names to underscores (_).

Translating directory names using the rename command

Renaming Directories With the find Command

In case you are not sure where the directory you want to rename is located, using the find command with the mv command lets you search for it and rename it when it’s found:

find . -depth -type d -name [current directory name] -execdir mv <> [new directory name] \;

In the example above, -execdir executes the mv command once the find command locates the directory.

For instance, the command below finds and renames Directory1 into Example_Directory:

find . -depth -type d -name Directory1 -execdir mv <> Example_Directory \;

Using the find command to rename a directory

Renaming Directories With Bash Scripts

Using bash scripts is another way of renaming multiple directories at the same time. Unlike the rename command, bash scripts allow you to save a template for future use.

Start by creating the script with a text editor, such as Nano:

sudo nano rename_directories.sh

The following example is a bash script that searches for directories and appends the current date to their name:

#!/bin/bash #Searches for directories and renames them according to the specified pattern for d in * do if [ -d "$d" ] then mv -- "$d" "_$(date +%Y%m%d)" fi done 
  • The first line instructs the script to go through all files and directories in the current location.
  • Lines 2 and 3 check for directories.
  • Lines 4 and 5 append the current date to the name of any directory found.

Press Ctrl+X, type Y, and press Enter to close and save the script.

As an example, let’s use the script above to change the names of Directory1, Directory2, and Directory3, located in the Example directory.

Start by moving in to the Example directory:

Learn more about Linux cd command in our guide on how to change and move to different categories.

Next, execute the script by using the sh command:

Using the ls command allows us to verify the name change:

Using the ls command to verify the name change

After reading this article, you should know how to rename directories in Linux using commands and bash scripts.

Learn more about using Linux commands in our Linux Commands All Users Should Know .

Источник

Оцените статью
Adblock
detector