coder007
coder007

Reputation: 97

rename files in a folder using find shell

i have a n files in a different folders like abc.mp3 acc.mp3 bbb.mp3 and i want to rename them 01-abc.mp3, 02-acc.mp3, 03-bbb.mp3... i tried this

#!/bin/bash

IFS='
' 
COUNT=1 
for file in ./uff/*;
do mv "$file" "${COUNT}-$file" let COUNT++ done

but i keep getting errors like for syntax error near 'do and sometimes for not found... Can someone provide single line solution to this using "find" from terminal. i'm looking for a solution using find only due to certain constraints... Thanks in advance

Upvotes: 0

Views: 103

Answers (4)

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

you can try this;

#!/bin/bash
COUNT=1 
for file in ./uff/*;
do 
path=$(dirname $file)
filename=$(basename $file)

   if [ $COUNT -lt 10  ]; then
      mv "$file" "$path"/0"${COUNT}-$filename";
   else
      mv "$file" "$path"/"${COUNT}-$filename";
   fi

COUNT=$(($COUNT+1));

done

Eg:

user@host:/tmp/test$ ls  uff/
abc.mp3  acc.mp3  bbb.mp3

user@host:/tmp/test$ ./test.sh

user@host:/tmp/test$ ls uff/
01-abc.mp3  02-acc.mp3  03-bbb.mp3

Upvotes: 1

eush77
eush77

Reputation: 4088

Ok, here's the version without loops:

paste -d'\n' <(printf "%s\n" *) <(printf "%s\n" * | nl -w1 -s-) | xargs -d'\n' -n2 mv -v

You can also use find if you want:

paste -d'\n' <(find -mindepth 1 -maxdepth 1 -printf "%f\n") <(find -mindepth 1 -maxdepth 1 -printf "%f\n" | nl -w1 -s-) | xargs -d'\n' -n2 mv -v

Replace mv with echo mv for the "dry run":

paste -d'\n' <(printf "%s\n" *) <(printf "%s\n" * | nl -w1 -s-) | xargs -d'\n' -n2 echo mv -v

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755054

I'd probably use:

#!/bin/bash

cd ./uff || exit 1

COUNT=1 
for file in *.mp3;
do
    mv "$file" $(printf "%.2d-%s" ${COUNT} "$file")
    ((COUNT++))
done

This avoids a number of issues and also includes a 2-digit number for the first 9 files (the next 90 get 2-digit numbers anyway, and after that you get 3-digit numbers, etc).

Upvotes: 2

Parthian Shot
Parthian Shot

Reputation: 1432

Here's a solution.

i=1
for f in $(find ./uff -mindepth 1 -maxdepth 1 -type f | sort)
do
  n=$i
  [ $i -lt 10 ] && n="0$i"
  echo "$f" "$n-$(basename "$f")"
  ((i++))
done

And here it is as a one-liner (but in real life if you ever tried anything remotely like what's below in a coding or ops interview you'd not only fail to get the job, you'd probably give the interviewer PTSD. They'd wake up in cold sweats thinking about how terrible your solution was).

i=1; for f in $(find ./uff -mindepth 1 -maxdepth 1 -type f | sort); do   n=$i;   [ $i -lt 10 ] && n="0$i";   echo "$f" "$n-$(basename "$f")" ;   ((i++)); done

Alternatively, you could just cd ./uff if you wanted the rename them in the same directory, and then use find . (along with the other find arguments) to clear everything up. I'm assuming you only want files moved, not directories. And I'm assuming you don't want to recursively rename files / directories.

Upvotes: -1

Related Questions