How do I name one random file in the deepest level of a directory tree using basic Bash commands and script?
I was searching for a way to do it as an one-liner and without functions. Assuming also that I'm starting in the current directory and then work towards the depth.
find
, grep
, sed
and awk
are acceptable commands.
My current attempt looks like this and does not work:
find -type d | declare COUNT=-1; declare P=""; while read LINE ; do echo $LINE; declare C; C=$(echo $LINE | cut -c3- | sed "s/\//\n\//g" | grep '/' -c); echo $C; if [ $COUNT -gt $C ]; then let COUNT=$C; let P=$LINE; echo "Done"; fi; done
This would only find the directory.
How could this be solved in the most simple way?
Best Answer
That's an odd request!
I'd use
find
+awk
to grab a file in the deepest directory:Using
${deepest}
in yourmv
command is left as an exercise but the following five lines may help you further:Following update to question:
By supplying
-type f
tofind
to find all files (f
), not all directories (d
).