Looping through all files in a directory [duplicate] Looping through all files in a directory [duplicate] shell shell

Looping through all files in a directory [duplicate]


For files and directories, not recursive

for filename in *; do echo "put ${filename}"; done

For files only (excludes folders), not recursive

for file in *; do     if [ -f "$file" ]; then         echo "$file"     fi done

For a recursive solution, see Bennet Yee's answer.


Recursively (including files in subdirectories)

find YOUR_DIR -type f -exec echo "put {}" \;

Non-recursively (only files in that directory)

find YOUR_DIR -maxdepth 1 -type f -exec echo "put {}" \;

Use * instead of YOUR_DIR to search the current directory


For all folders and files in the current directory

for file in *; do    echo "put $file"done

Or, if you want to include subdirectories and files only:

find . -type f -exec echo put {} \;

If you want to include the folders themselves, take out the -type f part.