How to rename multiple files at the same time

I have many files, directories and subdirectories in my filesystem.
For example:

/path/to/file/test-poster.jpg
/anotherpath/my-poster.jpg
/tuxisthebest/ohyes/path/exm/bold-poster.jpg

I want to switch all filenames from *-poster.jpg

to folder.jpg


I have tried with sed

and awk

without success.
little help?

+3


source to share


3 answers


You can do it with find

:

find -name "*poster.jpg" -exec sh -c 'mv "$0" "${0%/*}/folder.jpg"' '{}' \;

      

Explanation

Here, for each matched filename, is executed:

sh -c 'mv "$0" "${0%/*}/folder.jpg"' '{}'

      

Where '{}'

is the filename passed as the command_string argument:



mv "$0" "${0%/*}/folder.jpg"

      

So, in the end, $0

will have a filename.

Finally, it ${0%/*}/folder.jpg

expands to the path of the old filename and adds /folder.jpg

.

Example

Note. I replace mv

withecho

$ find -name "*poster.jpg" -exec sh -c 'echo "$0" "${0%/*}/folder.jpg"' '{}' \;
./anotherpath/my-poster.jpg ./anotherpath/folder.jpg
./path/to/file/test-poster.jpg ./path/to/file/folder.jpg
./tuxisthebest/ohyes/path/exm/bold-poster.jpg ./tuxisthebest/ohyes/path/exm/folder.jpg

      

+5


source


Try this script, it should rename all files as needed.

for i in $(find . -name "*-poster.jpg") ; do folder=`echo $i | awk -F"-poster.jpg" {'print $1'}`; mv -iv $i $folder.folder.jpg; done

      



You can replace. to the directory where these files are put into the command find . -name "*-poster.jpg"

in the script. Let me know if it works ok for you.

+1


source


you can try this like

find  -name '*poster*' -type f -exec sh -c 'mv "{}"  "$(dirname "{}")"/folder.jpg' \;

      

find all files containing poster == find -name '*poster*' -type f

copy the path to the directory of the file and store it in a temporary variable and then bind "folder.jpg" to the path to the directory == -exec sh -c 'mv "{}" "$(dirname "{}")"/folder.jpg'

\;

0


source







All Articles