LukaszBielsko
LukaszBielsko

Reputation: 321

How to count moved files with bash script?

My bash script moves screenshot files from desktop into newly created folder. I would like it to echo the number of the files moved after it finishes.

I've tried with exit code, but it only shows an exit code for one command which is mv. Is there a way that I can see what is going under the hood of mv command, which in that case moves more than one file?

#!/bin/bash
date=$(date +"%d-%m-%y")
mkdir SCREENS/"$date"
mv Screenshot*.png SCREENS/"$date"
#echo $? - it gives only one exit code

Upvotes: 2

Views: 997

Answers (2)

James Brown
James Brown

Reputation: 37414

Use the for, look:

$ c=0 ; for f in *.png ; do mv "$f" destination/ && ((c++)) ; done ; echo $c
2

Upvotes: 2

Walter A
Walter A

Reputation: 20022

You are moving to a new directory, so you can count the files in the new directory.
A filename can have a newline, so a simple wc will fail (don't parse ls).
You can instruct find to write one line for each file and count those lines:

find SCREENS/"$date" -type f -exec echo x \; | wc -l

Upvotes: 3

Related Questions