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

  • Home
  • SEARCH
  • 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 6100677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:51+00:00 2026-05-23T13:24:51+00:00

I want to write a script in bash, that produces a list of a

  • 0

I want to write a script in bash, that produces a list of a directory into a file. It is necessary to mark every line as file or directory.

This is my unfinished attempt:

#!/bin/bash
if [ $# -eq 1 ]
then
 if [ -d $1 ]
 then
  touch liste.txt
  ls -l $1 | grep '^-' >> liste.txt
  ls -l $1 | grep '^d' >> liste.txt
 fi
fi

now I don t know how to print in every line “file” or “directory”. Maybe there is a more elegant way to solve this.

Greetings,
Haniball

Thanks Pavium,

here the finished script:

#!/bin/bash
if [ $# -eq 1 ]
then
 if [ -d $1 ]
 then
  rm liste.txt
  touch liste.txt
  ls -l $1 | grep '^-' | sed -e "s/^-/File /g" >> liste.txt
  ls -l $1 | grep '^d' | sed -e "s/^d/Directory /g" >> liste.txt
 fi
 more liste.txt
fi

I am sure that there a more elegant solution. Maybe grep can be thrown out, but I had to restrict the output to only lines that match the pattern.

Greetings,
Haniball

  • 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-23T13:24:52+00:00Added an answer on May 23, 2026 at 1:24 pm

    You can use sed as pavium already said.

    ls -l $1 | grep '^-' | sed 's/^/file: /' >> liste.txt
    ls -l $1 | grep '^d' | sed 's/^/directory: /' >> liste.txt
    

    Or in one command:

    ls -l $1 | sed -n -e '/^-/{s/^/file: /p;d;}' -e '/^d/{s/^/directory: /p;d;}' > liste.txt
    

    Or you can do something different:

    for f in $1/* ; do 
      if [ -d "$f" ]; then
        echo "directory: $f" >> liste1.txt
      else
        echo "file: $f" >> liste1.txt
      fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a bash script that I can run in a directory
I want to write a sh/bash script that can determine whether a particular directory
I want to write a php script that keeps the apache_log file open and
I'm trying to write a bash script that can edit itself. The line of
I want to write a simple bash script that will act as a wrapper
I want to write a bash script that sorts the input by rules in
So, I want to write a bash script that are a sequence of steps
I want to write in a bash script a piece of code that checks
I want to write a bash script that (recursively) processes all files of a
I want to write a bash script that will execute one command in the

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.