LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 12-18-2005, 02:02 PM   #1
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Rep: Reputation: 15
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
 
Old 12-18-2005, 03:01 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
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?
 
Old 12-18-2005, 03:28 PM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

How about '$i/..'

Dave

Edit. Ack. Too late by far.
 
Old 12-18-2005, 03:37 PM   #4
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
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/../
 
Old 12-18-2005, 05:02 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

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

Last edited by ilikejam; 12-18-2005 at 05:46 PM. Reason: Added "" round find command
 
Old 12-18-2005, 05:46 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
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.
 
Old 12-19-2005, 04:55 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
find . | grep pattern |
while read file;do
echo mv "$file" to_here
done
 
Old 12-19-2005, 11:08 AM   #8
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks guys; the ${var}/.. works beautifully.
 
Old 12-19-2005, 10:51 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Glad it worked out for you. Hope your whole project goes that smoothly.
 
  


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
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 12:01 PM
how to write a script to move files from one directory to another myeire Linux - Newbie 7 02-25-2005 10:01 AM
denied access to move files into /usr/src directory Larry O Linux - Newbie 4 01-17-2005 11:14 PM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 03:04 AM


All times are GMT -5. The time now is 08:55 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