Results 1 to 9 of 9
The solution below which I found by some searching
find . -type f -name "*" -print0 | xargs -0 --replace=% mv % `pwd`
works fine at moving all files anywhere ...
- 01-03-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
[SOLVED] Moving files up a directory layer
The solution below which I found by some searching
find . -type f -name "*" -print0 | xargs -0 --replace=% mv % `pwd`
works fine at moving all files anywhere below the current directory to the current directory.
My question is can it be modified so it only moves all the files up one directory layer? Otherwise I shall have to cd 100 or so times and run it in each directory i want to compress...
I imagine the directory below which the file is stored is in the % somewhere it is just a case of extracting it and applying it to the mv command, yes? Sorry I am a very new to Linux command line.
Yours, hopefully
- 01-03-2011 #2Linux Newbie
- Join Date
- Apr 2007
- Posts
- 119
Test is first, but try adding a /.. after the `pwd`
Code:find . -type f -name "*" -print0 | xargs -0 --replace=% mv % `pwd`/..
- 01-03-2011 #3Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
- 01-04-2011 #4
cd into the highest level directory that contains files that you want to move. Then run
the find command you know, the perl splits the command up into the directory path, and the filename, and adds a '../' between them, and a move command. it'll takeCode:find . -type f | perl -pe '(s!(\./.*/)(.*)!mv \1\2 \1../\2!);' | sh
./test_directory2/file2-1
and make it
mv ./test_directory2/file2-2 ./test_directory2/../file2-2
the ` | sh ` just executes the command in the shell.
this will not work if you have newlines in the file or directory names (incredibly stupid, but technically possible,) and I can't guarantee the order of the mv's, so it may move a file from a lower directory, into a higher one, replacing any identically named files.
Perl may be hard to read, but it's great
New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 01-04-2011 #5Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
Thank you Mark, that looks as though it ought to work and indeed does on directories with no spaces in the name but the parameters are not coping with spaces in the directory names. I tried:
in the hope that this might work but it seems the bit before the last space is lost so I getCode:find . -type f -name "*" -print0 | xargs -0 --replace="%" mv "%" `pwd`
when the directory was actually "level 1 2".Code:mv: target `2' is not a directory
I'm not averse to changing the directory names to replace spaces with underscores, but there are near 700 so it would have to be another script...
PS My knowledge of Perl is even less that Linux Terminal!
- 01-04-2011 #6
Spaces are evil .... so very evil .........
ok, I revised the script, you can now run
to see what will be done by the shell (it's just a list of commands that the shell interprets.) If it looks good, pipe it to sh again,Code:find . -type f | perl -pe '(s!(\./.*/)(.*)!mv "\1\2" "\1../\2"!);'
you do NOT need to know perl to see what it is doing. the perl script is just used to make some shell readable commands. You could do the same thing with sed or awk, but I know perl better. It's just a regular expresion substitution.Code:find . -type f | perl -pe '(s!(\./.*/)(.*)!mv "\1\2" "\1../\2"!);' | sh
The output from the first command will look exactly like it does if you were to type it out. It pipes to shell, and interprets it as if you really had typed all of it out.
This will work with spaces in filenames, and SHOULD work will all special characters, EXCEPT newlines. I know for a fact it doesn't with those.New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 01-04-2011 #7Linux Newbie
- Join Date
- Apr 2007
- Posts
- 119
Thanks for the perl. I had planned on using sed this morning to figure it out, but alas, no need.
- 01-04-2011 #8Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
Thanks to both. Job done. Appreciate your help.
- 01-05-2011 #9New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4



