Showing all directories and files, also readable writable and executable - unix

#!/bin/sh
LOCATION=$1
if [ "$#" -ne "1" ]
then
echo "Usage: test1 <directory_name>"
else
echo "Number of directories:` find -type d | wc -l `"
echo "Number of files: ` find -type f | wc -l`"
echo "Number of readable:` find -perm -g+r | wc -l`"
echo "Number of writable:` find -perm -g+w | wc -l`"
echo "Number of executable:` find -perm -g+x | wc -l`"
fi
is there anything wrong with this code? I want it to show all the directories, files readable, writable, and executable when i type in ./test1

You need to add LOCATION in find command. If you didn't give any value, then it will start to find files from current directory and recursively.
Your script should be like,
#!/bin/bash
LOCATION=$1
if [ "$#" -ne "1" ]
then
echo "Usage: test1 <directory_name>"
else
echo "Number of directories: $(find $LOCATION -type d | wc -l)"
echo "Number of files: $(find $LOCATION -type f | wc -l)"
echo "Number of readable:$(find $LOCATION -perm -g+r | wc -l)"
echo "Number of writable: $(find $LOCATION -perm -g+w | wc -l)"
echo "Number of executable: $(find $LOCATION -perm -g+x | wc -l)"
fi

Related

Suffix subdirectory name with number of files underneath it

I have subdirectories a b c. For various obscure reasons, I would like to count all files recursively underneath these and only for maxdepth=1 mindepth=1 suffix this first layer of subdirectories with the file count down to the bottom of each subdirectory tree (no limit).
So if a and its subdirectories have 23 files, b...64 and c...82 I will end up with subdirectories renamed as
a_23
b_64
c_82
I have a routine to count recursively:
function count_all_files () {
echo "enter directory"
find "$1" -type f | wc -l
}
but am at a loss how to construct a find -exec operation to rename as I need.
Something like this pseudo code.
find . -type d -mindepth 1 -maxdepth 1 "*" -exec $(count_all_files {}) && [suffix dir name]
Grateful for thoughts. Needs to work with directories containing spaces too.
This seems to be working. I have amended it so it always makes a clean update eg if you add new files.
function label_subdirectories_number_files () {
for file in *_my_dir_count_* ; do rename 's/_my_dir_count_.*//g' "$file" ; done
find . -type d -mindepth 1 -maxdepth 1 -name '*' -exec bash -c 'cd {} \
&& number_of_files=$(find . -type f | wc -l) && directory=$(pwd) \
&& directory="${directory## }" && read -r number_of_files <<< "$number_of_files" \
&& new_directory="$directory""_my_dir_count_""$number_of_files" && \mv "$directory" "$new_directory" ' &> /dev/null 2>&1 \;
}
This variation does selected number of lower subdirectories too in case you want a quick eyeball test of lower level counts.
function label_subdirectories_number_files_many () {
echo "enter number of levels to scan"
for file in *_my_dir_count_* ; do rename 's/_my_dir_count_.*//g' "$file" &> /dev/null 2>&1 ; done
for zcount in $(seq 1 "$1") ; do
echo "level = $zcount out of $1 "
find . -type d -mindepth $zcount -maxdepth $zcount -name '*' -exec bash -c 'cd {} \
&& number_of_files=$(find . -type f | wc -l) && directory=$(pwd) \
&& directory="${directory## }" && read -r number_of_files <<< "$number_of_files" \
&& new_directory="$directory""_my_dir_count_""$number_of_files" && \mv "$directory" "$new_directory" ' &> /dev/null 2>&1 \;
done
}

how to improve the below space issue coding

while i ran the below script it returns nothing... expect the prompt as the output..
#!/bin/sh
cd /dla
op=`df -k /dla/ |awk '{print $5}' |grep '%' |cut -d '%' -f1`
if[ $op -ge 80 ]
then
echo ' dla is more tha 80% - Purge started'
find /dla/ -name '20[0-9][0-9]*' -type d -print |sort |cut -d '/' -f7 |grep 20 | sort | uniq -c |head -20 > /tmp/file_list.dat
for i in cat /tmp/file_list.dat |awk '{print $#}'
do
find /dla -name $i -type d -exec rm -rf [] \;
newop='df -k /dla/ |awk '{print $5}' |grep '%' |cut -d '%' -f1'
if [$newop -le 80]
then
echo 'dla is 80% -Purge stopped'
exit 0
fi
done
else
echo 'dla is less than 80% - No Purge Required'
fi

Unix Loop if Condition and exit comand

I am facing an issue, I have to delete files from some folders given in Path.lst,
The entire script is working fine but when some wrong path is given in Path.lst the script does exits out of the loop and perform no operation on the next paths.
But the last line
echo -e "\n ENDING SCRIPT SUCCESSFULLY ON `date` " >> $LOG_FILE
gets executed because exit 1 is not working in this part
if [ ! -d $path ]
then
echo -e "\nERROR :$path IS INVALID." >> $LOG_FILE
echo -e "\nENDING SCRIPT WITH ERRORS ON `date`" >> $LOG_FILE
exit 1
---------------------------------------------------------------------------------------
THE SCRIPT IS LIKE :
echo -e "\nSTARTING SCRIPT ON `date`">> $LOG_FILE
if [ $1 -gt 0 ]
then
DAYS_BFOR="$1"
else
echo -e "\nERROR :Please pass a single positive integer to the script" >>$LOG_FILE
echo -e "\nENDING SCRIPT WITH ERRORS ON `date` " >> $LOG_FILE
exit
fi
cat Path.lis | sed 's|^PATH[0-9]*=||g' | while read path
do
if [ ! -d $path ]
then
echo -e "\nERROR :$path IS INVALID." >> $LOG_FILE
echo -e "\n ENDING SCRIPT WITH ERRORS ON `date` " >> $LOG_FILE
exit 1
else
echo -e "\nFILES DELETED FROM THE "$path" DIRECTORY --" >> $LOG_FILE
find $path -type f -mtime +$DAYS_BFOR -printf "%TY-%Tm-%Td %kKB %p\n" | column -t | sed "s|"$path"||g" >> $LOG_FILE 2>&1
file_count=`find $path -type f -mtime +$DAYS_BFOR | wc -l`
if [ $file_count -ge 1 ]
then
find $path -type f -mtime +$DAYS_BFOR | xargs rm 2>>$LOG_FILE 2>&1
fi
fi
done
echo Exit Status : $?
echo -e "\n ENDING SCRIPT SUCCESSFULLY ON `date`" >> $LOG_FILE
Please help and explain the reason as well.
If you only want the "ENDING SCRIPT SUCCESSFULLY" message to appear if files were successfully deleted, not if an invalid path was given you could just move the last two echo lines up to the end of the else statement like this:
else
echo -e "\nFILES DELETED FROM THE "$path" DIRECTORY --" >> $LOG_FILE
find $path -type f -mtime +$DAYS_BFOR -printf "%TY-%Tm-%Td %kKB %p\n" | column -t | sed "s|"$path"||g" >> $LOG_FILE 2>&1
file_count=`find $path -type f -mtime +$DAYS_BFOR | wc -l`
if [ $file_count -ge 1 ]
then
find $path -type f -mtime +$DAYS_BFOR | xargs rm 2>>$LOG_FILE 2>&1
fi
echo Exit Status : $?
echo -e "\n--------------------------- ENDING SCRIPT SUCCESSFULLY ON `date` ----------------------------------" >> $LOG_FILE
fi
done
If you want to just skip to the next item in the Path.lis file then just remove the exit statement from the first loop. That way it will continue to execute the script until all the lines in the file have been read, and just show an error if the current file is not a valid path.

Case statement is not showing any output

I am trying to execute below script,
data=$(printf "%s " $(find output.log -type f -exec grep 'ACTIVE\| NOT ACTIVE' {} \; | awk '{print $1}'))
status=`find output.log -type f -exec grep 'ACTIVE\| NOT ACTIVE' {} \; | awk '{print $3}'`
case "$data" in
("Instance1")
echo "Status for Instance1 is : $status";
;;
("Instance2")
echo "Status for Instance2 is : $status";
;;
"") echo "empty things"
;;
esac
but it is not showing any output.. maybe i am missing something in my script (may be lot)
The logfile I am using in above script is ie. output.log,
INSTANCE_NAME OPEN_STATUS STATUS
---------------- ------------ -----------------
Instance1 OPEN ACTIVE
Instance2 OPEN NOT ACTIVE
Can anyone tell me what is wrong with above script ?
Thanks,
You don't loop over your entries, thus you never have a data "Instance1" but "Instance1 Instance2".
You probably want something as the following:
items=$(grep -0 'ACTIVE\| NOT ACTIVE' output.log | tr -s ' ' )
IFS=$'\r\n'
for it in $items; do
data=$(echo $it | cut -d ' ' -f1 )
status=$(echo $it | cut -d ' ' -f3- )
case "$data" in
("Instance1")
echo "Status for Instance1 is : $status";
;;
("Instance2")
echo "Status for Instance2 is : $status";
;;
"") echo "empty things"
;;
esac;
done

Remove duplicate jars in a directory

I have a script to remove lower version jars files in a directory.
#!/bin/bash
#Script to remove lower version jar files.
for PREFIX in `ls *.jar|sed 's/-[0-9\.\0-9\.a-zA-Z]*\.jar//g'|uniq -d`; do
for FILE in `ls -r ${PREFIX}*|sed '1d'`; do
echo " $FILE"
rm $FILE
done
done
It has a bug.
I have below list of Duplicate jar files in a directory.
xyz-1.1.jar
xyz-1.1.1.jar
abc-1.6.jar
abc-1.3.jar
abc-xyz-pqr-1.9.6.jar
abc-xyz-pqr-1.9.2.jar
xyz-tom.jar
xyz-tom-20120423.jar
xyz-tom-20120410.jar
abc-toolkit-1.6-runtime-5.2.0.jar
abc-toolkit-1.6-runtime-5.0.0.jar
The bug is with xyz pattern jar files.
BUG:
Script is removing xyz-1.1.1.jar file instead of xyz-1.1.jar
Script is removing xyz-tom-20120423.jar and xyz-tom-20120410.jar files.
#!/bin/bash
if [ $# == 0 ]; then
dir='.'
elif [ $# == 1 ]; then
dir=$1
else
echo "Usage: $0 [dir]";
exit 1;
fi
for lib in `find $dir -name '*.jar'`; do
for class in `unzip -l $lib | egrep -o '[^ ]*.class$'`; do
class=`echo $class | sed s/\\\\.class// | sed s/[-.\\/$]/_/g`
existing=$( eval "echo \$CLS_${class}" )
if [ -n "$existing" ]; then echo "$lib $existing"; fi
eval CLS_${class}="\"${lib} ${existing}\""
done
done | sort | uniq -c | sort -nr
I find this code here

Resources