Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Bash script instead of rm to prompt with number of files about to delete (a safer rm)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="OS/4" data-source="post: 1792964" data-attributes="member: 358964"><p>I found out about the -d option on ls, which avoid listing directory contents if a dir is specified (or expanded from *). That makes the script simpler:</p><p></p><p>[CODE]</p><p>#!/bin/bash</p><p># Writes out how many files and then prompts for deletion</p><p>if [ "$#" -eq 0 ] ; then</p><p> echo "usage: rmi [-f | -i] [-PRrvW] file ..."</p><p> exit 1</p><p>fi</p><p></p><p>FLAGS="d" # don't go into specified/expanded directories unless recursive flag (below)</p><p>for PARAM in "$@" ; do</p><p> if [ "$PARAM" = "-r" ] || [ "$PARAM" = "-R" ] ; then</p><p> FLAGS="R"</p><p> fi</p><p>done</p><p></p><p>NUMBER=$(ls -1APp$FLAGS "$@" 2>/dev/null | grep -c '[^/:]$') # don't count directories or directory headings</p><p>if [ $NUMBER = 0 ] ; then</p><p> echo "rmi: $@: No such file or directory"</p><p>else</p><p> read -n 1 -p "Deleting $NUMBER file(s), Are you sure (Y/N)? " yn</p><p> echo</p><p> if [ "$yn" != "${yn#[Yy]}" ] ; then</p><p> echo rm "$@"</p><p> fi</p><p>fi</p><p>[/CODE]</p><p></p><p>So now the only questions I would have for someone experienced with UNIX is whether there are any big dangers or flaws in this script. Thanks!</p></blockquote><p></p>
[QUOTE="OS/4, post: 1792964, member: 358964"] I found out about the -d option on ls, which avoid listing directory contents if a dir is specified (or expanded from *). That makes the script simpler: [CODE] #!/bin/bash # Writes out how many files and then prompts for deletion if [ "$#" -eq 0 ] ; then echo "usage: rmi [-f | -i] [-PRrvW] file ..." exit 1 fi FLAGS="d" # don't go into specified/expanded directories unless recursive flag (below) for PARAM in "$@" ; do if [ "$PARAM" = "-r" ] || [ "$PARAM" = "-R" ] ; then FLAGS="R" fi done NUMBER=$(ls -1APp$FLAGS "$@" 2>/dev/null | grep -c '[^/:]$') # don't count directories or directory headings if [ $NUMBER = 0 ] ; then echo "rmi: $@: No such file or directory" else read -n 1 -p "Deleting $NUMBER file(s), Are you sure (Y/N)? " yn echo if [ "$yn" != "${yn#[Yy]}" ] ; then echo rm "$@" fi fi [/CODE] So now the only questions I would have for someone experienced with UNIX is whether there are any big dangers or flaws in this script. Thanks! [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Bash script instead of rm to prompt with number of files about to delete (a safer rm)
Top