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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help with a shell script? List files, delete them and log them
# 1  
Old 10-18-2014
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:

Code:
#!/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


Last edited by Don Cragun; 10-18-2014 at 03:49 PM.. Reason: Add CODE tags.
# 2  
Old 10-18-2014
Quote:
Originally Posted by jose2802
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:

Code:
#!/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.

Code:
#####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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-18-2014
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.
# 4  
Old 10-18-2014
Thanks very much, i added the date the file was deleted too, but i have another question, what is this command for?

Code:
| 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

Last edited by rbatte1; 10-20-2014 at 09:48 AM.. Reason: Added CODE tags
# 5  
Old 10-18-2014
Quote:
Originally Posted by jose2802
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.

Last edited by Aia; 10-18-2014 at 08:55 PM.. Reason: grammar
# 6  
Old 10-18-2014
You can try this script. Bit of a different approach but you may find it useful.


Code:
#!/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

# 7  
Old 10-20-2014
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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to delete the ip address from files

Hello, I am new to shell scripting, need help, my requirement is to delete the ip address from serveral files, please suggest (2 Replies)
Discussion started by: manoj.solaris
2 Replies

2. Shell Programming and Scripting

Shell Script to delete files from 5 different servers

Hello, I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations. Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell... (2 Replies)
Discussion started by: Teja G
2 Replies

3. Shell Programming and Scripting

Shell Script to delete the protected files.

Hello, we have more than 100000 files in a directory which are write-protected regular file, these files are quite old and would like to delete them completely, Kindly let me know the command or peice of code to automate the process. The filenames are like below MPNT_... (6 Replies)
Discussion started by: Hadoop_Master
6 Replies

4. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

5. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

6. Shell Programming and Scripting

writting a shell script to delete damage files

hi, I have 100 files say File1.mp3, File2.mp3 .......file100.mp3 i found that File1.mp3 to File50.mp3 are damaged. I want to delete the damaged files from the directory using sed with regex how can i do this. thanks (5 Replies)
Discussion started by: pchangba
5 Replies

7. Shell Programming and Scripting

Shell script delete log files from folder & subfolders on space usage

Hi, I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes. I have to keep the latest 5 files at any time, my problem is that log files... (3 Replies)
Discussion started by: saha
3 Replies

8. Shell Programming and Scripting

shell script for delete old files

i want to delete all the files in my directory except the latest one. i need to do this from shell script. say i have a.txt - latest file b.txt, c.txt.. it should delete all the files except a.txt? (4 Replies)
Discussion started by: krishnarao
4 Replies

9. Shell Programming and Scripting

How to delete files in UNIX using shell script

Hi, I have the following task to perform using shell script. The user will provide a directory name along with a date. The script will delete all the files in the specified directory that was created earlier to that date. Also it should display the number of files that has been deleted. ... (7 Replies)
Discussion started by: theguy16
7 Replies

10. UNIX for Advanced & Expert Users

how to delete empty files in a shell script

I'm trying to figure out a way to delete empty files in a directory. I have a cron that runs and creates a flat file every 15 mins. However, most times at night the flat file will be empty. I'd like to run a script to delete empty files that end with *.dat Any suggestions? Rich (1 Reply)
Discussion started by: rpnuge
1 Replies
Login or Register to Ask a Question