Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:57:23+00:00 2026-05-31T09:57:23+00:00

How do I list newest directory and add as variable to bash script to

  • 0

How do I list newest directory and add as variable to bash script to process files recursively

ls -t1 | head -n1

Works perfectly to list the latest directory, but I want to add that directory name to my script so I can process the files within using the following script:

#!/bin/bash
ls | while read -r FILE
do
    mv -v "$FILE" `echo $FILE | tr ' ' '_' `
done
ls | while read -r FILE
do
    mv -v "$FILE" `echo $FILE | tr '\*.JPEG' '\*.jpg' `
done
    mogrify -resize 750 *.jpg
wait 
    jpegoptim *.jpg –max=70 --strip-all
exit

I also want to process the files recursively, there might be at most one level of sub directories.

Basically keep the bash script at the root of the directory and process all latest directories and sub directories files.

OK I modified the script to this:

#!/bin/bash
DIR=ls -t1 | head -n1
ls $DIR | while read -r FILE
do
    mv -v "$FILE" `echo $FILE | tr ' ' '_' `
done
ls $DIR | while read -r FILE
do
    mv -v "$FILE" `echo $FILE | tr '\*.JPEG' '\*.jpg' `
done
    mogrify -resize 750 $DIR/*.jpg
wait 
    jpegoptim $DIR/*.jpg –max=70 --strip-all
exit

But it does not seem to recognise the $DIR variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:57:25+00:00Added an answer on May 31, 2026 at 9:57 am

    This bash script will rename and convert the jpg files in the newest directory in the current directory and the files in the first level of directories under that directory.

    #!/bin/bash
    
    FIRST_DIR=`ls -t1F | grep / | head -n1`
    DIR="./${FIRST_DIR}"
    ls -t1F $DIR | while read -r FILE
    do
        if [ "$FILE" ]
        then
            if [[ $FILE = */ ]]
            then
                echo "here ${DIR}${FILE}."
                DEEP_DIR="${DIR}${FILE}"
                ls -t1 $DEEP_DIR | while read -r FILE2
                do
                    if [ "$FILE2" ]
                    then
                        if [[ $FILE2 != */ ]]
                        then
                            RENAME=`echo ${FILE2//\*/} | tr ' ' '_' `
                            mv -v "${DEEP_DIR}${FILE2//\*/}" "${DEEP_DIR}${RENAME}"
                            FILE2=$RENAME
    
                            RENAME2=`echo ${FILE2//\*/} | tr '\*.JPEG' '\*.jpg' `
                            mv -v "${DEEP_DIR}${FILE2//\*/}" "${DEEP_DIR}${RENAME2}"
                            FILE2=$RENAME2
                        fi
                    fi
                done
    
                mogrify -resize 750 "$DEEP_DIR*.jpg"
                wait 
                jpegoptim "$DEEP_DIR*.jpg" –max=70 --strip-all
            fi
            if [[ $FILE != */ ]]
            then
                RENAME=`echo ${FILE//\*/} | tr ' ' '_' `
                mv -v "${DIR}${FILE//\*/}" "${DIR}${RENAME}"
                FILE=$RENAME
    
                RENAME2=`echo ${FILE//\*/} | tr '\*.JPEG' '\*.jpg' `
                mv -v "${DIR}${FILE//\*/}" "${DIR}${RENAME2}"
                FILE=$RENAME2   
            fi
        fi
    done
    if [ "$FIRST_DIR" ]
    then
        mogrify -resize 750 "$DIR*.jpg"
        wait 
        jpegoptim "$DIR*.jpg" –max=70 --strip-all
    fi
    

    Here are a couple of good links about bash programming:

    http://tldp.org/LDP/abs/html/comparison-ops.html

    http://tldp.org/LDP/abs/html/string-manipulation.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the newest files/directory on my FTP server (updated today), I've
Basically I have a directory of DWG files stored in a List that I
List<tinyClass> ids = new List<tinyClass(); ids.Add(new tinyClass(1, 2)); bool b = ids.IndexOf(new tinyClass(1, 2))
List comprehensions can be useful in certain situations, but they can also be rather
List<Foo> fooList = Session[foo] as List<Foo>; fooList.Add(bar); Does the call to Add() change the
I am trying to write a script that will parse a directory, find the
I have a method which reads the content from files located in a directory.
I have made a list based navigation bar for my newest project and I
I've been thinking about how to display the newest array (because the array list
I have a script that creates a zip package daily in a directory. After

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.