M BUZZ CRAZE NEWS
// news

I need to find all the files with .txt extension with their file size

By Gabriel Cooper

something like du -hs *txt and find 30k avbd

I don't need their extension in output, just size and file name.

3

2 Answers

How about:

IFS=$'\n'
for f in `du -hs *.txt`;do echo $f | sed 's/\(.*\)\.txt/\1/';done

The IFS part is necessary so that the for loop consumes the whole line at the same time. Please also note the backticks around the "du -hs *.txt" part of the command. The backtick button should be above your tab button.

ls:

The easy solution for interactive use is "ell ess minus ell" where column #5 contains the file size in bytes

ls -l *.txt

or if you want 'human readable format'

ls -lh *.txt

You find more details in man ls. Please notice that ls is not recommended for automation (in shellscripts etc).

find:

Your question was vague, so here is a list of commands to find and print text files with the extension txt. Pick the format you want or some combination. You find more details in man find.

The primitive list with only the names of text files in the current directory excluding for example directories and symbolic links but including files in subdirectories

find . -type f -name "*.txt"

A list with size (bytes) and file names

find . -type f -name "*.txt" -printf "%9s '%p'\n"

A list with sizes and names sorted according to size

find . -type f -name "*.txt" -printf "%9s '%p'\n" | sort -n

A list with sizes and names sorted according to name

find . -type f -name "*.txt" -printf "%9s '%p'\n" | sort -k2

A list excluding files in subdirectories with sizes and names sorted according to size

find . -maxdepth 1 -type f -name "*.txt" -printf "%9s '%p'\n" | sort -n

The corresponding list where the dot and extension is removed from each file name

find . -maxdepth 1 -type f -name "*.txt" -printf "%9s '%p'\n"|sed "s/\.txt'$/'/"|sort -n

The corresponding list where the name of the starting-point under which the file was found removed

find . -maxdepth 1 -type f -name "*.txt" -printf "%9s '%P'\n"|sed "s/\.txt'$/'/"|sort -n

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy