Remover/Exibir Arquivo/Diretório de uma estrutura de diretórios
Publicado por Italo Pessoa (última atualização em 05/10/2012)
[ Hits: 5.074 ]
Homepage: http://xconhecimento.blogspot.com.br
Script simples para procurar e remover ou exibir o path de um arquivo ou diretório.
O script é simples
rmr_init [-f | -d] directory [directory | file]
rmr_init [-f | -d] [-e | -r] directory [directory | file]
-e show the path of file or directory
-r remove the file or directory
-f set the type to regular file
-d set the type to directory
Já existe o find para fazer a mesma coisa, pesquisar e remover um diretório ou arquivo, mas para efeito de aprendizagem resolvi fazê-lo
#!/bin/sh
# rm_r.sh
# Script to found and remove or display
# a regular file or directory in a directory
# and their sub-directories
# Italo Pessoa <italoneypessoa@gmail.com>
# 01/10/2012 20:15
# # # # # # # # # # # # # HOW TO # # # # # # # # # # # # # # #
# source rm_r.sh #
# rmr_init [-f | -d] directory [directory | file] #
# rmr_init [-f | -d] [-e | -r] directory [directory | file] #
# #
# -e show the path of file or directory #
# -r remove the file or directory #
# -f set the type to regular file #
# -d set the type to directory #
# # # # # # # # # # # # # HOW TO # # # # # # # # # # # # # # #
# list of files and directories of the directory informed
_lsDir(){
# verify if is a regular file or a directory
if [ "$ISFILE" ]; then
_findMatchingFile "$1" "$2" "$ACTION"
else
_findMatchingDir "$1" "$2" "$ACTION"
fi
for dir in $(ls -A "$1" | sed 's/\ /(~)/g'); do
# dirctory name
dirName=$(echo "$dir" | sed 's/(~)/\ /g')
# verify if the directory exists
if [ -d "$1/$dirName" ]; then
# list of files and directories of the directory informed
_lsDir "$1/$dirName" "$2"
fi
done
}
# scan files on the current directory
_findMatchingFile(){
# regex to remove spaces
for file in $(ls -A "$1" | sed 's/\ /(~)/g'); do
# regex to replace previous to a correspondent value of spaces
fileName=$(echo "$file" | sed 's/(~)/\ /g')
# verify if is a regular file and exists
if [ -f "$1/$fileName" ]; then
# verify if is the same name of the searched file
if [ "$fileName" = "$2" ]; then
# verify if has action
if [ "$ACTION" ]; then
# validate action
case "$ACTION" in
"-r" )
# remove file
echo "Removing file:"
echo "$1/$fileName"
rm "$1/$fileName"
echo "Removed."
;;
"-e" )
# displays the path of file, starting of the current user
# directory, the directory from which the current user is
echo "$1/$fileName"
;;
esac
else
# STANDARD
# displays the path of file, starting of the current user
# directory, the directory from which the current user is
echo "$1/$fileName"
fi
fi
fi
done
}
# scan directories on the current directory
_findMatchingDir(){
# regex to remove spaces
for dir in $(ls -A "$1" | sed 's/\ /(~)/g'); do
# regex to replace previous to a correspondent value of spaces
dirName=$(echo "$dir" | sed 's/(~)/\ /g')
# verify if is a directory and exists
if [ -d "$1/$dirName" ]; then
# verify if is the same name of the searched directory
if [ "$dirName" = "$2" ]; then
# verify if has action
if [ "$3" ]; then
case "$3" in
"-r" )
# remove directory
echo "Removing directory:"
echo "$1/$dirName"
rm -r "$1/$dirName"
echo "Removed."
;;
"-e" )
# displays the path of direcory, starting of the current user
# directory, the directory from which the current user is
echo "$1/$dirName"
;;
esac
else
# STANDARD
# displays the path of direcory, starting of the current user
# directory, the directory from which the current user is
echo "$1/$dirName"
fi
fi
fi
done
}
# verify is the directory exists
_dirExists(){
if [ ! -d "$1" ]; then
echo "ERROR: The directory \"$1\" doesn't exists."
exit 1
fi
}
# configure the action
_configAction(){
case "$1" in
"-r" )
# remove directory or file
ACTION="-r"
;;
"-e" )
# displays the path of file or directory
ACTION="-e"
;;
*)
echo "ERROR: The option \"$1\" doesn't exists."
exit 1
;;
esac
}
rmr_init(){
# key to control the type of file (regular or directory)
export ISFILE=""
# key to control the action
export ACTION="-e"
# verify how many parameters were informed
if [ "$#" -ge 3 ]; then
if [ "$#" -eq 4 ]; then
case "$1" in
"-f" )
# verify if directory exists
_dirExists "$3"
# configure the action
_configAction "$2"
# set the type of file to regular file
ISFILE=1
;;
"-d" )
# verify if directory exists
_dirExists "$3"
# configure the action
_configAction "$2"
# set the type of file to directory
ISFILE=""
;;
*)
# display error message
echo "ERROR: You must set: file(-f), directory(-d), remove(-r) or show(-e)."
exit 1
;;
esac
# list of files and directories of the directory informed
_lsDir "$3" "$4"
elif [ "$#" -gt 4 ]; then
echo "ERROR: You inserted many options"
exit 1
elif [ "$#" -eq 3 ]; then
case "$1" in
"-f" )
_dirExists "$2"
ISFILE=1
;;
"-d" )
_dirExists "$2"
ISFILE=""
;;
*)
# display error message
echo "ERROR: You must set: file(-f) or directory(-d)."
exit 1
;;
esac
# list of files and directories of the directory informed
_lsDir "$2" "$3"
fi
else
# display error message
echo "ERROR: you must insert at least three parameters."
fi
}
Shell Script para instalação do ePSXe no Ubuntu
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Atualizar Linux Mint 22.2 para 22.3 beta
Jogar games da Battle.net no Linux com Faugus Launcher
Como fazer a Instalação de aplicativos para acesso remoto ao Linux
Conky, alerta de temperatura alta (11)
Assisti Avatar 3: Fogo e Cinzas (3)
Duas Pasta Pessoal Aparecendo no Ubuntu 24.04.3 LTS (42)









