I am using find against a number of directories that have in them the same filenames but different contents. Then, I am copying them to one directory with numerical filenames.

The problem I faced was that the find command reads the folders in different order than the ls command. Making it difficult to correlate the new files to their original directories.

My solution was to run the same find command on the original directories to get them in the same order and number them accordingly.


find . -maxdepth 1 -type d | sed 's#./##' > rename.list
while read -r line
    mv ./"$line" ./"$j"_"$line"
    j=$(( $j + 1))
done < rename.list

This almost solved the problem, but the find command for some reason lists the current working directory as the first output which offsets the numbering by 1.

$ find . -maxdepth 1 -type d | sed 's#./##'

although, the first find commands I ran did not have the same problem:

find . -wholename "*__substg1.0_007D001E*" -wholename "*attach_version*" -type d > header_files.txt

Set the -mindepth option to 1 i.e. -mindepth 1 to exclude the current directory (.) from find's output:

find . -maxdepth 1 -mindepth 1 -type d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.