Shell Script for file and directory localization

Asked

Viewed 1,671 times

2

I wish someone could help me to create this script correctly, in this case it is to create a script that Oce type the name and directory of a file and then indicate whether it was located or not and that manages this type of output on the screen

  1. $ ./locates.sh
  2. Type the file name: .txt file
  3. Enter the directory name: /home/user/Documents
  4. The.txt file was not located in the directory /home/usuario/Documents

Or

  1. $ ./locates.sh
  2. Type the file name: .txt file
  3. Enter the directory name: /home/user/Codes
  4. The.txt file was located in the /home/user/Codes directory

I did it but it wasn’t totally right

#!/bin/bash

# localiza.sh

# script para localizar arquivos e diretórios

echo "Digite o nome do diretório: "

read DIR

echo "Digite o nome do arquivo: "

read ARQUIVO

#

find $DIR -name '$ARQUIVO' && echo "Busca efetuada com sucesso!" || echo "Arquivo não encontrado"
  • What happened? Any errors?

  • Even if you don’t find the file but find the directory the message is always the same.

  • Always appears the search message successfully performed even if the file does not exist.

1 answer

2


I made some modifications to your code and it worked here:

#!/bin/bash

# localiza.sh

# script para localizar arquivos e diretórios

echo "Digite o nome do diretório: "

read DIR

echo "Digite o nome do arquivo: "

read ARQUIVO

X=$(find $DIR -name "$ARQUIVO")

#
[ -n "$X" ] && echo "Busca efetuada com sucesso!" || echo "Arquivo não encontrado!"
  • 1

    Thanks a lot, thanks a lot for your help.

  • @I smoke if I managed to answer your question, mark my post as solution on the left side. Thanks!

  • 1

    My colleague had forgotten to do it,.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.