Rename Files’ Suffixes Recursively And Find Folders

Find Folders

To find all folders in the current path, I wrote a simple shell script file. It ignores invisible folder which path looks like ./.XXX.

#! /usr/bin/bash

dirs=()
find . -type d -print0 >tmpfile
while IFS=  read -r -d $'\0'; do
    dirs+=("$REPLY")
done < tmpfile

#echo ${dirs[@]}
#echo ${#dirs[@]}

dirArray=()
len=${#dirs[*]}
for(( i=0;i<${len};i++ )); do
    str=${dirs[$i]}
    if [[ "${str:0:3}" = ./. ]]; then
        continue
    fi
    dirArray+=("${str}")
done

for(( i=0;i<${#dirArray[*]};i++)); do
    echo ${dirArray[$i]}
done

Rename Files’ Suffixes Recursively

The following example is to rename the files from .gz to .txt.

#! /usr/bin/bash
files=()
find . -name "*.gz" -print0 >tmpfile
while IFS=  read -r -d $'\0'; do
    files+=("$REPLY")
done < tmpfile

# echo ${files[@]} #print all
len=${#files[*]}
newFiles=()
for(( i=0;i<${len};i++ )); do
    newDir=$(echo ${files[$i]} |sed s/.gz/.txt/g)
    newFiles+=("$newDir")
done;

for(( i=0;i<${len};i++ )); do
    echo ${files[$i]}" ==> "${newFiles[$i]}
    cp "${files[$i]}" "${newFiles[$i]}"
done
Categories: Shell

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Computer Number Terms
: You can use the tool to get different expressions of color, disk space and number.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x