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
3 Answers
ls -alXh > Option1.txt
man ls would help, also ... ;)
ls -ahlX ~|awk '{printf "%-20s %-40s\n",$9,$5 }'>~/Desktop/Option1.txtSorted 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 columnsBut 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.txtThis one prints file names even if there are spaces.
But it is always a bad idea to parse ls.
4Parsing 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=2Redirecting 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