LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-14-2011, 01:42 AM   #1
whatthefunk
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Rep: Reputation: 0
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.
 
Old 02-14-2011, 01:58 AM   #2
whatthefunk
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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....
 
Old 02-14-2011, 12:26 PM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,765

Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
Try using a trailing slash:
mv -- "$SOURCE" "$DEST/${FILE}/"
instead of:
mv -- "$SOURCE" "$DEST/${FILE}"
 
Old 02-14-2011, 03:56 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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

Last edited by Nominal Animal; 03-21-2011 at 07:56 AM.
 
Old 02-15-2011, 01:52 AM   #5
whatthefunk
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Nominal Animal View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash file names with spaces alex1986 Programming 5 07-26-2010 09:47 PM
For loop in bash using ls to get array of file names bioinformatics_guy Linux - Newbie 5 02-19-2009 10:49 AM
BASH: Truncating file names with special character tboss888 Programming 2 12-18-2008 07:28 PM
bash - replace all spaces in file, folder names babag Programming 24 04-20-2008 12:17 AM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM


All times are GMT -5. The time now is 09:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration