Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have this folder that contains many folders, each containing many files with the name structure .XYZ.zip.

I'd like to rename them (using bash) to XYZ.zip (i.e. un-hide them).

I've seen a question attempting to do a similar thing,

alias deannoy='for annoyingbak in *.bak;do mv "$annoyingbak" ."$annoyingbak";done'>> ~/.bashrc && . .bashrc

but I've not been able to manage to change so it's done recursively for all folders down from the current folder.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

The for ... in *.bak command searches only the current directory.

You want instead to use the find command, which searches recursively. This command will locate all zip files starting with a dot at any depth in the current directory (.).

find . -iname '.*.zip'

Removing the leading dot is a bit trickier though. The following seems to work (but may have edge cases, caveat emptor).

for f in $(find -iname '.*.zip'); do f2=$(echo $f | sed -re 's/(.*)\/\.(.*)/\1\/\2/'); echo $f $f2; done

This will print all the operations it would perform (echo $f $f2), if this list looks right change it to mv $f $f2 and it will do the renames.

share|improve this answer
    
this is my personal favorite (but i can't try it now, the computer on which the files reside is busy....): simplest [also good that it we can do a dry run] –  user2413 Oct 19 '12 at 13:26
add comment

There is a good answer on sister-site stackoverflow: It says:

#!/bin/bash
recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse /path

OR if you have bash 4.0

#!/bin/bash
shopt -s globstar
for file in /path/**
do
    echo $file
done

Say thank you over at stackexchange to ghostdog74 if this works for you. The askubuntu account will work there too.

share|improve this answer
add comment

You can use this command:

$ find foobar/ -type f -iname ".*" -exec rename -n 's/^(.+)\/\.(.+)$/$1\/$2/' '{}' \;

foobar/sub_dir/moresubdir/.foo bar.zip renamed as foobar/sub_dir/moresubdir/foo bar.zip
foobar/sub_dir/moresubdir/.one.zip renamed as foobar/sub_dir/moresubdir/one.zip
foobar/sub_dir/moresubdir/.two.zip renamed as foobar/sub_dir/moresubdir/two.zip
foobar/sub_dir/.one.zip renamed as foobar/sub_dir/one.zip
foobar/sub_dir/.two.zip renamed as foobar/sub_dir/two.zip
foobar/.foo bar.zip renamed as foobar/foo bar.zip
foobar/.one.zip renamed as foobar/one.zip
foobar/.two.zip renamed as foobar/two.zip

find will recursively search for all hidden files then pass them to rename. The -n param causes rename to dry-run the substitution rule to show you what those files would be renamed to. If you're happy with the results, remove the param so it renames the files for real

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.