1

I am having issues with my code below:

#!/bin/bash
#Removing Files into the Recycle Bin(Deleted Directory)
filemove=$1 #Saving the first argument as "filemove"
mkdir -p ~/deleted #Create the deleted directory if it doesn't exist
mv $filemove ~/deleted #Moves the file

I need the files in the recycle bin to follow a format as: filename_inode.

muru
  • 72,889
  • 1
    Thank you for the edit! Just to clarify your question, you're asking for "the next line of code" -- to do what? You've already moved the file. Are you now trying to rename it to include the inode? Consider renaming during the 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
  • You might better use some ready solutions: See https://askubuntu.com/questions/213533/command-to-move-a-file-to-trash-via-terminal or https://unix.stackexchange.com/questions/42757/make-rm-move-to-trash – pLumo May 16 '19 at 13:44
  • I am trying to put files the into the recycle bin after being removed. I need the files in the recycle bin to follow a format as: filename_inode. – Johnny Cash May 16 '19 at 13:50

1 Answers1

4
  • 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"

pLumo
  • 22,565
  • before you copy paste my script, make sure you understand every single bit of it. I did not test it! – pLumo May 16 '19 at 14:01
  • I'm doing that now! I wasn't copying and pasting that's plagiarism just getting a idea of what I need to do. I really appreciate it – Johnny Cash May 16 '19 at 14:02
  • If you liked an answer, you might consider an upvote, and if it worked for you, accept it by clicking the check mark on the left ;-) – pLumo May 16 '19 at 14:18
  • I have no issue with that :D thanks again I wish I can do more to recommend you but I am new to the site – Johnny Cash May 16 '19 at 14:20