Pass a folder name and move only the empty files in that folder to another folder
#!/bin/bash
echo "Enter the folder name "
read Folder_Name
for i in $(ls $ Folder_Name)
do
for j in $(ls -s $ Folder_Name /$i)
do
if [ $j = 0 ]
then
mv $ Folder_Name /$i /home/Raghu/Empty_File_folder
fi
done
done
2. Pass a folder name and store the details about the files in that folder to another file with name size and date , that folder may have sub folder.
#!/bin/bash
echo "Enter the folder name :"
read Folder_Name /*to get the passing folder name*/
for i in $(ls $ Folder_Name) /* to list the files inside the folder*/
do
if [ -f $ Folder_Name /$i ] /* to check is a file or directory*/
then
echo -n $i >> file_word.txt /*to copy the file name in to another file*/
echo -n " $(wc -w $ Folder_Name /$i)" >> file_word.txt /*to copy number of words in that file to another file*/
echo -n "$(date)" >> file_word.txt
echo " " >> file_word.txt
else
echo " $fold/$i is a directory $(date) " >> file_word.txt
fi
done
Pass a folder name and find the files inside the folder which are having
cust* as file name (the folder may contain sub folders and each folder can
contain files as well as folders), then save that particular files path into
another file then by using that path's file check the completeness of a file ,
then move the valid files into processed_files folder else move into invalid_files
folder
#!/bin/bash
echo "Enter the folder :"
read folder
for i in $(find $folder -type f-name
"*cust*") /*to get the files
only which contain cust in it*/
do
echo "$i" >>cust_rec /*to print the file’s path into another file*/
done
for j in $(cat cust_rec) /*to get the paths*/
do
if [ $(sed –n ‘$p’$j | grep "EOF" | wc
-l) -ne 0 ]
/*this will check thatfile contains EOF in it
last line or not*/
then
mv $j proc_rec/$(basename$j)$(date
+"%Y%m%d%H%M%S")
/* above line to move a valid file into processed
folder with datetime*/
else
mv $j inv_rec/$(basename$j)$(date
+"%Y%m%d%H%M%S")
/*above line to move a invalid file into invalid
folder with datetime */
fi
done
cp /dev/null cust_rec
/*once thescript is ran have to truncate the
file’s path file*/
========================
#!/bin/sh
#
# Script positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one
integers"
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
========================
#!/bin/sh
# Script test if..elif...else
#
if [ $v -gt 0 ];
then
echo
"$v is positive"
elif [ $v -lt 0 ]
then
echo
"$v is negative"
elif [ $v -eq 0 ]
then
echo
"$v is zero"
else
echo
"Opps! $v is not number, give number"
fi
========================
#!/bin/sh
Productlist="Products are available"
for prod in $productlist
do
if [ "$prod"mobile[
"$prod"laptop" ]
then
echo "I like ${prod}s"
else
echo "I like ${prod}s"
fi
done
========================
#!/bin/sh
inc()
{ #
The increment is defined first so we can use it
echo $(($1 + $2)) #
We echo the result of the first parameter plus the second parameter
}
#
We check to see that all the command line arguments are present
if [ "$1" "" ] || [
"$2" = "" ] || [ "$3" = "" ]
then
echo USAGE:
echo " counter startvalue incrementvalue
endvalue"
else
count=$1 #
Rename are variables with clearer names
value=$2
end=$3
while [ $count -lt $end ] # Loop while count is
less than end
do
echo $count
count=$(inc $count $value) # Call
increment with count and value as parameters
done #
so that count is incremented by value
fi
========================
#!/bin/sh
inc()
{
local value=4
echo "value is $value within the
function\\n"
}
value=5
echo value is $value before the function
echo "\$1 is $1 before the function"
echo
echo -e $(inc $value)
echo
echo value is $value after the function
echo "\$1 is $1 after the function"
inc()
{
local value=4
echo "value is $value within the
function\\n"
}
========================
echo Enter a number to print all the prime
numbers before it
read num
flag=0
i=2
while [ $i -lt $num ]
do
j=2
while
[ $j -lt $i ]
do
if [
`expr $i % $j` -eq 0 ]
then
flag=1
break
fi
j=`expr $j + 1`
done
if [
$flag -eq 0 ]
then
echo $i
fi
flag=0
i=`expr $i + 1`
done
========================
commands
pwd (to print current working directory)
dir or ls (to print the files and directories in
current directory)
ls -f
ls -a
ls -i
ls -l
ls -afi
clear or ctrl+l
manual pages
tough- to create file
who- who are all in the terminal
cal- calendar
date- display the date
cat- filename content in the particular file
tac- filename content in the particular file in
reverse order
uname
vi- text editor
whoami- login user
history
mkdir- make directory
cd- change directory
rm- remove
rmdir- remove directory
cd ..- this is used to change one step back
which- where command is strored
seq sequence number
head- filename first 10 lines of that file
tail- filename last 10 lines of that file
bc- calculator
grep- filter
cp- copy the file from one place to another
mv- move the file from one place to another
ln- create short cut
directory structure
bin- binary file,it is used by any user.
sbin-super binary file
etc-contain all configuration file
lost+found- some time system crash or reboot that
time file may damage this kind of file in this folder
media- it contain media file
proc- process related information
home -
display user
selinux- kind of firefall`
dev- contain all the devices
temp- it contain temporary file
lib- contain library file
opt- it contain download file
misc
========================
1).Script to count the number of words and characters in a file
clear
echo "enter the filename"
read file
echo "number of characters in a file $file is "
cat $file | wc -c
echo "number of words in a file $file is "
cat $file | wc -w
echo end
2).Finding number of files from given directory
clear
echo "enter any directory name "
read dr
echo "number of files from $dr "
ls $dr | wc -w
echo end
3). Script to print user information who currently login , current date
& time, number of users and calender.
clear
echo "Hello "; whoami
echo "Today is "; date
echo "Number of user login ; who | wc -l
echo "Calendar"
cal
echo end
======
#!/bin/bash
a=0
while [ "$a" -lt 10 ]
do
b="$a"
while [ "$b" -ge 0 ]
do
echo -n "$b"
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
Output is as follows -
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
===============
#!/bin/bash
echo enter file name
read file
$file
echo number of words
cat $file | wc -w
echo no of bytes
cat $file | wc -c
echo no of lines
cat $file | wc -l
echo no of characters
cat $file | wc -m
echo length
cat $file | wc -L
This is script file is for finding
>number of words in file
>number of bytes its consumed in a file
>total no of lines are there in file
>total no of character in a file
>length of longest line in a file
by dynamically passing name of the file . so by running this script you can find out the information
about the file.
=============
vieditor or vim editor
This isthe basic editor to insert or change any lines in our file.
To insert
i or esc+ i
Tosave file
esc + :+ w
Toquit
esc + :+ q
Tosave and quit
esc + :+ wq
Tosearch word
esc +/word
variable
while declaring num1=10 only
to get the value $num1.
echo
echo isthe command used to print values or string
expr
usedfor calculation with operators
expr 3+ 5
Basicscripts
Thescripts first line have to start with #!/bin/bash
To tellthe os that it is providing name and location of the shell.
Example1:
Filename as morning
vi morning
then
i
then
#!/bin/bash
clear
echo“Good morning”
then
esc +:wq
to runthe script
morning
It willshow error message as “permission denied”
To grant the permission
chmod 755 morning
then
morning
Without permission we can run script using
Bash morning
Thescript will run and it will print
Goodmorning
Example2:
Vi addition
#!/bin/bash
echo“Welcome”
num1=10
num2=20
let num3=$num1+$num2
echo“the answer is $num3”
addition
Example3:
Using if,
#!/bin/bash
echo“enter a number :”
read num
if[ “$num” –eq 10 ]
then
echo“given number is 10”
fi
Alias
Today function {
Date+ “%A, %B %-d, %Y”
}
If we just give today that print the date in that format
Wednesday,September 14,2016
=======
1).Script to count the number of words and
characters in a file
clear
echo "enter the filename"
read file
echo "number of characters in a file $file
is "
cat $file | wc -c
echo "number of words in a file $file is
"
cat $file | wc -w
echo end
2).Finding number of files from given directory
clear
echo "enter any directory name "
read dr
echo "number of files from $dr "
ls $dr | wc -w
echo end
3). Script to print user information who
currently login , current date & time, number of users and calender.
clear
echo "Hello "; who am i
echo "Today is "; date
echo "Number of user login ; who | wc -l
echo "Calendar"
cal
echo end
==========
========================
========================
========================