I need to find all the files with .txt extension with their file size
something like du -hs *txt and find 30k avbd
I don't need their extension in output, just size and file name.
32 Answers
How about:
IFS=$'\n'
for f in `du -hs *.txt`;do echo $f | sed 's/\(.*\)\.txt/\1/';doneThe 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 *.txtor if you want 'human readable format'
ls -lh *.txtYou 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 -nA list with sizes and names sorted according to name
find . -type f -name "*.txt" -printf "%9s '%p'\n" | sort -k2A 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 -nThe 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 -nThe 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