LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: bash file names are different than openbox trash file names (https://www.linuxquestions.org/questions/programming-9/bash-bash-file-names-are-different-than-openbox-trash-file-names-862533/)

whatthefunk 02-14-2011 01:42 AM

Bash: bash file names are different than openbox trash file names
 
Im writing my first bash script. Its function is to move files to the trash can and write a log file in the same format that the system does to allow for file restoration. The problem is that in bash, everything works fine, but in the OpenBox window session, the files are named after the source directory, not the original name.
Heres the script:

Code:

#!/bin/bash

# trash - Script to move file or folder to the trash can and create a log file

##### Functions #####

err_output ()                                          # Writes error message
{
  echo "$0: cannot stat \`$1\': No such file or directory"
  echo "USAGE: $0 SOURCE DEST"
  exit 1
} >&2

write_log_numbered ()                                  # Writes log entry for numbered files
{
echo "[Trash Info]" > $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
echo "Path="$PWD >> $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
echo "DeletionDate="`date "+%Y-%m-%dT%H:%M:%S"` >> $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
}

write_log_unique ()                                    # Writes log entry for unique files
{
echo "[Trash Info]" > $HOME/.local/share/Trash/info/${FILE}.trashinfo
echo "Path="$PWD >> $HOME/.local/share/Trash/info/${FILE}.trashinfo
echo "DeletionDate="`date "+%Y-%m-%dT%H:%M:%S"` >> $HOME/.local/share/Trash/info/${FILE}.trashinfo
}

make_paths ()                                          # Makes necessary folders
{
mkdir -p ~/.local/share/Trash
mkdir -p ~/.local/share/Trash/files
mkdir -p ~/.local/share/Trash/info
}

##### Main #####

make_paths

SOURCE="$1"
DEST=$(dirname ~/.local/share/Trash/files/filename)

[ -e "$SOURCE" ] || err_output "$SOURCE"
[ -d "$DEST" ] || err_output "$DEST"

FILE="$(basename "$SOURCE")"

if [ -e "${DEST}/${FILE}" ]; then
  max=0
  DIR="$(pwd)"
  cd "$DEST"
  shopt -s nullglob
  for backup in "${FILE}."; do
    nr=${backup#${FILE}.}
    if [[ "$nr" =~ ^[0-9]+$ ]]; then
      if (( nr>max )); then
        max="$nr"
      fi
    fi
  done
  cd "$DIR"
  max=$(( max + 1 ))
  write_log_numbered
  mv -- "$SOURCE" "${DEST}/${FILE}.$max"
else
  write_log_unique
  mv -- "$SOURCE" "$DEST/${FILE}"
fi

So I run the script with the test file "Junk"..
In bash, it moves over and its named correctly.
Code:

~/.local/share/Trash/files$ ls
file  file.1  Files  Files.1  Junk

The log file is also named correctly
Code:

~/.local/share/Trash/info$ ls
file.1.trashinfo  Files.1.trashinfo  Files.trashinfo  file.trashinfo  Junk.trashinfo

But, when I go to view the trash can in the file manager in Openbox, the file is called "Testing" which is the name of the source directory. However, if I go to the trashcan via its full path (going to .local/, then share/) all the files are named correctly. Whats going on here?? Is there some way to get the trash can to read the correct file name?
Any help would be appreciated.

whatthefunk 02-14-2011 01:58 AM

I just did a second test. I simply moved a file with the mv command to the Trash folder and then viewed it in the file manager. The file was named correctly. So there is something wrong with the way that I am naming files with my script....

gnashley 02-14-2011 12:26 PM

Try using a trailing slash:
mv -- "$SOURCE" "$DEST/${FILE}/"
instead of:
mv -- "$SOURCE" "$DEST/${FILE}"

Nominal Animal 02-14-2011 03:56 PM

Use
Code:

echo "Path=$SOURCE" >> "$HOME/.local/share/Trash/info/${FILE}.$max.trashinfo"
echo "Path=$SOURCE" >> "$HOME/.local/share/Trash/info/${FILE}.trashinfo"

instead. The Path in .trashinfo files name the original file. You're putting the working directory there instead.

Hope this helps,
Nominal Animal

whatthefunk 02-15-2011 01:52 AM

Quote:

Originally Posted by Nominal Animal (Post 4258274)
Use
Code:

echo "Path=$SOURCE" >> "$HOME/.local/share/Trash/info/${FILE}.$max.trashinfo"
echo "Path=$SOURCE" >> "$HOME/.local/share/Trash/info/${FILE}.trashinfo"

instead. The Path in .trashinfo files name the original file. You're putting the working directory there instead.

Hope this helps,
Nominal Animal

Yep, thanks Nominal Animal. My path ended at the directory, not at the file name and so the Trash can was naming everything th path directory. Thanks!


All times are GMT -5. The time now is 04:35 PM.