Rename files and folders in linux

How to rename and move files and directories on Linux

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.

mv old-filename new-filename

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.

mv /home/student1/lab-work.log /var/labs/student1/lab-work.log

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.

mv old-filename /new/path/new-filename

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.

mv student1.txt /var/students/class1-student1.txt

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.

mv source-file-1 source-file-2 target-path

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

mv student1.txt student2.txt /var/students

Moving Directories on Linux

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

mv source-directory target-directory

For example, to move a directory path /tmp/logs to ~/data/logs you would run the following command.

Читайте также:  Linux сменить владельца всех файлов

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.

mv /dir/1 /dir/2 /dir/3 /target/path

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.

mv -v student1.txt student2.txt

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.

mv -n student1.txt student2.txt

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.

mv -f /var/data/logs /tmp/data/logs

Источник

Rename Files & Folders Linux Command Line

Rename Files & Folders Linux Command Line

Now we shall use the rename command. If you don’t have rename installed you can install it as follows:

If you’ve been following this tutorial so far then you should have 4 “.txt” files in your folder which we will be working with.

    Lets run the following command to rename the files and replace the “.txt” extension with “.bak«:

file1.bak file2.bak file3.bak file4.bak
rename 'y/a-z/A-Z/' *.bak && rename 's/.BAK/.bak/' *.BAK
FILE1.bak FILE2.bak FILE3.bak FILE4.bak
file1.bak file2.bak file3.bak file4.bak

Renaming Folders

  • Renaming a single folder with mv: This will rename “dir1” to “dir2”.
for i in *; do mv "$i" "$i// />"; done
dir001 dir002 dir003 dir004

Conclusion

This was a quick look at how to rename files and folders in linux from the command line. While the mv command would do just fine, the rename command proves to be more efficient and versatile for renaming files and folders.

Источник

Linux fundamentals: How to copy, move, and rename files and directories

Two windows with a small pane open

Copying, moving, and renaming files and directories are standard tasks for sysadmins and end users. Depending on your Linux distribution, you can accomplish these operations in various ways.

The Bash shell is usually the most efficient tool for file management. This article assumes you already have a basic understanding of how to open a Linux terminal and enter commands. (See How to access the Linux terminal if you want a refresher.) Connect to your Linux terminal with your regular user account, and get ready to reorganize.

Change to your home directory and create a new directory named mydir for the exercises. The command to create a new directory is mkdir :

Move files and directories

The mv command moves both directories and files. Check its options and parameters from the —help results below:

$ mv --help Usage: mv [OPTION]. [-T] SOURCE DEST or: mv [OPTION]. SOURCE. DIRECTORY or: mv [OPTION]. -t DIRECTORY SOURCE. Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument -f, --force do not prompt before overwriting -i, --interactive prompt before overwrite -n, --no-clobber do not overwrite an existing file If you specify more than one of -i, -f, -n, only the final one takes effect. --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update move only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done -Z, --context set SELinux security context of destination file to default type --help display this help and exit --version output version information and exit The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values: none, off never make backups (even if --backup is given) numbered, t make numbered backups existing, nil numbered if numbered backups exist, simple otherwise simple, never always make simple backups

Rename files and directories

You also use the mv command to rename directories and files if the destination doesn’t already exist. If the destination exists, then they’re moved using the syntax mv . Here is an example of moving existing files to existing directories:

$ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 17:52 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 17:52 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 $ mv file1 dir1/ $ mv file2 dir2/ $ mv file3 dir3/ $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir1 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir2 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 [mydir]$ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir1 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir2 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 ./dir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 ./dir2: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 ./dir3: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:35 subdir1 ./dir4/subdir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:35 file4

[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]

And you can use the mv command to move directories into other directories:

$ ls -1 dir1 dir2 dir3 dir4 $ mv dir1/ dir2/ $ mv dir2/ dir3/ $ ls -1 dir3 dir4

The dir1 and dir2 directories still exist; you’ve just moved them. See what it looks like for yourself:

$ ls -R dir3 dir4 ./dir3: dir2 file3 ./dir3/dir2: dir1 file2 ./dir3/dir2/dir1: file1 ./dir4: subdir1 ./dir4/subdir1: file4

Copy files and directories

The cp command copies both files and directories. This command has many options, but the basic syntax is simple. Run cp to copy from one place (source) to another (destination). Consider the following example:

$ ls -1 dir3 dir4 $ cp dir4/subdir1/file4 . $ ls -1 dir3 dir4 file4 $ ls -R dir4/ dir4/: subdir1 dir4/subdir1: file4

To copy an entire directory with its contents, use the -R option, as seen below:

$ cp -R dir3/ dir4/ $ ls -R dir3 dir4 file4 ./dir3: total 0 dir2 file3 ./dir3/dir2: dir1 file2 ./dir3/dir2/dir1: file1 ./dir4: dir3 subdir1 ./dir4/dir3: dir2 file3 ./dir4/dir3/dir2: file2 ./dir4/dir3/dir2/dir1: file1 ./dir4/subdir1: file4

Great Linux resources

When you copy empty directories into other directories, there’s no need for the -R parameter.

More to explore

For each command I’ve demonstrated, there are many more options I’ve left out for the sake of brevity.

As a sysadmin, you must know how to copy, move, and rename files and directories. These file-management commands are the basis of much of what you do on the system and are the building blocks for effective Linux administration. I hope this article aids you in understanding this topic, helps your Linux certification path, and adds to your general sysadmin knowledge.

Источник

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