Created
November 23, 2018 14:34
-
-
Save sham1/00dfa831730bbce3793f6a43b8627347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
output_file="${HOME}/found_empty.txt" | |
# For user convenience. If the user runs this using `sudo`, | |
# the file shall be within their own home directory, instead | |
# of the root's. | |
# | |
# We shall also create the file and make its ownership make sense | |
# for the user. | |
if [ -n "${SUDO_USER}" ]; then | |
user_home=$(eval echo ~"${SUDO_USER}") | |
output_file=$(printf "%s/found_empty.txt" "${user_home}") | |
# Create file if it doesn't exist already. | |
# No need to remove it if it already exists because | |
# we'd write over the contents anyhow. | |
[ ! -f "${output_file}" ] && touch "${output_file}" | |
# Little bit of non-standard stuff for this. | |
# We will set the owner of the resulting file to be | |
# the same as the home directory so they can read and | |
# such. | |
# | |
# For this being an example to be probably run on a regular | |
# Linux system using GNU Coreutils this isn't an issue, | |
# but for anyone trying to adapt this sample for something | |
# more portable, this is worth mentioning! | |
chown --reference="${user_home}" "${output_file}" | |
fi | |
# Main body of the program. We just go through | |
# the output of `find` line-by-line, with each line | |
# being stored in `${file}`, getting their sizes | |
# with `wc -c` which counts the bytes in a file. | |
# | |
# If the size is zero, it gets printed and thus redirected | |
# to the output file. | |
find /etc -type f | while read file; do | |
size=$(wc -c ${file} | cut -d' ' -f1) | |
[ "${size}" -eq 0 ] && printf "%s\n" "${file}" | |
done > "${output_file}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment