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 8961221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:50:16+00:00 2026-06-15T15:50:16+00:00

I want to read all file names form a particular directory and then create

  • 0

I want to read all file names form a particular directory and then create new files with those names by appending some string to them in another directory.

e.g > ‘A’, ‘B’, ‘C’ are in ‘logs’ directory
then script should create ‘A_tmp’, ‘B_tmp’, ‘C_tmp’ in ‘tmp’ directory

what i am using is –

tempDir=./tmp/
logDir=./logs/

for file in $( find `echo $logDir` -type f )
 do
      name=eval basename $file
      echo $name
      name=$(echo $name | sed 's/.$//')
      echo $tempDir
      opFile=$tempDir$name
      echo $opFile
 done

But what I understood is, $file is containing ‘\n’ as last character and I am unable to concatenate the string.

right now I am not creating files, just printing all the names.

So, how I can remove the ‘\n’ from the file name, and is my understanding correct ?

  • 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-06-15T15:50:18+00:00Added an answer on June 15, 2026 at 3:50 pm

    Analysis

    There are multiple issues to address in your script. Let’s take it step by step:

    tempDir=./tmp/
    logDir=./logs/
    
    for file in $( find `echo $logDir` -type f )
    

    This scheme assumes no spaces in the file names (which is not an unusual restriction; avoiding problems with spaces in names is relatively tricky). Also, there’s no need for the echo; just write:

    for file in $(find "$logDir" -type f)
    

    Continuing:

    do
        name=eval basename $file
    

    This runs the basename command with the environment variable name set to the value eval and the argument $file. What you need here is:

        name=$(basename "$file")
    

    where the double quotes aren’t strictly necessary because the name can’t contain spaces (but it’s not a bad habit to get into to quote all file names because sometimes the names do contain spaces).

        echo $name
    

    This would echo a blank line because name was not set.

        name=$(echo $name | sed 's/.$//')
    

    If name was set, this would chop off the last character, but if the name was A, you’d have nothing left.

        echo $tempDir
        opFile=$tempDir$name
        echo $opFile
    done
    

    Give or take double quotes and the fact that you’ve not added the _tmp suffix to opFile, there’s nothing wrong with the rest.

    Synthesis

    Putting the changes together, you end up with:

    tempDir=./tmp/
    logDir=./logs/
    
    for file in $(find "$logDir" -type f)
    do
        name=$(basename "$file")
        echo "$name"                    # Debug only
        echo "$tempDir"                 # Debug only
        opFile="$tempDir${name}_tmp"
        echo "$opFile"
    done
    

    That shows all the intermediate results. You could perfectly well compress that down to:

    tempDir=./tmp/
    logDir=./logs/
    
    for file in $(find "$logDir" -type f)
    do
        opFile="$tempDir"$(basename "$file")"_tmp"
        echo "$opFile"
    done
    

    Or, using a simpler combination of double quotes because the names contain no spaces:

    tempDir=./tmp/
    logDir=./logs/
    
    for file in $(find "$logDir" -type f)
    do
        opFile="$tempDir$(basename $file)_tmp"
        echo "$opFile"
    done
    

    The echo is there as a surrogate for the copy or move operation you plan to execute, of course.

    EDIT: …and to remove restrictions on file names containing spaces and globbing characters, do it as:

    tempDir=./tmp/
    logDir=./logs/
    
    find "$logDir" -type f |
    while IFS= read -r file
    do
        opFile="${tempDir}${file##*/}_tmp"
        echo "$opFile"
    done
    

    It will still fail for file names containing newlines. If you want to handle that then investigate a solution using find ... -print0 | xargs -0 or find ... -exec.

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

Sidebar

Related Questions

Basically I want to read and write to a file then save all the
I want to list out all the file names in the public/images directory for
i want to read from a text file in C#. But I want all
I want to read a folder for all the files and programmatically print them
I have a File.aspx form where I display all the files. When I upload
Currently I want to read some data (metadata, scene names, mesh count, vertices count
I'm fairly new to python scripting and I want to verify file names in
i want to upload files(all format) to a server and store the file location
I want to read all the contacts from the phone but i am unable
I just want what my title says.I have already read all the previous similar

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.