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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:00:43+00:00 2026-05-18T01:00:43+00:00

How can i execute a certain command for every file/folder in the current folder?

  • 0

How can i execute a certain command for every file/folder in the current folder?

I’ve started with this as a base script, but this seems that its only working when using temporary files, and i dont really like the ideea. Is there any other way?

FOLDER=".";
DIRS=`ls -1 "$FOLDER">/tmp/DIRS`;

echo >"/tmp/DIRS1";
while read line ; do
    SIZE=`du "$FOLDER$line"`;
    echo $SIZE>>"/tmp/DIRS1";
done < "/tmp/DIRS";

For anyone interested, i wanted to make a list of folders, sorted by their size. Here is the final result

FOLDER="$1";
for f in $FOLDER/*; do 
  du -sb "$f";
done | sort -n | sed "s#^[0-9]*##" | sed "s#^[^\./]*##" | xargs -L 1 du -sh | sed "s|$FOLDER||";

which leads to du -sb $FOLDER/* | sort -n | sed "s#^[0-9]*##" | sed "s#^[^\./]*##" | xargs -L 1 du -sh | sed "s|$FOLDER||";

  • 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-18T01:00:43+00:00Added an answer on May 18, 2026 at 1:00 am

    Perhaps xargs, which reinvokes the command specified after it for each additional line of parameters received on stdin…

    ls -1 $FOLDER | xargs du
    

    But, in this case, why not…

    du *
    

    …? Or…

    for X in *; do
        du $X
    done
    

    (Personally, I use zsh, where you can modify the glob pattern to only find say regular files, or only directories, only symlinks etc – I’m pretty sure there’s something similar in bash – can dig for details if you need that).

    Am I missing part of your requirement?

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

Sidebar

Related Questions

I have a certain PHP script that calls exec() to execute a command to
VB 6: How can I execute a .bat file but wait until its done
In cmd.exe, I can execute the command copy c:\hello.txt c:\hello2.txt and it worked fine.
From my understanding, Lua is an embeddable scripting language that can execute methods on
How can I execute a.exe using the Cygwin shell? I created a C file
How can I execute a terminal command (like grep ) from my Objective-C Cocoa
I have a stored procedure that returns multiple tables. How can I execute and
I coded up 4 .java files. The thing is that I can only execute
I've noticed that certain command cause LINQtoSQL to connect to the database and download
I've written a small console application that can perform certain tasks. The user interface

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.