0

How can i create a new file called Option1.txt on my desktop. This file should contain the contents of my 'home' directory. And how would i make my output is:

  • Sorted alphabetically by entry extension.
  • Includes any hidden files and folders.
  • Shows all of the file sizes in human readable format
user264934
  • 1,395
  • 3
  • 9
  • 5

3 Answers3

3

ls -alXh > Option1.txt

man ls would help, also ... ;)

Fred B.
  • 116
  • 1
  • 5
3
ls -ahlX ~|awk '{printf "%-20s %-40s\n",$9,$5 }'>~/Desktop/Option1.txt

Sorted alphabetically by entry extension.(Using ls -X)

Includes any hidden files and folders.(Using ls -a)

Shows all of the file sizes in human readable format(Using ls -lh)

awk '{print $9,$5 } just prints the name and size of files and directories

and finally stores in Option.txt.

%-20s %-40s\n is for formatting the columns But this will not print full directory name, as pointed out in the comments by @ Glenn Jackman ,if there are spaces in directory name.

So,

Another option would be (source)

ls -ahlX ~|gawk -F':[0-9]* ' '/:/{print $2}'>~/Desktop/Option1.txt

This one prints file names even if there are spaces.

But it is always a bad idea to parse ls.

Stormvirux
  • 4,466
0

Parsing output of ls is never a good idea. My approach would be to use du to list disk usage for each file, with --mad-depth option to only list current directory (because du is recursive) and then set sort command organize output based on the 2nd field.

 du --all --max-depth 1 --human-readable | sort --key=2

Redirecting that to a file then is simply du --all --max-depth 1 --human-readable | sort --key=2 > outputFileName.txt

You also may be interested in more verbose info than provided by du. Refer to my other answer that utilizes find and du ( although it may need to be modified to include | sort part ): Command 'ls' showing directory size instead of block-size

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497