user1875841 user1875841 - 2 months ago 17
Linux Question

How to prefix / rename all subfolders and their file contents in Ubuntu/Debian Linux

I'm stuck looking for a one-liner to add a prefix to all subfolder names and file names in a directory

eg "AAA" in the examples below

/folder/AAAfile.txt

/folder/AAAread/AAAdoc.txt

/folder/AAAread/AAAfinished/AAAread.txt

I've tried using xargs and find, but can't get them to go recursively through the subdirectories and their contents. Any suggestions?
James

Answer

You could use something like that

find . -mindepth 1 | sort -r | xargs -l -I {} bash -c 'mv $1 ${1%/*}/AAA${1##*/}' _ {}

Tested with your folder structure, executed from the root (same as AAAfile.txt).

Comments