0

I'm creating a variable listing all filenames in a directory, for later use in a for loop.

OIFS="$IFS"
IFS=$'\n'
images=website/images
dirlist=`find $images -type f`
echo $dirlist

$dirlist currently shows all files correctly and can be used in the subsequent for loop, but also contains the directory names:

website/images/Todiste71.png website/images/Torsion.png website/images/GLP Bewegung Tag 0bis3.gif website/images/Todiste70.png website/images/StanislawSisk.jpg website/images/Hejného metoda graf.jpg website/images/Mathemagicslogo.png website/images/Odddecomp2d.jpg website/images/Fields of Mathematics Ukr.png website/images/Todiste99.png website/images/Todiste72.png website/images/Subdivision rule related to 4-chain alternating link.png website/images/Todiste73.png website/images/Todiste98.png website/images/Todiste88.png website/images/Todiste77.png website/images/Todiste76.png website/images/Todiste89.png website/images/Illustration of the slope of a straight line y=-2x+13.png website/images/Portal Math Banner hy.png website/images/Calculating the slope of the linear equation y=-2x+13 (bold).png website/images/Vingenere 1.png website/images/Todiste74.png website/images/Vertical horizontal.PNG website/images/Curve with tangent line.png website/images/Correlacion.png website/images/Todiste75.png website/images/GradientOfRegularPentagonExteriorAngle.gif website/images/PointToLineDistance v2.png website/images/Sistema anotación alge 005.jpg website/images/Todiste132.png website/images/Todiste126.png website/images/Tree-basis.jpg website/images/1-6 Rossler Skeleton.png website/images/LeibnizInfinitesimalTriangle.png website/images/Calculating the slope of the linear equation y=-2x+13.png website/images/SilverRatioCirclesTangentLine 2.gif website/images/Tangent line versus secant line.png website/images/Histogram sum of length 2 permutations of 1 2 3.svg website/images/Todiste127.png website/images/Todiste133.png website/images/Monodromy action.svg website/images/Todiste125.png website/images/Todiste131.png website/images/Todiste119.png website/images/Mobius shadow.jpg website/images/Rossler Periodic Orbit w2.gif website/images/Todiste118.png website/images/Todiste130.png ...

I need to make the code so that $dirlist just shows images/{filename}, or just the {filename} without the directory names. For instance, the following would be acceptable outputs:

images/Todiste71.png

Todiste71.png

Thanks in advance for the help!

6
  • 1
    "...subsequent for loop..." -- consider using -exec progname {} + or -exec progname {} \; instead of a for-loop, as the former are much more stable with regards to "funny" filenames. Commented Sep 8, 2021 at 9:35
  • Because I think you need some parts of the directory names (subdirectories in your base folder) for the later loop, you also can do a simple cd before the find and a cd - >/dev/null after the find. It's also possible to eliminate the content of your pathvariable (images) with a sed: find $images -type f | sed -e "s#^$images/##" Commented Sep 8, 2021 at 10:10
  • 1
    @GaryKong : Wouldn't it make more sense to use an Array? It's easier to process than a scalar variable holding all the filenames, and you don't run into problems with spaces in a filename. Commented Sep 8, 2021 at 10:33
  • @GaryKong: Removed sh tag, because it is irrelevant to your question. Commented Sep 8, 2021 at 10:42
  • Does Remove path from find command output answer your question? Commented Sep 13, 2021 at 14:27

1 Answer 1

2

Use basename to remove the directory.

Or use the printf option of find.

find "$images" -type f -printf '%f\n'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Used basename and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.