user1875841 user1875841 - 1 year ago 160
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




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

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download