Help with a shell script? List files, delete them and log them

Hello, i'm trying to solve this script.
List, one at a time, all files larger than 100K in the /home/username directory tree. Give the
user the option to delete or compress the file, then proceed to show the next one. Write to a
logfile the names of all deleted files and the deletion times.
I made this pseudocodebut i don't think i'm getting anywhere near of what my script is supposed to do, can anyone help me solve this?
Pseudocode:

#!/bin/bash
  for $filename in (~)
  skip if size(~) < 100K
  display $filename
  echo =  "Delete or compress?"
  if response == "delete"
    rm  $nombre
     $nombre + time >> log.txt
  else if response == "compress"
   zip $nombre.zip $nombre

Hello jose2802,

Welcome to forum, kindly use code tags while posting codes/commands in your post. Here is the script which may help you in same. please let us know if you have any queries on same too.

#####MAIN script ######
cat check_files.ksh
BA=`find -type f | awk '{gsub(/\.\//,X,$0);print $0}'`

for i in $BA[@]
do
	echo $i
	./check_files1.ksh $i
done

###### Other second script called in first script #####
cat check_files1.ksh
echo "Do you want to delete or zi the file"
read choice;
FILENAME=$1;
echo $FILENAME is file

case "$choice" in
"delete") echo "$FILENAME" >> LOGS_files_deleting
	      rm "$FILENAME"
;;
"zip") echo "File is zipping now."
      gzip "$FILENAME"
;;
*) echo "wrong option:"
	exit
;;
esac

### Run it as follows ###
./check_files.ksh

Thanks,
R. Singh

1 Like

Is this a homework assignment?

You are using variables that haven't been defined, using variables incorrectly, and do not seem to want to allow a user to keep a large file.

Why are you writing pseudocode instead of actual bash code with comments or echo statements indicating what the code you still need to complete is supposed to do.

Thanks very much, i added the date the file was deleted too, but i have another question, what is this command for?

| awk '{gsub(/\.\//,X,$0);print $0}'` 

Also, if i'd wanted to list the files from a directory other than /home/username what should i do?
PS: Thanks for the time

It substitutes every instance of ./ with X, before displaying.

You can try this script. Bit of a different approach but you may find it useful.

#!/bin/bash
###############################
#
# Script Name: largefiles.sh
#
###############################
clear
############ VARS #############
SCRIPT=${0##*/}
RUNUSER=$(whoami)
SIZE=102400  #100k
TARGETDIR=/home/${RUNUSER}
LOG=/tmp/${SCRIPT%%.sh}_${RUNUSER}.log
FILES=($(find ${TARGETDIR} -type f -size +${SIZE}c))
 
############ MAIN #############
echo "Showing all files larger than ${SIZE} bytes in ${TARGETDIR} sorted by size."
find ${TARGETDIR} -type f -size +${SIZE}c -ls | sort +6 -nr
echo -e "\n\n____File menu____"
while [[ ${#FILES[@]} -gt 0 ]]; do
        PS3="Please choose a file:"
        select file in ${FILES[@]}
                do
                        [[ -z ${file} ]] && {
                                echo "Invalid choice!"
                        } || {
                break
                        }
        done    
        
PS3="Select an option:"
echo -e "\nYou have selected the below file. Do you want to REMOVE or ZIP the file?"
ls -ltr ${file}
 
select option in ZIP REMOVE BACK
do
        [[ -z ${option} ]] && {
                echo "Invalid choice!"
        } || {
                break
        }
done
 
case ${option} in
        "ZIP" )
        echo "You chose ZIP. Zipping file now..."
        gzip ${file}
        echo "$(date "+%F %T") ${file} compressed."
                ;;
                
        "REMOVE" )
        echo "You chose REMOVE. Removing file now..."
        rm ${file}
        echo "$(date "+%F %T") ${file} removed." | tee -a ${LOG}
                ;;
                
        "BACK" )
        continue
                ;;
esac
 
FILES=($(find ${TARGETDIR} -type f -size +${SIZE}c))
 
done

No, it's not homework, i'm trying to solve this pool of scripts to try to learn to code in bash, but i'm just starting to, so i'm looking for some help to begin with since i'm not that experienced coding in bash. :slight_smile:

@pilnet101, please review your posting!
What is your second find for?
Then, sort +6 is deprecated, get used to sort -k7 !
Last but not least, you better replace

[[ -z ${file} ]] && {
  echo "Invalid choice!"
} || {
  break
}

by

if [[ -z "$file" ]]; then
  echo "Invalid choice!"
else
  break
fi

because an non-zero exit state in the && branch will continue the || branch,
while a non-zero exit state in the then branch will never go to the else branch.