Score:-3

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

mk flag

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

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

Irsu85 avatar
cn flag
The extention is in the file name btw
pLumo avatar
in flag
And what did you try? What does `avbd` mean?!
sudodus avatar
jp flag
Please notice that `du` shows the disk usage (in blocks) which is not the same as file size unless you add the option `-b` alias `--bytes`. A block is counted even if only a single byte or a few bytes of it is actually occupied by the file content.
Score:1
ru flag

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.

Score:0
jp flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.