0

I have a total of 31 directories. Every directory has a random name.

❯ ls -al total 32 drwxr-xr-x 34 shinokada staff 1088 Dec 28 17:16 . drwxr-xr-x 32 shinokada staff 1024 Dec 28 17:12 .. -rw-r--r--@ 1 shinokada staff 14340 Dec 28 17:16 .DS_Store drwxr-xr-x 5 shinokada staff 160 Dec 28 17:10 05618066 drwxr-xr-x 5 shinokada staff 160 Dec 28 17:08 0fef2d20 drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 11ff096d drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 1651ff1f drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 2123b256 ... and more 

Now I'd like to rename them sample1 sample2 sample3 ...sample31.

How can I do it on a terminal(bash/zsh)?

Is there a quick way to do it rather than rename one by one?

2
  • You can't be running Bash and Zsh at the same time. The answers will probably look different for each. Commented Dec 28, 2020 at 8:46
  • MacOS does not seem to ship a rename tool; there are several incompatible ones, so it's unclear which one you refer to, but if you have the Perl-based one, it can probably do what you are asking in a slick one-liner. Commented Dec 28, 2020 at 8:47

1 Answer 1

2

This should do:

num=1; for dir in */ ; do mv "${dir}" "sample$num" ; ((num++)); done 

I assume you don't want to rename the files, only directories inside your current working directory.

Sign up to request clarification or add additional context in comments.

2 Comments

I just needed to change num=0 to num=1. Thanks.
Sorry, I missed that you are starting from sample1. Thanks for pointing it out (I edited it in the answer).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.