Thread: Bash script quick question
hi
i'm bash script (almost) absolute beginner. i'm writing quick script maintain /home/ in order, deleting files system generates never/seldom use (ex: "example~" files generated emacs, "dead.letter" file generated alpine etc.).
want script check existence of said files , if exist delete them, if file not exist, want bash tell me has nothing do.
here wrote:
==begin==
## delete file "~" in .myscript folder if exist
if ls ~/.my_scripts/*~
rm -f ~/.my_scripts/*~
else printf "nothing mate!\n"
fi
## delete file dead.letter generated alpine
if ls ~/dead.letter
rm -f ~/dead.letter
else printf "nothing mate!\n"
fi
==end==
here problem : when script checks existence of file 'ls' verbose result, annoying... if file exist if file not exist.
1. there way ask 'ls' not verbose result?
2. there better way i'm trying do?
thanks!
ls may not best way this
untested, may better
code:#!/bin/bash ifs=$'\n' ## delete file "~" in .myscript folder if exist files=$(find ~/.myscripts -type f -name '*~') if [ -n "$files" ]; for file in $files; rm -f "$file" done else echo "nothing mate!\n" fi ## delete file dead.letter generated alpine if [ -f ~/dead.letter ]; rm -f ~/dead.letter else echo "nothing mate!\n" fi
Forum The Ubuntu Forum Community Ubuntu Official Flavours Support General Help [ubuntu] Bash script quick question
Ubuntu
Comments
Post a Comment