Unix SSH : Find files with different path on two servers - unix

I have server A & Server B . I want to file find command on both server but with different path .
Currently i created below code to do so :
dir1=( $DATA_DIR/sdfgv $DATA_DIR/1wefgg $DATA_DIR/3fdsevg );
dir2=( $DATA_DIR/asdf $DATA_DIR/sdfewfT $DATA_DIR/efergvfw );
timestamp=$(date +%Y%m%d%H%M%S);
report_name=Audit_Report_${timestamp}.txt
uname=xyz
server=( serv1 serv2);
for j in ${server[#]};
do {
if [ "$j" == "serv1 " ]
then
for i in ${dir1[#]};
do {
Size=`ssh -q $uname#$j "find $i -type f -mtime +1 -name '*.gz' -printf '%s + ' | dc -e0 -f- -ep"`;
echo " $j $i $Size "
}
done
else
for i in ${dir2[#]};
do {
Size=`ssh -q $uname#$j "find $i -type f -mtime +1 -name '*.gz' -exec du -k {} \; | awk '{ total += $ 1} END{print total/1024;}'"`;
echo " $j $i $Size "
}
done
fi
}
done
This code works pretty good but i want something that can be generic without if else on server name .
I dont want to use if else for server name .
Both server have different directory path to search for
Please come up with some suggestions .
Thank You !

Bash functions would help. Perhaps one for determining which directory to use for which server, and then one for executing the find command. Something like this (pseudo-code, untested):
getDirs() {
if [ $1 == 'server1' ]; then
echo $dir1
fi
if [ $1 == 'server2' ]; then
echo $dir2
fi
}
findFiles() {
local srv=$1
shift
local dir=$1
shift
local findArgs=$#
echo $(ssh -q $uname#$srv "find $dir $findArgs")
}
for srv in ${server[#]};
do
dirs=$(getDirs $srv)
for d in $dirs
do
findFiles $srv $d -type f -mtime +1 -name '*.gz'
done
done
Your sample does different things with the find results, so you will still need to add logic to handle that (could just be an if statement inside the loops maybe..)

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 calculate total size of all css,js and html pages separately uing shell script?

I have the following code in shell script
find -name "*.css" -exec -printf '%16f Size: %6s\n'
This gives me the file size of every css file. How do I modify this to get the added sum of all the file sizes ?
You could use awk:
find . -name "*.css" -type f -printf '%s\n' | awk '{ tot+=$0 } END { print tot }'
Or in pure bash:
total=0
while read -r s;
do
total=$(( total+s ))
done < <(find . -name "*.css" -type f -printf '%s\n')
echo $total
In 2 steps:
1) ll *css | tr -s " " > filename.txt
2) awk 'BEGIN {x=0} {x+=$5} END {print x}' filename.txt

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

How to list all directories/files that you have copy access permission to - Unix

Normally, the ls -la command shows the files and the copy access rights, owners and access group.
**
How can I list only list the directories/files that I have access to?
How can I copy these directories/files to a destination directory?
Try this:
a=`find $Your_Source_Path -iname "yourFolder_whose_Files_tohide" -prune -o -type f -print`
for i in $a
do
cp $Your_source $Your_Dest_Path
done
One more answer which will copy only those files whose having copy access permission. For this first navigate to the directory whose file wanted to copy. Also inside this mention your destination path first where you wanted to copy. Try the below:-
destination_Path="/Users/Home/Desktop/test"
b=~/Desktop/copyPermission.txt
if [ ! -f $b ]
then
touch $b
fi
a=`ls -l`
e="-----w--w-"
echo "\n$a" | sed '1d' > $b
g=`pwd`
while read line
do
d=`echo "$line" | awk '{print $1}'`
if [ $e != $d ]
then
r=`echo "$line" | awk '{print $9}'`
echo "Can have copy permission $g/$r"
{
cp $g/$r "$destination_Path" && echo "copied successfully"
} || {
echo "cannot copy due to some error"
}
#else
#r=`echo "$line" | awk '{print $9}'`
#echo "Cannot have copy permission $r"
fi
done <"$b"

How to find file extension using UNIX?

I need to find file extension for file to be processed using UNIX. The two file extension which i will be handling are '.dat' and '.csv'.
Please let me know how this can be done.
find . -name "*.dat" -o -name "*.csv"
Finds in the current directory and recursively down, all files that end in those two extensions.
So my stab at this.
filename=file.dat
extension=$(echo ${filename}|awk -F\. '{print $2}')
if [ ${extension} == "dat" ]; then
your code here
fi
Echo the variable ${filename} pipe that output to awk. With awk reset the field separator to a . then pick up field 2 (the print $2 part)
This is what you want ?
find . -name "*.dat"
find . -name "*.csv"
find /path -type f \( -name "*.dat" -o -name "*.csv" \) | while read -r file
do
echo "Do something with $file"
done
if you have the filename in a variable
filename = test.csv
then just use this to get the "csv" part:
echo ${filename##*.}
works for bash, try it in ksh
edit:
filename=test.csv
fileext=${filename##*.}
if [ fileext = "csv" ]; then
echo "file is csv, do something"
else
if [ fileext = "dat" ]; then
echo "file is dat, do something"
else
echo "mhh what now?"
fi
fi

Resources