How to rename folders linux

How To Rename Directories and Folders In Linux?

Renaming or moving directories and folder can be tricky if those directories and folders have some subfolders. Or the destination may have all ready same name folder which will create some error. In this tutorial we will look how to rename and move directories and folders in Linux.

Rename Using Mv Command

The defacto command for rename directories and folders is mv . mv is the short form for the move . We can simply rename by providing the current directory and folder name and destination directory or folder name. Syntax is like below. If the source or current directory folder have content we need to rename by using recursive move. This will only change given directory or folder name but move all sub files and folder.

In this example we will rename directory named backup into old_backup .

Verbose

While renaming or moving files and folders we may need to get verbose information. Verbose mode will print every move or rename. This will be helpful for recursive option. We will provide -v to enable verbose.

Verbose

Overwrite Forcibly If Exists

In some cases there may be existing folder or directory with the new name. We need to confirm the overwrite. But this can be daunting task if there is a lot of them. We can overwrite existing files and folder with -f option automatically. -f means forcibly.

Prompt For Confirmation Before Overwriting

If we do not want to write existing file forcibly we can confirm for each file rename. In this case we have to use -i like below.

Источник

Unix Command To Rename A Folder

H ow do I rename a folder under UNIX operating systems using a command line tools?

You need to use the mv command to rename a folder under UNIX operating systems. You must have write permission for all folders.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m

Syntax to rename a directory on Unix

The syntax is as follows:
mv old-folder-name new-folder-name

mv /path/to/old /path/to/new

Example: Unix rename a directory command

In this example, a folder called drivers is renamed as olddrivers. Open a terminal (shell prompt) and type the following commands:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

OR pass the -v option to get verbose output:

Do not prompt for confirmation before overwriting

Prompt for confirmation before overwriting

Pass the -n option to mv command to do not overwrite an existing file/dirname. The -n option overrides any previous -f or -i options:

Recommend readings

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to rename multiple folders in Linux using command line

How to rename multiple folders in Linux

  1. To rename multiple folders, one can use rename command from MariaDB/MySQL or Linux utilities package.
  2. Another option is to use the bash for loop.

Linux rename multiple folders using rename command

The syntax is:
rename expression replacement file

rename command examples

Let us see some examples. Create some files as follows using the touch command touch file<1..4>.txtz
List those files:
ls file*
Now correct file extension from .txtz .txt i.e. fix the extension of your .txtz files:
rename -v .txtz .txt *.txtz
Verify with the help of ls command ls file*

Where,

  • -v : Verbose output
  • .txtz Match all .txtz extension
  • .txt Replace with .txt
  • *.txtz Work on all *.txtz file in the current working directory

Working on folders

Getting help on rename command

Run man command as follows:
man rename
OR
rename —help
Sample outputs:

Renaming multiple folders using bash for loop

Say you have dirs as follows:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

We need remove the white spaces in the folder names. Run:

mmv – a Command line tool for Renaming multiple files in Linux

First install mmv using the apt command/apt-get command/yum command/apt-get command as per your distro:
sudo yum install mmv ###
sudo apt install mmv ###
sudo dnf install mmv ###
Sample outputs from Fedora 30 box:

The mvm command move, copy, append/link multiple files/dirs by wildcard patterns. The syntax is:
mmv from to
mmv [options] from to

Say you wan to make all folder names uppercase, try:
mmv «dir*» «#u1»
If you wan to make all folder names lowercase, try:
mmv «dir*» «#l1»

You can change a suffix i.e. extension from *.C to *.cpp as follows:
mmv ‘*.C’ ‘#1.cpp’

the extension “.bar” is lost and the extension “.foo” is added.
The mmv command is powerful cli tool, and it has many more options. Thus, read the man page of the mmv:
man mmv

Use thunar GUI tool to rename multiple folder at once in Linux

The thunar is an easy to use file manager for the Xfce Desktop Environment. You can install it as follows:
sudo yum install thunar ###
sudo apt install thunar ###
sudo dnf install thunar ###
Sample session from thunar:

File Manager for the Xfce Desktop Environment with the bulk renamer

Conclusion

You learned how to rename multiple folders in Linux using various options. See my page “Linux Rename Multiple Files At a Shell Prompt” for more information. The rename command is part of the util-linux package and is available here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Linux Rename Folder Command

Linux Rename Folder Command

The procedure to rename a folder or directory on Linux:

  1. Open the Terminal application.
  2. Type the following command to rename foo folder to bar:
    mv foo bar
    You can use full path too:
    mv /home/vivek/oldfolder /home/vivek/newfolder

Let us see examples and syntax in details.

How to rename folders in Linux

The pwd command shows current working directory:
pwd
Sample outputs:

mv command syntax form is as follows:
mv old-name new-name
mv [options] old-name new-name
mv [options] source target
To rename a directory called pics to mypictures in the current directory:
pwd
ls -l
mv pics mypictures
Verify it with the ls command:
ls -l
If the mypictures folder is located in the same directory as the pics folder, then the pics folder can only be renamed. Hence always use pwd command and ls command to find out your current working directory.

How to move folders in Linux

The following command would move a folder named accounting, without changing its name, from the current directory to an existing directory named /mnt/backups/:
mv accounting /mnt/backups/
You can give full path too:
mv /home/vivek/accounting/ /mnt/backups/
Let us move multiple files/folders. The following mv command moves all files and directories, including all the contents of those directories, from the current directory to the directory /raid/home/new/vivek/:
mv * /raid/home/new/vivek/
The asterisk is a wildcard character that represents any or all files in the current directory. To get confirmation when renaming or moving folders pass the -i too mv command to avoid overwriting files/folders:
mv -i file.doc

/Documents/
OR
mv -i ExcelFiles

/Documents/
Sample outputs:

Pass the -v to explain what is being done i.e. show whether file being moved or renamed and so on:
$ mv -v old new
$ mv -v file.doc /backups/
Pass the -n option to mv to not overwrite an existing file/folder:
$ mv -v -n file.doc /backups/
Sample session from above commands:

Getting help

Issue the following command at the cli to read man page of mv for all option:
man mv
OR
mv —help
Sample outputs:

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to rename files and folders in linux using the command line

One of the common operations that you will perform repeatedly is renaming files and folders. Most often you will rename files directly from the command line. Sometimes you might also have to do it from within a shell script. Well, you could do it with a file manager as well, but what fun is that?

In Linux, all folders are files as well, or it can be considered as a file in almost all file operations. This means that the commands that you use to rename files can also be used to rename folders.

The Linux command to rename files is mv or the move command. The move and rename are the almost the same operation in Linux and can be used interchangeably. The general syntax of the mv command is

Another command is rename which is much more flexible than the mv command. Most modern distros have the rename command installed, other wise you could install it. It is widely used to rename similarly named multiple files in one go. The general syntax of rename command is

I will use file names as examples in the rest of this post. You can easily replace the file name with a folder name to perform the same operation on the folder as well.

Rename Files and Folders in the Current Folder

Most times you will be rename the file within the same folder. In this case the mv command takes two arguments: the source file name and the destination name. Assuming that the file world.txt is in the current working directory, you can rename it to earth.txt.

$ mv world.txt earth.txt

The new file name is earth.txt and it will be in the same folder as where the file world.txt was and will have the same file contents and metadata such as permissions. Using the rename command here to just rename one file is probably an overkill, but here is how you do it.

$ rename world earth earth.txt

Rename Files and Folders to Another Folder

You can move the file to another folder as well as the change the name of the file using the same mv command. You just have to provide either the absolute or relative path of the destination.

$ mv world.txt /path/to/folder/earth.txt

If you provide a folder as destination, then the command will just move the file to the new folder without renaming it. The destination path just end with the path seperator which is forward slash (/).

$ mv world.txt /path/to/folder/

Files with Extensions

Even if you want to change just the file extension and not the entire file name, the process is the same. You specify the file name with the new extension as the extension is part of the file name.

$ mv image.jpeg image.jpg

The rename command is much more useful in this case. You can rename several different files with the same extension to another extension rather easily.

$ rename .jpeg .jpg image*.jpeg

The above command will rename the extension to jpg of all files that have file name that start with image and an extension jpeg.

File or Folder names with Spaces or Hypen (-)

If either the source or destination files have spaces in them, you will need to escape those names. You can do it in one of two ways: either by using the escape character which is the backward slash (\) or by using double quotes (“) around the file name.

$ mv «file name with space.txt» «new file space.txt»

The above example uses double quotes around the names. The double quotes will work even if there are no spaces in the file names. The example below shows how to use escape the names without the quotes.

$ mv file\ name\ with\ space.txt new\ file\ space.txt

Each back slash is followed by a space character which causes the command to match the space character literally rather than consider it as a argument separator. FWIW, You can use both the quotes as well as escape the name in the same command as shown below…

$ mv «file\ name\ with\ space.txt» «new\ file\ space.txt»

The same technique will work with file names that start with a hypen (-). The hypen is used by the command to specify command line options, so if the file name happen to start with a hypen then it will cause the command to consider it as an option.

$ mv -v «\-file\ name.txt» newfile.txt

Use Date as part of the File or Folder Name

Sometimes you would want to use the output of a command as part of the file name. The most common use case is when you want to append the current date or time to the file name when backing up files. If you are using a shell script to backup files and you want to know when the backup was made, adding the current date to the file name will help.

$ mv logfile.txt «logfile.txt.$(date)»

You use the dollar sign ($) to specify that what follows is a command. The command itself is enclosed with in the simple brackets as (…). In the above example the date command is executed and the result is appended to the file name.

Renaming Multiple Files

The rename command is meant for this use case. It allows you to easily rename files (or folders) based on a regular expression. Using the mv command to do a rename of multiple files is much more involved. You can do this by using a shell loop such as for or while and using mv repeatedly for each file.

The rename command takes regular expressions to match existing file names and you can replace a part of the file name that are common across all matching file names.

$ rename world hello *world*

For example, lets’ change the word world to hello in all the files that contain the word world. You can also change the extension easily as mentioned in an example above. The rename command is much more powerful than the simple examples shown here. You can use regular expression substitution syntax for more powerful substitutions. Consider reading the manual or info page for rename.

Источник

Читайте также:  Windows блокировать все соединения
Оцените статью