Skip to content
Advertisement

Find all files with specific name and move it two levels up from its location

I want to find all files with specific name “stdout.1.0”, move it two/three levels up from its location. While moving it two/three levels up, I also want to rename it to “testjob.out”.

All “stdout.1.0” files are located six levels down from parent directory.

./dirXXXXXX/dirXXXXXX/dirXXXXXX/dirXXXXXX/dirXXXXXX/dirXXXXXX/stdout.1.0

I used:

find . -type f -name stdout.1.0 

and it outputs:

./dir100000/dir110000/dir111000/dir111100/dir111110/dir111111/stdout.1.0
./dir100000/dir110000/dir112000/dir111100/dir111110/dir111111/stdout.1.0
./dir100000/dir110000/dir113000/dir111100/dir111110/dir111111/stdout.1.0
./dir200000/dir210000/dir211000/dir211100/dir211110/dir211111/stdout.1.0
./dir200000/dir210000/dir212000/dir211100/dir211110/dir211111/stdout.1.0
./dir200000/dir210000/dir213000/dir211100/dir211110/dir211111/stdout.1.0
./dir300000/dir310000/dir311000/dir311100/dir311110/dir311111/stdout.1.0
./dir300000/dir310000/dir312000/dir311100/dir311110/dir311111/stdout.1.0
./dir300000/dir310000/dir313000/dir311100/dir311110/dir311111/stdout.1.0

.
.
./dirXXX000/dirXXX000/dirXXX000/dirXXX100/dirXXX110/dirXXX111/stdout.1.0

The directories above is just representative of where the file is, but there are multiple “stdout.1.0” files starting three levels down from parent directory.

Advertisement

Answer

Here is a method in plain bash using globstar shell option , without using the find:

#!/bin/bash

shopt -s globstar
for file in **/stdout.1.0; do
    echo mv "$file" "${file%/*/*/*}/testjob.out"
done

Drop the echo if output looks fine.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement