Well, I hope this is the right forum, first of all.
Here's my issue:
I have a lot of images stored in subfolders like this example: /Stallone/photos or /Aguilera/photos. I'd like to move all the images from the photos folders one folder up, if possible. I'm hoping maybe there's a SSH command to do this.
Basically, in the example above, I want to move all the Aquilera's images from /Aquilera/photos folder to /Aguilera folder.
Does anyone know how to do this?
I can't do it by copy/paste, there are to many folders.

$ pushd /Stallone/photos
$ mv * ../
$ popd
$ pushd /Aguilera/photos
$ mv * ../
$ popd

Of course, this moves everything in the photos directory up one level - including other directories. You could limit that with globbing. For instance mv *.png ../ would only move files ending in .png up one level.

Sorry, maybe I didn't explained very well: folders like /Stallone/photos I have thousands and I can do this for any of them - it will take the same time like browsing and copy/paste (forever?!)
I was hoping that there's a 'magic' command that can do this for all the /Stallone/photos like folders at once :)

Let me make sure I understand. You have possibly thousands of folders that all contain a subdirectory called photos . In that subdirectory there are possibly many more image files you would like to have moved up one directory.

First, I'm not sure what you get out of that, it's not a very meaningful change - but your intentions don't necessarily need to be clear to me.

You can do this with find coupled with a script. Consider foo.sh :

#!/bin/bash

pushd $1
mv * ../
popd

Then you can execute - from some directory above the ones you want to locate - the following:

$ find . -name \*photos -type d -exec ./foo.sh '{}' \;

The same caveat holds about the mv in that script: you will have to modify it to suit your needs. Also, if there are other directories named photos under that path that do not contain photos like you describe then they will be affected and you will need a way to filter them out.

OK, it works but only if the directory doesn't contain spaces. For example, for /Stallone/photos works but for /Silvester Stallone/photos it doesn't. It will say:

./foo.sh: line 3: pushd: ./A: No such file or directory
mv: cannot stat `*.jpg': No such file or directory
./foo.sh: line 5: popd: directory stack empty

By the way, I've replaced mv * ../ with mv *.jpg ../
Is there a way to fix this?
Thank you!

ok, I got it.... for directories containing empty spaces or special characters pushd doesn't work, unless you put the directory inside quotes. So I modified pushd $1 with pushd "$1" and seems to do the trick.
Thanks a lot for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.