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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-18-2005, 02:02 PM
|
#1
|
|
Member
Registered: Sep 2005
Posts: 75
Rep:
|
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 $1for 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 
|
|
|
|
12-18-2005, 03:01 PM
|
#2
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
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?
|
|
|
|
12-18-2005, 03:28 PM
|
#3
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Hi.
How about '$i/..'
Dave
Edit. Ack. Too late by far.
|
|
|
|
12-18-2005, 03:37 PM
|
#4
|
|
Member
Registered: Sep 2005
Posts: 75
Original Poster
Rep:
|
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/../
|
|
|
|
|
12-18-2005, 05:02 PM
|
#5
|
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
|
12-18-2005, 05:46 PM
|
#6
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
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.
|
|
|
|
12-19-2005, 04:55 AM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
find . | grep pattern |
while read file;do
echo mv "$file" to_here
done
|
|
|
|
12-19-2005, 11:08 AM
|
#8
|
|
Member
Registered: Sep 2005
Posts: 75
Original Poster
Rep:
|
Thanks guys; the ${var}/.. works beautifully.
|
|
|
|
12-19-2005, 10:51 PM
|
#9
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
Glad it worked out for you. Hope your whole project goes that smoothly. 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:14 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|