LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Recursively move files one directory up (https://www.linuxquestions.org/questions/linux-newbie-8/recursively-move-files-one-directory-up-760093/)

FLX 10-06-2009 11:56 AM

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

lutusp 10-06-2009 12:46 PM

Quote:

Originally Posted by FLX (Post 3709795)
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) ..

tredegar 10-06-2009 12:56 PM

Quote:

root@/volume1/Movies/Hitman.2007-FLX: tree
You are running as root?
Are you mad?

rikijpn 10-06-2009 01:09 PM

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

FLX 10-06-2009 01:10 PM

Quote:

Originally Posted by lutusp (Post 3709849)
"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 (Post 3709864)
You are running as root?
Are you mad?

As always.

FLX 10-06-2009 01:14 PM

Quote:

Originally Posted by rikijpn (Post 3709890)
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

lutusp 10-06-2009 01:25 PM

Quote:

Originally Posted by rikijpn (Post 3709890)
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.

tredegar 10-06-2009 01:35 PM

... But FLX is running as root, so he won't mind copying and pasting obscure one- (maybe two-) liners and executing them, will he? ;)

FLX 10-06-2009 01:41 PM

Quote:

Originally Posted by tredegar (Post 3709928)
... 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.


All times are GMT -5. The time now is 03:46 PM.