bash - Batch rename directories to match name of a file in subdirectory -
i have many directories within /home/environment named various time , date stamps. want rename of these directories instead of time date stamp each directory has name of .txt file contains part of shell script. e.g.
before
/home/environment/2014-03-12-09-49-26/xxxx-a.2a1-xxxx-0x_b.txt /home/environment/2014-03-12-09-50-34/xxxx-c.d57-xxxx-4x_e.txt after
/home/environment/xxxx-a.2a1-xxxx-0x_b/xxxx-a.2a1-xxxx-0x_b.txt /home/environment/xxxx-c.d57-xxxx-4x_e/xxxx-c.d57-xxxx-4x_e.txt
my answer question better way rename files based on multiple patterns describes general pattern can here:
job_select /path/to/directory| job_strategy | job_process where job_select responsible selecting objects of job, job_strategy prepares processing plan these objects , job_process executes plan. general discussion pattern, please see previous question, write down implementation here.
this assumes filenames not contain vertical bar | nor newline character.
the job_select function
# job_select path # produce list of files process job_select() { find "$1" -type f -name '*.txt' } the job_strategy function
# job_strategy # prepare plan renaming directories job_strategy() { sed -e 's@/\([^/]*\)/\([^/]*\)\.txt$@|\1|\2@' } the job_process function
# job_process # rename directories according plan job_process() { local radical oldname newname while ifs='|' read radical oldname newname; mv "$radical/$oldname" "$radical/$newname" done } note assumes files in distinct directories distinct.
Comments
Post a Comment