Advertisement
dzocesrce

[OS] indexYZ.txt files

Jan 21st, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | None | 0 0
  1. #!/bin/bash
  2. #if the number of arguments is not 3
  3. if [ $# -ne 3 ]
  4. then
  5.     echo "USAGE: `basename $0` number1 number2 destination_folder"
  6.     exit 1
  7. fi
  8. #if the directory exists remove it, then create it again
  9. if [ -d $3 ]
  10. then
  11.     rm $3
  12. fi
  13. mkdir $3
  14. TO=$3
  15. number1=$1
  16. number2=$2
  17. #get all txt files from the home directory that have been modified between the last 10 and 14 days.
  18. txt_files=`find ./home -name "\.txt$" -type f -mtime +${number1} -mtime -${number2}`
  19. number_of_txt_files=`find ./home -name "\.txt$" -type f -mtime +${number1} -mtime -${number2} | wc -l`
  20. touch temp.txt
  21. i=0
  22. while [ i -lt $number_of_txt_files ]
  23. do
  24.     #so we dump the files that are below or equal to 150B size and stick to more superior ones
  25.     filesize=`ls -l ./home | grep ${text_files[${i}]} | awk 'print $4'`
  26.     if [ filesize -gt 150 ]
  27.     then
  28.         echo "${txt_files[${i}]}" >> temp.txt
  29.     fi
  30.     i=`expr $i + 1`
  31. done
  32. for row in $(cat temp.txt)
  33. do
  34.     #apparently this is how we count how many times a ceratin word appears in a file
  35.     echo_count=`grep -o 'echo' $row | wc -l`
  36.     #if there ain't a file with indexY.txt name where Y is the echo_count, make one and continue with another file
  37.     if [ ! -f $TO/index${echo_count} ]
  38.     then
  39.         cp ./home/$row $TO/index${echo_count}.txt
  40.         continue;
  41.     fi
  42.     #if there exists a file with that name, we add another digit character that kinda counts in a way how many files have the exact number of echos in em
  43.     Zcount=1
  44.     flag=0
  45.     while [ flag -eq 0 ]
  46.     do
  47.         #if a file like this ain't existing, create it finally and get out, continue with another file
  48.         if [ ! -f $TO/index${echo_count}${Zcount} ]
  49.         then
  50.             cp ./home/$row $TO/index${echo_count}${Zcount}.txt
  51.             break;
  52.         else
  53.             #if a file like that exists try again by incrementing Z
  54.             Zcount=`expr $Zcount + 1`
  55.         fi
  56.     done
  57. done
  58. #let us see what we've got...
  59. echo `ls -l | $TO | awk 'print $9'`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement