2

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.

#!/bin/sh

find . -maxdepth 1 -type d | sed 's#./##' > rename.list
j=1
while read -r line
do
    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#./##'
.
dir2
dir1
dir3

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

1 Answer 1

2

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
1
  • 1
    This was exceedingly difficult to find the words to Google for, thank you very much Commented Aug 17, 2022 at 0:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.