Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4

    Question [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

  2. #2
    Linux 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`/..

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    Quote Originally Posted by markcole View Post
    Test is first, but try adding a /.. after the `pwd`

    Code:
    find . -type f -name "*" -print0 | xargs -0 --replace=% mv % `pwd`/..
    Sorry it takes them to the directory above the one in which I would run the script...

  4. #4
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    Quote Originally Posted by VirtualOdin View Post
    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
    cd into the highest level directory that contains files that you want to move. Then run

    Code:
     find . -type f | perl -pe '(s!(\./.*/)(.*)!mv \1\2 \1../\2!);' | sh
    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 take

    ./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

  5. #5
    Just 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:
    Code:
    find . -type f -name "*" -print0 | xargs -0 --replace="%" mv "%" `pwd`
    in the hope that this might work but it seems the bit before the last space is lost so I get
    Code:
    mv: target `2' is not a directory
    when the directory was actually "level 1 2".

    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!

  6. #6
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    Quote Originally Posted by VirtualOdin View Post
    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:
    Code:
    find . -type f -name "*" -print0 | xargs -0 --replace="%" mv "%" `pwd`
    in the hope that this might work but it seems the bit before the last space is lost so I get
    Code:
    mv: target `2' is not a directory
    when the directory was actually "level 1 2".

    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!

    Spaces are evil .... so very evil .........

    ok, I revised the script, you can now run

    Code:
     
    find . -type f | perl -pe '(s!(\./.*/)(.*)!mv "\1\2" "\1../\2"!);'
    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"!);' | sh
    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.

    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

  7. #7
    Linux 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.

  8. #8
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    Thanks to both. Job done. Appreciate your help.

  9. #9
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    Quote Originally Posted by VirtualOdin View Post
    Thanks to both. Job done. Appreciate your help.
    You're welcome

    Quote Originally Posted by markcole View Post
    Thanks for the perl. I had planned on using sed this morning to figure it out, but alas, no need.
    I don't imagine it'd be too different with sed, but I've been reading up on / learning perl, so I wanted to use my golden hammer to pound a nail
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...