LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2009, 11:56 AM   #1
FLX
Member
 
Registered: Apr 2005
Distribution: Fedora Core 5, SuSE 10.1
Posts: 40

Rep: Reputation: 15
Recursively move files one directory up


Hello,

I have folders using the following structure:
Code:
root@/volume1/Movies/Hitman.2007-FLX: tree
.
|-- 1. Sample
|   `-- Hitman.2007.[Sample].mkv
|-- 2. Pictures
|   |-- Hitman 1.bmp
|   |-- Hitman 2.bmp
|   |-- Hitman 3.bmp
|   `-- Hitman 4.bmp
|-- 3. Movie
|   |-- Hitman.2007.mkv
|   `-- hitman.2007.info
|-- Thumbs.db
`-- folder.jpg
I rip my own movies for safekeeping but now I'd like to move the mkv in each movie folder ending in -FLX to move the mkv one folder up, so that it is directly located under Some.Movie.Name-FLX instead of Some.Movie.Name-FLX/3.\ Movie/.

I have a lot of movies so it would be handy if someone would know a command to do this with.

Thanks!

Dennis
 
Old 10-06-2009, 12:46 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by FLX View Post
Hello,

I have folders using the following structure:
Code:
root@/volume1/Movies/Hitman.2007-FLX: tree
.
|-- 1. Sample
|   `-- Hitman.2007.[Sample].mkv
|-- 2. Pictures
|   |-- Hitman 1.bmp
|   |-- Hitman 2.bmp
|   |-- Hitman 3.bmp
|   `-- Hitman 4.bmp
|-- 3. Movie
|   |-- Hitman.2007.mkv
|   `-- hitman.2007.info
|-- Thumbs.db
`-- folder.jpg
I rip my own movies for safekeeping but now I'd like to move the mkv in each movie folder ending in -FLX to move the mkv one folder up, so that it is directly located under Some.Movie.Name-FLX instead of Some.Movie.Name-FLX/3.\ Movie/.

I have a lot of movies so it would be handy if someone would know a command to do this with.

Thanks!

Dennis
"One folder up" --

1. Detect the circumstances you want.

2. Move to the target subdirectory (containing the file to be moved).

3.
Code:
$ mv (filename) ..
 
Old 10-06-2009, 12:56 PM   #3
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Quote:
root@/volume1/Movies/Hitman.2007-FLX: tree
You are running as root?
Are you mad?
 
Old 10-06-2009, 01:09 PM   #4
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
Guys come on. He probably means he wants to move all his movies one directory up, and of course as fast and easy as possible (not one by one).
supposing you are in the directory /volume1/Movies as in your example;

Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt
sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash
rm temp1.txt
I know this seems complicated, but I don't think there is a simpler way. By movies you probably spaces and characters you wouldn't use as a regular name in linux, so it gets kind of tricky.

This would put every movie (the one the 1 and 3 directories) on the their upper directories. If you want only the ones on the 3 directory to be moved, you just replace the part that says "ls -Q */*/*mkv" with "ls */3*/*mkv". Also, you may want to see the output of the command first (just to be sure your movies are going to be moved to the right place), taking the "|bash" part out will only show you the command, without moving your movies.

You should put the above lines in a file and run it by
Code:
bash this_file
You can also just copy this line and press enter on your shell
Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt ;sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash ;rm temp1.txt
 
Old 10-06-2009, 01:10 PM   #5
FLX
Member
 
Registered: Apr 2005
Distribution: Fedora Core 5, SuSE 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by lutusp View Post
"One folder up" --

1. Detect the circumstances you want.

2. Move to the target subdirectory (containing the file to be moved).

3.
Code:
$ mv (filename) ..
I mean a command that will travel all my folders recursively, using find for example

Quote:
Originally Posted by tredegar View Post
You are running as root?
Are you mad?
As always.
 
Old 10-06-2009, 01:14 PM   #6
FLX
Member
 
Registered: Apr 2005
Distribution: Fedora Core 5, SuSE 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by rikijpn View Post
Guys come on. He probably means he wants to move all his movies one directory up, and of course as fast and easy as possible (not one by one).
supposing you are in the directory /volume1/Movies as in your example;

Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt
sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash
rm temp1.txt
I know this seems complicated, but I don't think there is a simpler way. By movies you probably spaces and characters you wouldn't use as a regular name in linux, so it gets kind of tricky.

This would put every movie (the one the 1 and 3 directories) on the their upper directories. If you want only the ones on the 3 directory to be moved, you just replace the part that says "ls -Q */*/*mkv" with "ls */3*/*mkv". Also, you may want to see the output of the command first (just to be sure your movies are going to be moved to the right place), taking the "|bash" part out will only show you the command, without moving your movies.

You should put the above lines in a file and run it by
Code:
bash this_file
You can also just copy this line and press enter on your shell
Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt ;sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash ;rm temp1.txt
Thanks for being the only one taking me seriously, posting in the newbie forum does not exactly mean you're a linux newbie

The command worked like a charm!

Regards,

Dennis
 
Old 10-06-2009, 01:25 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by rikijpn View Post
Guys come on. He probably means he wants to move all his movies one directory up, and of course as fast and easy as possible (not one by one).
supposing you are in the directory /volume1/Movies as in your example;

Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt
sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash
rm temp1.txt
I know this seems complicated, but I don't think there is a simpler way. By movies you probably spaces and characters you wouldn't use as a regular name in linux, so it gets kind of tricky.

This would put every movie (the one the 1 and 3 directories) on the their upper directories. If you want only the ones on the 3 directory to be moved, you just replace the part that says "ls -Q */*/*mkv" with "ls */3*/*mkv". Also, you may want to see the output of the command first (just to be sure your movies are going to be moved to the right place), taking the "|bash" part out will only show you the command, without moving your movies.

You should put the above lines in a file and run it by
Code:
bash this_file
You can also just copy this line and press enter on your shell
Code:
ls -Q */*/*mkv|sed -e 'p' -e 's/\(^.*\/\).*\/.*mkv/\1/' >temp1.txt ;sed -e 'N' -e 's/\n/ /g' -e 's/^/mv /g' temp1.txt |bash ;rm temp1.txt
Quote:
I know this seems complicated, but I don't think there is a simpler way.
Another miraculous one-liner (technically two-liner) that serves only to intimidate newbies. And yes, there is a simpler way -- unwrap the code into multiple lines, each of which is comprehensible, modifiable, and maintainable.

The OP can use this one-liner once, for one purpose, one day, then he is well-advised to throw it away.

Or is the real point to look like a genius and make computer programming seem incomprehensible except to those few superior individuals on whom nature has smiled?

Contrary to common belief, computer programming is not an open-ended code obfuscation contest.
 
Old 10-06-2009, 01:35 PM   #8
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
... But FLX is running as root, so he won't mind copying and pasting obscure one- (maybe two-) liners and executing them, will he?
 
Old 10-06-2009, 01:41 PM   #9
FLX
Member
 
Registered: Apr 2005
Distribution: Fedora Core 5, SuSE 10.1
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tredegar View Post
... But FLX is running as root, so he won't mind copying and pasting obscure one- (maybe two-) liners and executing them, will he?
No, I don't because I can understand what the lines mean and it's not like he's trying to forkbomb or rm me. Its not that obfuscated.
This is also a box on a sealed vlan, my rule of thumb is never to use shady commands in a prod env. This however was a private matter and worked well. Give me a break.
 
  


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
Recursively move files michaelsanford Linux - General 5 02-17-2016 01:24 AM
move files of a particular directory visitnag Programming 1 08-24-2008 02:02 PM
how to recursively delete *.xtension files from a directory structure kpachopoulos Linux - General 6 08-24-2008 08:53 AM
how to find files in current directory only (not recursively) babu198649 Linux - Newbie 2 12-27-2007 01:30 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration