Linux command rename directory

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.

Источник

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.

Читайте также:  Athr sys синий экран windows

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:

Источник

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

Источник

UNIX Command To Rename A File

H ow do I rename a file in UNIX using command line options?

This quick UNIX tutorial explains mv command to rename file, which is a Unix command that renames one or more files or directories. The original filename or directory name is no longer accessible. Write permission is required on all directories and files being modified. Use the mv command to:

Tutorial details
Difficulty level Easy
Root privileges No
Requirements mv command to move files on Unix/macOS/*BSD
Est. reading time 1 minute
  • Moves a file (i.e. gives it a different name).
  • Rename a file.

mv command syntax to rename a file on Unix

You need to use the mv command to rename a file as follows on Unix, macOS and *BSD family of operating systems:

Examples

Open the terminal (bash shell prompt) and type the following command to list file names:

In this example, rename a file called data.txt to letters.txt, enter:

File is renamed so the following command will display an error on screen:

The following would rename a file called foo to bar, while keeping it in the current directory:

  • 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

Rename a directory/folder Unix command

The following would rename a directory (folder) called dir1 to dir2, while keeping it in the current directory:

Move a file/folder to an existing directory

The following would move a file named resume.txt, without changing its name, to an existing subdirectory called /home/nixcraft/Documents/

How to rename a file on Unix and display verbose output

Pass the -v option to the mv command to show files after they are moved. For example:

Moving or renaming files with confirmation on Unix

We need to pass the -i option to the mv. It causes the mv command to write a prompt to screen before moving a file that would overwrite an existing file. If the response from the keyboard input begins with the character ‘y’ or ‘Y’, then move attempted. It is a safety feature:
mv -i old_name new_name
mv -i -v sales old_sales
We can also tell the mv command, not to overwrite an existing file by passing the -n :

You will see message as follows:

Here is a sample session from my FreeBSD Unix server:

A note about renaming multiple files

You need to use additional commands as mv cannot do that for you. Please see the following tutorials:

Summing up

The mv command may change or add an option as per Unix vendor and version. Hence read the man pages. Type the following man command to read mv command man page:
man mv

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

Источник

How to rename and move files and directories on Linux

Overview

In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.

Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.

As such, you will notice that commands used to perform actions on files are identical with directories.

Rename files on Linux

To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.

The mv command will take the source file specified and rename it to the target file.

To rename a file named student1 to student10, for example, you would run the following command.

Provided the file target is the same directory, all file attributes will remain, including permissions.

Moving a file on Linux

To move a file to another location we use the same method as renaming a file, except the file path should be different.

For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.

Moving and Renaming files on Linux

A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.

For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.

Moving Multiple files on Linux

The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.

For example, to move student1.txt and student2.txt to /var/students , you would run the following command.

Moving Directories on Linux

Moving directories work the same as moving files. We specify the source directory and give a target directory.

For example, to move a directory path /tmp/logs to

/data/logs you would run the following command.

Moving Multiple Directories on Linux

As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.

Verbose Output Flag

The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.

To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.

Do Not Overwrite Existing Files

To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.

In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.

Do Not Prompt to Confirm Overwrites

If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.

Источник

Читайте также:  Драйвер для принтера canon mg5140 windows 10
Оцените статью