How would I implement the search function to find a file or directory - linux

I am currently working on a script for bash that will ask the user for a starting location and a file name then search for the file. If possible I would also like to cat the file once found. Here is the script I have so far if someone would point out how I would include the function I would be very thankful. Also still a Unix beginner so please bear with me :)
#!/bin/bash
function press_enter
{
echo""
echo -n "Press Enter to continue"
read
clear
}
selction=
until [ "$selection" = "3" ]; do
echo -e "Where would you like to search
1- Root
2- Home
3- Exit
Enter your choice ---> \c"
read selection
case $selection in
1) cd / ; press_enter ;;
2) cd /home ; press_enter ;;
3) echo "Have a nice day!" ; exit ;;
esac
done

You can use find as one command, or use your selection inputs to drive find in your script.
find $where_selection -name $what_selection
# with cat:
find $where_selection -name $what_selection -exec cat {} \;
-exec cat {} replaces the {} with the current found file path from find.

Related

Linux shell script to search logs with user input

I am trying to write a script which searches a tomcat log from a keyword which the user inputs. I'm having issues getting the read command to work correctly.
#!/bin/bash
TOM1=/ap/mvr/servers/tomcat1/logs/
TOM2=/ap/mvr/servers/tomcat2/logs/
find $TOM1 $TOM2 -type f -name "mvr-gateway.log" | while read file
echo "What do you want to search for? " keyword
read keyword
do
LOGERRORS=$(grep -B1 -A1 $keyword $file)
if [[ ! -z $LOGERRORS ]]
then
echo $file
else echo No Data Found
fi
done
try this:
#!/bin/bash
TOM1=/ap/mvr/servers/tomcat1/logs/
TOM2=/ap/mvr/servers/tomcat2/logs/
read -p "What do you want to search for?" search_term
grep -B1 -A1 ${search_term} $(find $TOM1 $TOM2 -type f -name "mvr-gateway.log")
As mentioned before, ask first the term you are searching for.

Renaming directories at multiple levels using find from bash

I'm looping over the results of find, and I'm changing every one of those folders, so my problem is that when I encounter:
/aaaa/logs/ and after that: /aaaa/logs/bbb/logs, when I try to mv /aaaa/logs/bbb/logs /aaaa/log/bbb/log it can't find the folder because it has already been renamed. That is, the output from find may report that the name is /aaaa/logs/bbb/logs, when the script previously moved output to /aaaa/log/bbb/.
Simple code:
#!/bin/bash
script_log="/myPath"
echo "Info" > $script_log
search_names_folders=`find /home/ -type d -name "logs*"`
while read -r line; do
mv $line ${line//logs/log} >>$script_log 2>&1
done <<< "$search_names_folders"
My Solution is:
#!/bin/bash
script_log="/myPath"
echo "Info" > $script_log
search_names_folders=`find /home/ -type d -name "logs*"`
while read -r line; do
number_of_occurrences=$(grep -o "logs" <<< "$line" | wc -l)
if [ "$number_of_occurrences" != "1" ]; then
real_path=${line//logs/log} ## get the full path, the suffix will be incorrect
real_path=${real_path%/*} ## get the prefix until the last /
suffix=${line##*/} ## get the real suffix
line=$real_path/$suffix ## add the full correct path to line
mv $line ${line//logs/log} >>$script_log 2>&1
fi
done <<< "$search_names_folders"
But its bad idea, Has anyone have other solutions?
Thanks!
Use the -depth option to find. This makes it process directory contents before it processes the directory itself.

Bash script - how to keep looping 'find' command until file/s are found?

I am very new to linux scripting & am trying to set up a simple loop which will:
Ask user for file name
Search a specific directory for the file
If no files are found, ask the user to reinput a file name
If files are found, move on to the next step of the script
This is what I have so far, but it is not looping at all(i.e when no files are found, it is not asking the user to re-enter a file name. )
#!/bin/bash
read -p "Enter file name: " file
find /directory/ -name "$file" -print
while [ "$?" -ne 0 ]; do
read -p "File not found. Please re-enter file name: " file
find /directory/ -name "$file" -print
done
echo "rest of script etc"
Any help is appreciated! :)
The easiest way to do this is probably using globstar (available with bash 4)
#!/bin/bash
shopt -s globstar
while true; do
read -p "Enter file name: " file
for f in /directory/**/"$file"; do
echo "$f"
break 2 # escape both loops
done
echo "'$file' not found, please try again."
done
echo "rest of script etc"
It's also possible to do with find, but slightly annoying, given that you can't use standard UNIX exit statuses:
#!/bin/bash
read -p "Enter file name: " file
found=$(find /directory/ -name "$file" -print -quit)
while [[ -z $found ]]; do
read -p "File not found. Please re-enter file name: " file
found=$(find /directory/ -name "$file" -print -quit)
done
echo "$found"
echo "rest of script etc"
Normally I wouldn't recommend parsing the output of find, but in this case we're only concerned as to whether or not there is any output.
The easiest and most portable way might be this:
# Loop until user inputted a valid file name
while true ; do
# Read input (the POSIX compatible way)
echo -n "Enter file name: "
read file
# Use find to check if the file exists
[ $(find /etc -type f -name "$file" 2>/dev/null | wc -l ) != "0" ] && break
# go to next loop if the file does not exist
done
echo "Ok, go on here"

Remove in linux

hey so I have some code for the question bellow but I am stuck its not working and I don't really know what I am doing.
This script should remove the contents of the dustbin directory.
If the -a option is used the script should remove ALL files from the dustbin.
Otherwise, the script should display the filenames in the dustbin one by one and ask the user for confirmation that they should be deleted
#!/bin/sh
echo " The files in the dustbin are : "
ls ~/TAM/dustbin
read -p " Please enter -a to delete all files else you will be prompted to delete one by one : " filename
read ans
if ["filename" == "-a"]
cat ~/TAM/dustbin
rm -rf*
else
ls > ~/TAM/dustbin
for line in `cat ~/TAM/dustbin`
do
echo "Do you want to delete this file" $line
echo "Y/N"
read ans
case "ans" in
Y) rm $line ;;
N) "" ;;
esac
EDITED VERSION
if test ! -f ~/TAM/dustbin/*
then
echo "this directory is empty"
else
for resfile in ~/TAM/dustbin/*
do
if test -f $resfile ; then
echo "Do you want to delete $resfile"
echo "Y/N"
read ans
if test $ans = Y ; then
rm $resfile
echo "File $resfile was deleted"
fi
fi
done
fi
this works however Now I get one of 2 errors either
line 4 requires a binary operator or line 4: to many arguments
I see one obvious mistake:
rm -rf*
when it should be
rm -rf *
to be asked about every file deletion - add -i key
rm -rfi *
Many problems here:
A space missing before the * in rm. The space is needed so the shell can recognize the wildcard and expand it.
Do you really want to remove all the files in the current directory? If not, specify the path rm -rf /path/to/files/* or cd into the directory, preferably with cd /path/to/files || exit 1.
I do not understand the logic of the script. You show a dustbin, but if the user gives -a, you overwrite it with all the non-hidden files (ls > dustbin). Is that what you want?
First of all, case "ans" of just matches a string "ans" to other strings, which is obviously false, you need case $ans of to get the value of variable ans. if ["filename" == "-a"] is also comparison between two strings, which is always false. The first parameter of a script can be accessed as $1 (the second as $2 and so on).
Please read man 1 sh to get the basics of shell programming (all of the above notes can be found there).

Editing every file in a directory after opening it bash

Looking around I didn't see exactly what I was looking for. Some similar stuff, but for some reason what I tried so far hasn't worked.
My main goals:
run script in my current directory
open the picture to see what it is
rename the picture i just viewed
repeat the process without running the script again
These were the sources I attempted to follow:
Bash Shell Loop Over Set of Files
Bash loop through directory and rename every file
How to do something to every file in a directory using bash?
==================================================================================
echo "Rename pictures. Path"
read path
for f in $path
do
eog $path
echo "new name"
read newname
mv $path $newname
cat $f
done
You should pass the script an argument rather than trying to make it interactive. You also have numerous quoting problems. Try something like this instead (untested):
#!/usr/bin/env bash
moveFile() {
local newName=
until [[ $newName ]]; do
printf '%s ' 'new name:'
read -er newName # -e implies Bash with readline
echo
done
mv -i "$1" "${1%/*}/${newName}"
}
if [[ ! -d $1 ]]; then
echo 'Must specify a path' >&2
exit 1
fi
for f in "$1"/*; do
eog "$f"
moveFile "$f"
done
You might want to try something like this:
for f in $*; do
eog $f
echo "new name:"
read newname
mv $f $newname
done
If you name the script, say, rename.sh, you can call
./rename.sh *gif
to review all files with extention 'gif'.
Using find command allows you to search for image files in the specified directory recursively.
echo -n "Rename pictures. Input image directory: "
read path
for f in `find $path -type f`
do
eog $f
echo -n "Enter new name: "
read newname
mv $f $newname
echo "Renamed $f to $newname."
done

Resources