what I'im trying to do is make a simple shell script /bin/bash to count files in a given directory and then add the file count in the directory name.

I can imagine how to count files (with find) and even to store file count in a variable, but I cannot figure it out how to move into dir, then goes back, get the dir path and rename..

I usually use Platypus to convert a shell script into a OSX app, enabling drag&drop.

So the usage should be:

drag a folder into this app the app count files into this folder the app append file count into folder name

Any help will be appreciate

Thanks Matt

  • Can You please post one example so it will be helpful Jun 19, 2021 at 9:19
  • So if you add three files to a directory xx already containing two files, it gets progressively named like xx_3_4_5 ? Jun 19, 2021 at 17:03
  • As a side suggestion, do not rename the original directory. Create a symlink so that the symlink has the file count in it's name, and points to the original directory. So, ./mydir_11 -> ./mydir, and thus, you can still get to the original directory if something goes wrong
    – C. M.
    Jun 19, 2021 at 18:09

2 Answers 2


You can append the file count to the directory name as follows, e.g. for a directory called mydir:

mv ../mydir ../mydir$(ls | wc -l)

ls | wc -l gives you the file count in the current directory.

Or for an absolute path, the script could look something like this:

mv $path $path$(ls $path | wc -l)
  • Thanks Hector! Platypus pass the dropped folder as $1 to the script, so it will be mv ../$1 ../$1$(ls | wc -l) correct?
    – Matt
    Jun 19, 2021 at 8:48
  • That should work, let me know if it works out.
    – soma
    Jun 19, 2021 at 8:53
  • it does not :-( I tried this: #!/bin/bash echo "Append File Count" echo "v.1.0" mv $1 $1$(ls | wc -l) it gives me: ​Append File Count v.1.0 usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory
    – Matt
    Jun 19, 2021 at 9:03
  • I have no experience with Platypus, but you should make sure the script receives the correct paths. Not sure if Platypus passes an absolute path, but you could use absolute paths instead.
    – soma
    Jun 19, 2021 at 9:08
  • Thanks Hector, I did an echo $1 to see what platypus pass as an arg, it passes /Users/xxxxxxxx/Desktop/test is this an absolute path? how can I change it as an absolute?
    – Matt
    Jun 19, 2021 at 9:16

this work :-) thank you

echo "Folder Name Append File Count"
echo "v.1.0"

cd "$1"

filecount=$(find . -type f -not -path "*/\.*" | wc -l)

mv "$1" "$1$filecount"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .