Move file(s) to folder [solved]

  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

I would like to setup a command can which would move a file (or files) into a specified folder:

e.g. 1: . can file1.txt
--->
. mv file1.txt /dir1/dir2/dir3/.trashcan

e.g. 2: . can file2.*
--->
. mv file2.* /dir1/dir2/dir3/.trashcan

Note: My system is HP-UX.

Is this possible? Thanks for any help!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

This *should* work for you, but I don't have an HP-UX machine to actually try it. Wildcards and all should function normally too.
Code: [ Select ]
#!/bin/bash

export CANDIR='/path/to/the/can'

if [ $# -lt 1 ]
then
    echo " Please specify a file."
    exit 1
fi

for i in $@;
do
    if [ -e "${i}" ]
    then
     mv -f "${i}" ${CANDIR}
    else
    echo "${i} not found, make sure you typed it correctly."
    exit 1
    fi
done
  1. #!/bin/bash
  2. export CANDIR='/path/to/the/can'
  3. if [ $# -lt 1 ]
  4. then
  5.     echo " Please specify a file."
  6.     exit 1
  7. fi
  8. for i in $@;
  9. do
  10.     if [ -e "${i}" ]
  11.     then
  12.      mv -f "${i}" ${CANDIR}
  13.     else
  14.     echo "${i} not found, make sure you typed it correctly."
  15.     exit 1
  16.     fi
  17. done
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

Hi this213. When I apply what you wrote above, I get prompted with:

Please specify a file.

when I log-in. How do I have it only execute when I want it to? e.e.g

can *.txt
can tab1.lis

Thanks for your help
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Only when you want it to? I'm not sure I understand the issue here.

Save the script I posted as ~/bin/can (change /path/to/the/can to wherever you need it) and set the execute bit and then you can issue it like any other command - providing ~/bin/ is in your path, which you may want to check:
Code: [ Select ]
echo $PATH

You'll then be able to use it exactly as you describe.
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

Thanks for your help, I think I'm almost there!

I saved the file can in /dir1/dir2/dir3/bin/

Code: [ Select ]
#!/dir1/dir2/dir3/bin
export CANDIR='/dir1/dir2/dir3/.trashcan/'
if [ $# -lt 1 ]
then
    echo " Please specify a file."
    exit 1
fi
for i in $@;
do
  if [ -e "${i}" ]
  then
     mv -f "${i}" ${CANDIR}
  else
    echo "${i} not found, make sure you typed it correctly."
    exit 1
  fi
done
  1. #!/dir1/dir2/dir3/bin
  2. export CANDIR='/dir1/dir2/dir3/.trashcan/'
  3. if [ $# -lt 1 ]
  4. then
  5.     echo " Please specify a file."
  6.     exit 1
  7. fi
  8. for i in $@;
  9. do
  10.   if [ -e "${i}" ]
  11.   then
  12.      mv -f "${i}" ${CANDIR}
  13.   else
  14.     echo "${i} not found, make sure you typed it correctly."
  15.     exit 1
  16.   fi
  17. done


I also added PATH=$PATH:/dir1/dir2/dir3/bin to my .bashrc file. When I type echo $PATH, I see the above appended on to the end of the $PATH directive.

I have the folder /dir1/dir2/dir3/.trashcan with 700 permissions.
I have the file /dir1/dir2/dir3/temp.lis with 770 permissions.

Test:
/dir1/dir2/dir3 . can temp.lis

Result:
-bash: /dir1/dir2/dir3/bin/can: /dir1/dir2/dir3/bin: bad interpreter: Permission denied
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

The shebang line (the top line) needs to point to your bash interpreter (usually /bin/bash). To find the proper location for bash, do:
Code: [ Select ]
which bash

This is why you're gettting a "bad interpreter" error.

*edit
while you're getting this figured out, you may want to change "mv -f" to "cp -f" so you don't inadvertantly nuke your system.
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

Thanks this213.

By changing the shebang line and "mv -f" to "cp -f" the can command copies a version of the file to the .trashcan folder.

I would like it to move it though. Is it safe to change cp back to mv do you think?
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

If it appears to be working properly, yes. I was just saying to change it just in case (better safe than sorry and all)
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

Awesome stuff this213.

Now I would like to when I login (or logout) have this .trashcan folder delete anything in there that is more than 7 days old.

In effect, this will act like a BULK folder in email where messages expire after a certain timeframe.

Can you point me in the right direction?
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Code: [ Select ]
crontab -e

then paste this in
Code: [ Select ]
0 0 * * * find /dir1/dir2/dir3/.trashcan/* -type f -mtime +7 -exec rm -f {} \;

This will run the find command every day at midnight, the "-exec rm -f {}" portion deletes any files modified more than 7 days ago ("-mtime +7").

Just a note of warning, the above command WILL delete any files found which match the criteria, make sure this is what you're really wanting before you implement it. Whether you log in or log out makes no difference, the crontab will run in any case.

You may or may not have permission on the system to install new crontab entries as a regular user (depends on how your system's configured). If this is the case, you can add the above command to your .bashrc which will execute when you log in.
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

I added find /dir1/dir2/dir3/.trashcan/* -type f -mtime +7 -exec rm -f {} \; to my .bashrc file - works like a charm.

Q: Would it be possible to change the mtime of these files to the date when they are moved?

Finally - back to the code one more time (sorry) I would like to add an argument so that directories aren't captured by changing
Code: [ Select ]
if [ -e "${i}" ]

to something like
Code: [ Select ]
if [ -e "${i}" ] && [ !-d "${i}" ]


Test:
can j*.txt

Result:
/dir1/dir2/dir3/bin/can: line 10: [: !-d: unary operator expected
j*.txt not found, make sure you typed it correctly.
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

issue "man utime" for all of the options on changing a file's mtime.

The other is close, but not quite. You'll have issues trying to test "if not directory". Instead, just test to see if it's a regular file as such:
Code: [ Select ]
#!/bin/bash

export CANDIR='/path/to/the/can'

if [ $# -lt 1 ]
then
    echo " Please specify a file."
    exit 1
fi

for i in $@;
do
  if [ -e "${i}" ] && [ -f "${i}" ]
  then
     mv -f "${i}" ${CANDIR}
  fi
done
  1. #!/bin/bash
  2. export CANDIR='/path/to/the/can'
  3. if [ $# -lt 1 ]
  4. then
  5.     echo " Please specify a file."
  6.     exit 1
  7. fi
  8. for i in $@;
  9. do
  10.   if [ -e "${i}" ] && [ -f "${i}" ]
  11.   then
  12.      mv -f "${i}" ${CANDIR}
  13.   fi
  14. done

You'll notice that I removed the "else" here, this is because the existence of a directory will throw an error because it's not a regular file.
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

Thanks this213!
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

No problem, now I have another script that I can add to my collection ;)
  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

Post 3+ Months Ago

And all this time I thought you were working for/helping me! :lol:

Post Information

  • Total Posts in this topic: 15 posts
  • Users browsing this forum: No registered users and 96 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.