LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   loop move files up one directory level (https://www.linuxquestions.org/questions/programming-9/loop-move-files-up-one-directory-level-394227/)

Melsync 12-18-2005 02:02 PM

loop move files up one directory level
 
I am trying to recursively move files with a certain pattern in some directories up one level in their path and then remove them from the original location.

Quote:

#!/bin/bash
IFS=`echo -e "\012\015"`

function moveup() {
pushd $1
for i in `ls -F | grep /`
j=${i%%expression*}*
do
if [ -d "$i" ]
then
mv $i/$j ${VARIABLE MEANING UPPER LEVEL THAN $i}
rm -f $i/$j
fi
done
popd
}
How can I write that ${VARIABLE MEANING UPPER LEVEL THAN $i}?

Osio

P.S. Most of the code above is inspired by CroMagnon's post http://www.linuxquestions.org/questi...43#post1217543, although in the IFS bit I really don't know what I'm doing :)

Matir 12-18-2005 03:01 PM

I am not 100% sure what you are doing, but have you considered doing: "$i/../" for your upper level? Or something along those lines. if $i is "hello/world", then $i/.. is "hello/". Is that what you wanted?

ilikejam 12-18-2005 03:28 PM

Hi.

How about '$i/..'

Dave

Edit. Ack. Too late by far.

Melsync 12-18-2005 03:37 PM

I define a variable with a pattern. Then I want to find files with that pattern in listed directories of a given one. Once found, I simply want to move those files from those directories up to the given directory.

Matir, I'm sorry but I don't understand. I've tried defining variables like
Quote:

i=`ls`
echo $i
dir1
dir2
file1
file2
but when I do
Quote:

echo $i/..
dir1
dir2
file1
file2/..
and
Quote:

echo $i/../
dir1
dir2
file1
file2/../

ilikejam 12-18-2005 05:02 PM

You'll have to deal with each file individually. At the moment, you've got all the paths in one variable.

Something like:
Code:

for go in "`find . -type f`"
  do
  mv "$go" `dirname "$go"`/..
done

would move all files in the current directory up a directory. You can add options to the find command to specify which files to move. See the find man page for further fun.

Dave

Matir 12-18-2005 05:46 PM

Then the 'mv $i/$j' wouldn't work either, as it would only append $j to the last item in $i. You will need to split it.

bigearsbilly 12-19-2005 04:55 AM

find . | grep pattern |
while read file;do
echo mv "$file" to_here
done

Melsync 12-19-2005 11:08 AM

Thanks guys; the ${var}/.. works beautifully.

Matir 12-19-2005 10:51 PM

Glad it worked out for you. Hope your whole project goes that smoothly. :)


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