Skip to main content
Swapped * for @ as per @cas' guidance
Source Link
Aubs
  • 221
  • 1
  • 5

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in $"${DIR[*]DIR[@]"}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh
Scanning /home/aubs/test1/dir1
contains 2 files
Scanning /home/aubs/test1/dir2
contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in ${DIR[*]}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh
Scanning /home/aubs/test1/dir1
contains 2 files
Scanning /home/aubs/test1/dir2
contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in "${DIR[@]"}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh
Scanning /home/aubs/test1/dir1
contains 2 files
Scanning /home/aubs/test1/dir2
contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

put example in code block
Source Link
Aubs
  • 221
  • 1
  • 5

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in ${DIR[*]}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh Scanning /home/aubs/test1/dir1 contains 2 files Scanning /home/aubs/test1/dir2 contains 0 files

$ ./test.sh
Scanning /home/aubs/test1/dir1
contains 2 files
Scanning /home/aubs/test1/dir2
contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in ${DIR[*]}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh Scanning /home/aubs/test1/dir1 contains 2 files Scanning /home/aubs/test1/dir2 contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in ${DIR[*]}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh
Scanning /home/aubs/test1/dir1
contains 2 files
Scanning /home/aubs/test1/dir2
contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.

Source Link
Aubs
  • 221
  • 1
  • 5

You can create a for loop, to iterate through the directories you want stored in a variable

My structure looks like:

:~/test1$ find . | sort
.
./dir1
./dir1/dir1-a
./dir1/dir1-a/file1a-a
./dir1/dir1-a/file1a-b
./dir1/dir1-a/file1a-c
./dir1/dir1-a/file1a-d
./dir1/dir1-b
./dir1/dir1-b/file1b-a
./dir1/dir1-b/file1b-b
./dir1/dir1-b/file1b-c
./dir1/dir1-c
./dir1/dir1-c/file1c-a
./dir1/dir1-c/file1c-b
./dir1/dir1-d
./dir1/dir1-d/file1d-a
./dir1/file1-a
./dir1/file1-b
./dir2
./dir2/dir2-a
./dir2/dir2-b
./dir2/dir2-c
./test.sh

Note dir1 contains files and folders. You haven't said what you want to do with the sub-folders, or if any will exist.

#!/bin/bash

DIR=( "/home/aubs/test1/dir1" "/home/aubs/test1/dir2" )

for Item in ${DIR[*]}
do
    echo "Scanning $Item"
    COUNT_FILES=$(find "$Item" -maxdepth 1 -type f | wc -l)
    echo "contains $COUNT_FILES files"
done

Everything you want doing on each if the items in DIR should be put between the do and done, the directory name on each iteration is $Item.

$ ./test.sh Scanning /home/aubs/test1/dir1 contains 2 files Scanning /home/aubs/test1/dir2 contains 0 files

Using ${#dir_files[@]} gives 6 and 3 for my test, which is clearly taking directories into account too. My example uses find "$Item" -maxdepth 1 -type f which only counts the number of files in the directory, and does not look into sub-directories. It then pipes it to wc -l which counts how many lines there are.

You also haven't taken into account if you want to take ownership of the files in the folder either.