- Use
stat
tool to get inode number.
- rename directly using
mv
.
- Quote filenames (!!), e.g.
"$filemove"
, never $filemove
.
- Add some checking for security before moving
[ ! -e "$target" ] && mv ...
- Use
set -euo pipefail
at the beginning of the script, so it fails on any error.
- Use
for f in "$@"; do ... done
loop to allow multiple files as arguments.
- Again: Quote filenames (!!).
- You might be better off using some ready solutions, e.g. see:
#!/bin/bash
# Removing Files into the Recycle Bin (Deleted Directory)
set -euo pipefail #make script exit on any error
mkdir -p "$HOME/deleted"
dest="$HOME/deleted/${1}_$(stat --format %i "$1")"
# check if file exists, and if not, do the move!
[ -e "$dest" ] && echo "Target exists, not moving: $1" || mv "$1" "$dest"
Use like trash file1
or trash "file with spaces"
(assuming trash
being the script name...)
or to be able to trash multiple files at once:
#!/bin/bash
# Removing Files into the Recycle Bin (Deleted Directory)
set -euo pipefail #make script exit on any error
mkdir -p "$HOME/deleted"
for f in "$@"; do
dest="$HOME/deleted/${f}_$(stat --format %i "$f")"
# check if file exists, and if not, do the move!
[ -e "$dest" ] && echo "Target exists, skipped moving: $f" || mv "$f" "$dest"
done
Use like trash file1 file2 "file with spaces"
mv
? Please edit your question with any further clarifications. Thank you, and welcome to the site! Don't forget to take our tour to become more familiar with the site. – Jeff Schaller May 16 '19 at 13:40