Bash script that deletes files in a specific directory? - bash

I'm starting to write bash scripts and I would like to do the following:
a script that deletes the contents of a directory specified in the second argument of the invocation. Like this: example.sh /home/A
Any suggestions?
Thanks!!

Literally find /home/A -delete. Or find "$#" -delete if you want to put it in a script and clean multiple directories.

please use following code
#!/bin/bash
yellow=`tput setaf 3`;
info() {
echo "${yellow}INFO : $# ${reset}";
}
main () {
if [ $# -ne 0 ]; then
S_DIR=$1;
rm -rf "$ACTION";
info "main : delete folder.";
else
info "main : please enter folder path.";
fi
}
main $#;

Related

recover files deleted with rm command

Please if I run the command
# CREATE TRASH FOLDER
if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/deleted
fi
TRASH=$HOME/deleted
mv $# $TRASH
To move file or directory to the trash created. what is the possible command i can run to recover same file to the original directory
If you create a deleted directory like this, you will probably get some unexpected behavior. For example:
rm_script test.txt
cd ../other_directory
rm_script test.txt
will create a single file test.txt with the content of the one in other_dir. Even more fun when you start rm_scripting/moving directories.
Knowing this, and referring once more to trash-cli (see comment Aserre), you might:
if ! [ -d "$HOME/deleted" ] ; then
echo "What a pity! No deleted directory."
echo "Your file(s) is/are lost forever!"
exit 361
fi
TRASH=$HOME/deleted
for file in $# ; do
if [ -f "$TRASH/$file" ] ; then
cp "$TRASH/$file" .
else
echo "Hmm,.. I cannot find $file."
fi
done
This also may have some unwanted results, like removing the file from one directory and un-deleting it in another.
I just do the reverse of the same command line
function undo () {
echo -n "Do you want to recover $*? "
read ANSWER
if [ "$ANSWER" = "y" ]; then
mv $TRASH/$# $PWD
echo "$# successfully recovered"
else
echo "Oops, request denied"
fi
}

Issues in getting $PWD in shell script

I am calling a system call from my linux application.
/* Some file.c */
file.c is embedded in an executable called file.elf. this file.elf is present in directory
/home/ubuntu/file.elf
when i execute the file.elf the echo $BB_PATH prints the executable directory path. i am expecting the directory path to be the path where the script has been placed. i.e
/home/ubuntu/Desktop/BIN/BB/Chk_File.sh
How can this be acheived ?
if(!(system("ls /home/ubuntu/Desktop/BIN/BB")))
{
/* Test Path : remove after testing */
dw_flag = system("/home/ubuntu/Desktop/BIN/BB/Chk_File.sh");//Call to execute Script
dw_flag = WEXITSTATUS(dw_flag);
}
this in turns call the file CHK_File.sh
ret_val=0
BB_PATH=$(pwd)
echo $BB_PATH
if [ ! -f ACTION_TAG.txt ]
then
echo " ACTION_TAG NOT PRESENT "
else
ret_val=1
fi
echo $ret_val
exit $ret_val
You can use dirname to get the directory of the shell script.
echo `dirname $0`
Adding this below lines in script helped me
if [ -L $0 ] ; then
DIR=$(dirname $(readlink -f $0)) ;
else
DIR=$(dirname $0) ;
fi ;
echo $DIR
Now we dont need to worry from which directory the script is called !! Great !!

Is it possible to make some shell calls automatically when entering a directory?

Just like a local .bashrc file, which is sourced every time I entered the directory.
How to make this work?
You can use an alias:
$ echo 'echo "execute something for $PWD"' > tests/.cdrc
$ _cd()
{
\cd $1
[ -r .cdrc ] && . .cdrc
}
this function first change to the dir specified as argument, check if the file .cdrc is readable and source it.
$ alias cd=_cd
Then
$ cd tests
execute something for /path/to/tests
bash and zsh (and probably many other shells) have a feature that allows you to run an arbitrary command before the prompt is displayed. You can use this to source a .dirrc file, and it won't break tab completion.
Here's how to do it in bash:
PROMPT_COMMAND='
if [ "${PREV}" != "$(pwd -P)" ]; then
if [ -r .dirrc ]; then
. ./.dirrc
fi
PREV=$(pwd -P)
fi
'
From the bash man page:
PROMPT_COMMAND: If set, the value is executed as a command prior to issuing each primary prompt.
This is how to do it in zsh (see the zshmisc man page):
precmd() {
if [ "${PREV}" != "$(pwd -P)" ]; then
if [ -r .dirrc ]; then
. ./.dirrc
fi
PREV=$(pwd -P)
fi
}

If fails or copy function fails?

I have a little bash script where I compare two files. If one doesn't exist and second one exists, then I will copy/replace backup to main folder.
Somehow this doesn't seem to work. Hope someone can give a hand on this one:
#!/bin/bash
if [ ! -f "/Folder1/$1.jpg" ] && [ -f "/BU_Folder2/$1_BU.jpg" ]; then
cp -fp /BU_Folder2/$1_BU.jpg /Folder1/$1.jpg
cp -fp /BU_Folder2/$1_BU.mp4 /Folder1/$1.mp4
fi
At the prompt, run the following commands:
$ set -- FILENAME # FILENAME is the value you think $1 is supposed to have
$ [ ! -f "/Folder1/$1.jpg" ] && [ -f "/BU_Folder2/$1_BU.jpg" ] && echo success
If the last command does not print "success", then your script probably does not have the value for $1 that you think it does. Add echo $1 to the top of your script to confirm.
If it does print "success", and your script has no error output from cp, I'm not sure what to suggest.

Check if directory is /Library or /Public

I am trying to write a bash script which takes a users home directory and cycles through the first level of subdirectories and performs some maintenance on those directories only if it is not the /Library or /Public folder. The code I have so far does not work as I get an error message saying that the directory name returned by $dir is a directory. Here is the code:
#!/bin/bash
user="short name"
source_root="/Users/"
source_use="$source_root$user"
cd "$source_use"
dirarr=( */ )
echo ${dirarr[#]}
for dir in "${dirarr[#]}"
do
if ( "$dir" -ne "/Library" -o "$dir" -ne "/Public")
then echo $dir.
# do something
fi
done
Can anyone help me get this working.
Many thanks
Your script has several problems:
You need to use [ ] or [[ ]] in your if statement, not ( ). In your example ( ) creates a subshell and tries to run a command "$dir", which is the reason you're getting the error message you see.
You're comparing against strings that you won't find - try "Library/" and "Public/" instead.
You probably want -a instead of -o.
-ne is used to compare numbers. You want !=.
Here's a corrected version of your script:
#!/bin/bash
user="short name"
source_root="/Users/"
source_use="$source_root$user"
cd "$source_use"
dirarr=( */ )
echo ${dirarr[#]}
for dir in "${dirarr[#]}"
do
if [ "$dir" != "Library/" -a "$dir" != "Public/" ]
then
echo $dir.
# do something
fi
done
Try this:
cd $source_root$user
for dir in `find . -maxdepth 1 -type d`
do
if [ $dir = ./Library ] || [ $dir = ./Public ]
then
continue
fi
(Perform actions)
done
Also, bash is backwards. != is string non-equality, -ne is integer non-equality. So, change to equals signs, too.
Good luck!

Resources