-2

I'm working in Linux, I have a folder that contains many sub-folders, in each sub-folder there is a file named Analyze.txt. I would like to move all these files (Analyze.txt) into one folder and change the name of the file Analyze.txt to the name of the sub-folder it originated from.

Thanks,

Raz

xbarvazx
  • 57
  • 3

1 Answers1

0

Create a bash script .sh and give it the right permission

chmod +x <file>.sh

Let see the code to write into the script. First of all move to the root directory:

cd <path-to-root>

Now you need to loop each sub-folder, rename the file in and then move it to destination directory:

for dir in `ls`; do
    if [[ -d $dir ]]; then
        cd $dir
        mv ./Analyze.txt ./$dir.txt
        mv ./$dir.txt <path-to-dest-directory>
        cd ..
    fi
done

With this code i supposed that the destination directory is not in the root of your tree.

Lorenzo Vincenzi
  • 1,153
  • 1
  • 9
  • 26
  • is it possible to manipulate $dir to include only a part of the folder name? something like if $dir = ^[0-9]*-[0-9]*-[0-9]*_[0-9h]*-[0-9m]*-[0-9s]*_(.*); then $name = $1 # (.*) ? – xbarvazx Nov 03 '15 at 10:10
  • Yes you can use regex. Check out this answer: [Bash regex if statement](http://stackoverflow.com/questions/18709962/bash-regex-if-statement) @xbarvazx – Lorenzo Vincenzi Nov 03 '15 at 10:22