A
A
AmdInDrive2020-11-15 13:41:56
linux
AmdInDrive, 2020-11-15 13:41:56

How to bulk rename files in a directory?

Hello everyone! I again have one of the easiest questions over which I puzzle all day. I have a task here to make a script that will rename files in a specific directory:
File1
...
File5
For example:
File1_renamed
...
File5_renamed
I know that this can be done through suffix , but the catch is that as an example I was given a simple rename files with the addition of this renamed, and you need to do just that. I thought about listing the files and renaming them like this, but I have no idea how to do it so that the changes affect the names of the files themselves in the directory, and not just as output to the console.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Victor Taran, 2020-11-15
@AmdInDrive

find /var/www/ -type f -exec mv {} {}_renamed \;
find-recursive search from the given directory and below
/var/www- where to look, if in the current directory, then you can replace it with .
-type f- only files
-exec execute with found
mvmove
{}what is found in this case filename
\;- just end the command.
You can check this way, instead of renaming, you will just get a print of the commands themselves. Mark as solved, thanks cap.
find /var/www/ -type f -exec echo {} {}_renamed \;
5fb18a8d63453365594996.png

R
Rsa97, 2020-11-15
@Rsa97

rename -e 's/(.*)/$1_renamed/' *

X
xotkot, 2020-11-15
@xotkot

there are many ways to rename, here are google examples Renaming files with find, sed and xargs
specifically in your case you can do this

ls -1 |awk '{print $0,$0"_renamed"}' | xargs -n2 mv

with spaces in the title
ls -1 |awk '{print "\""$0"\" \""$0"_renamed\""}' | xargs -n2 mv

K
ky0, 2020-11-15
@ky0

Search keywords are "find with the -exec key", "regex", "mv".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question