linux - bash - remove all directories (and contents) but not files in pwd

06
2014-04
  • atomh33ls

    I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:

    mydir1
    mydir2
    myfile1
    myfile2
    

    then I'd like to be left with just

    myfile1
    myfile2
    

    I assume that I need to use rm -r -i

    Am I correct?

  • Answers
  • fede.evol

    No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.

    I'd give a try to:

    find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} \;

    The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.

  • Martin

    I found this one somewhere:

    rm -r */
    

    Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...

  • Matthew Williams

    Something like this should work:

    find /path -type d -exec rm -rf '{}' \;

    -type d looks for only directories

  • WeSee

    Use

    rm -rf ./*/
    

    That avoids interactive mode an deletes only directories in your local directory.


  • Related Question

    Bash script: rename all files below a directory?
  • Richard

    Here's what I'd like to do in pseudocode:

    for subdir in [all first-level subdirectories of the current directory]:
        for file in [all files in subdir]:
            rename file to "myprefix_" + current_filename_padded_with_zeroes
    

    What I mean by current_filename_padded_with_zeroes is e.g. if the current filename is 01.png change to 0001.png, or 100.png change it to 0100.png.

    Can anyone help me translate the above into a bash script?

    Something like... I'm not sure how to do the renaming part:

    #!/bin/bash
    for DIR in $(ls) 
    do
        for FILENAME in $(ls $DIR)
            do
            mv "$FILENAME" "myprefix_{%FILENAME}"   
            done
    done
    

  • Related Answers
  • Daniel Beck

    Use the following to rename all files in the current directory:

    for file in * ; do
        mv "$file" "myprefix_$( printf "%04d" $file )"
    done
    

    The printf program is similar to the function of the same name in quite a few programming languages.


    You need to use basename and dirname to make it work from a different directory.

  • Jens Erat

    Use printf to pad zeros until it matches the length you want. %04d means filling zeros to a length of 4 digits (if your number is >= 4 digits, it won't change anything). ${a%.png} matches the part of $a before .png. Some basename and dirname is required to extract the actual filename from the path and reconcatenate it.

    You can replace the loop using find . -dept 2.

    find . -depth 2 -exec bash -c 'FILENAME=`basename {}`; mv {} `printf \`dirname {}\`/%04d.png ${FILENAME%.png}`'  \;