What linux command should I use to get a long list of items in current directory, sorted by size such that the biggest items appear last? - linux

On Linux, how do I derive a long list of items sorted by size (largest to smallest)?
Is this correct:
$ ls -laShr /var/~

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size).
$ ls -laS /var/~
And to sort in reverse order, add the -r flag as follows.
$ ls -laShr /var/~

U can use
$ ls -lShr /var/
l- for long listing
S- for sorting by size(default descending)
h- for human-readable format
r- for a reverse sort

Related

Unix: Sort 'ls' by return value of program

how can I use program as a key to sort in Unix shell? In other words to sort output of 'ls' (or any other program) by return value of a program applied on each line.
I'll give two example solutions:
A one-line command that is simpler and therefore something I'd try use first.
A bash script that allows sorting a list by output from an arbitrary bash function that reads each line of the list as input.
Example 1 (without executing command on each line)
If the question is how to, in general, sort outputs of programs like ls, below is an example specific to ls that sorts by inode. However, every program may have its own idiosyncrasies when generating its output so this example may have to be adapted:
ls -ail /home/user/ | tail -n+2 | tr -s ' ' | sort -t' ' -k1,1 -g
Here are the different parts of this command broken down:
ls -ail /home/user/
Lists all (-a) files in directory /home/user/ in list (-l) format with inode (-i).
tail -n+1
Cuts off first line from ls output.
tr -s ' '
Combines (-s) multiple spaces (' ') for sort.
sort -t ' ' -k 1 -g
Sorts list by first (1) field of integers (-g) separated by one space (' ').
Example 2 (executing command with each line as input)
Here is a more adaptable example in a bash script I worked up to show how the list of files generated from ls -a1 can be fed into bash function getinode which uses stat to output the inode for each file. A while loop repeats this process for each file, saving in comma-delimited format the data by repeatedly appending a variable named OUTPUT which at the end is sorted by sort using the first field.
The important part is that the function getinode can be anything, so long as it outputs a string. I set up getinode to receive a file path as input (first argument $1) and to then output the inode to stdout via echo $INODE. The script calls getinode via $(getinode "$FILEPATH").
#!/bin/bash
# Usage: lsinodesort.sh [file]
# Refs/attrib:
# [1]: How to sort a csv file by sorting on a single field. https://stackoverflow.com/a/44744800
# [2]: How to read a while loop variable. https://stackoverflow.com/a/16854326
WORKDIR="$1" # read directory from first argument
getinode() {
# Usage: getinode [path]
INODE="$(stat "$1" --format=%i)"
echo $INODE
}
if [ -d "$WORKDIR" ]; then
LINES="$(ls -a1 "$WORKDIR")" # save `ls` output to variable LINES
else
exit 1; # not a valid directory
fi
while read line; do
path="$WORKDIR"/"$line" # Determine path.
if [ -f "$path" ]; then # Check if path is a file.
FILEPATH="$path"
FILENAME="$(basename "$path")" # Determine filename from path.
FILEINODE=$(getinode "$FILEPATH") # Get inode.
OUTPUT="$FILEINODE"",""$FILENAME""\n""$OUTPUT" ; # Append inode and file name to OUTPUT
fi
done <<< "$LINES" # See [2].
OUTPUT=$(printf "${OUTPUT}" | sort -t, -k1,1) # sort OUTPUT. See [1]
OUTPUT="inode","filename""\n""$OUTPUT"
printf "${OUTPUT}\n" # print final OUTPUT.
When I run it on my own home folder I get output like this:
inode,filename
3932162,.bashrc
3932165,.bash_logout
3932382,.zshrc
3932454,.gitconfig
3933234,.bash_aliases
3933512,.profile
3933612,.viminfo
I'm not sure to understand your question, so I'll try to rephrase it first.
If I'm not mistaken, you want to sort the output of a program (it may be ls or any other command in a Unix shell).
I'll suggest using the pipeline feature available on Unix shell.
For instance, you can sort the output of the ls command using :
ls /home | sort
This feature is available but not limited to the ls command.
By the way, there are optional flags you can use for sorting ls command results if that's your specific use case :
ls -S # for sorting by file size
ls -t # for sorting by modification time
You can also append the --reverse or -r flag for displaying the result in reverse order.
As for the sort function, there are also flags allowing to customize your result as per your needs :
sort -n # for sorting numerically instead of alphabetically
sort -k5 # for sorting based on the 5th column
sort -t "," # for using the comma as a field separator
You can combine all of them like that for sorting the output of ‘ls -l‘ command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric/alphabetically).
ls -l /home/$USER | sort -t "," -nk2,5 -k9
sort function examples

How to pass a list of files to parallel command and execute downstream command such as samtools?

I have a list of files which I want to sort and index ,i listed all those files in a text file.
/run/media/punit/data1/GSE74246/tophat_output/CMP_SRR2753096/CMP_6792.bam
run/media/punit/data1/GSE74246/tophat_output/CMP_SRR2753104/CMP_7256.bam
The above one is just a list of my data which i want to sort and index.
Now i want to use this command
ls *.bam | parallel "samtools view -b -S {} | samtools sort - {.}; samtools index {.}.bam"
Meanwhile I have also files with
.bam
extension such as unmapped.bam which i dont want to sort and index
How can i exclude those "unmapped.bam" but since i dont have those unmapped.bam in my list but still i wonder if i use parallel then would it take those sort and index...
ls *.bam | grep -v unmapped | parallel ...

How to see last created 3 files in a directory without passing date parameters

How to list the last three new files. it should be sorted on the basis of the lat created status?.
You can use following
$ ls -rt /tmp/ | tail -3
ls will list all the files
-r reverse order while sorting
-t sort by modification time, newest first
tail will show you 10 lines by default from last
-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth

Creating Linux Alias to list only Directories

I'm wanting to create an alias called dir for KSH that will show only the sub-directories in a cluttered directory with many files and directories.
I can output a single-column list of the directories, but I am having a problem converting the single-column list into a multi-column.
I want it to automatically determine the number of columns to use based on the length of the longest string and the width of the terminal window in the same way that the ls command automatically creates tabbed columns.
Say I type the following at the terminal to give me a list of all the directories in the current directory:
ls -1Fa | grep /
When I pipe this output to column -t, it doesn't seem to have any effect... I get a list of 54 directories in a single column. I've also tried the common suggestion in other posts of piping to column -t -s $'\t' or column -t -s $'\n', but it always results in a single list output. I've looked at the man pages and tried all the options, but I cannot get it to produce output similar to ls.
I've also tried piping the output to pr with the following:
alias dir="ls -1Fa | grep / | pr -T -w $COLUMNS -6"
Unfortunately, this will always output 6 columns and if the terminal window is rather skinny (i.e. $COLUMNS is something small like 50) then long directory names are truncated at the beginning of the next column.
Can somebody answer why column is not outputting the list into columns, or does somebody have a better solution using another tool such as sed, awk, perl?
Thanks for your help!
Really, all you need is
alias dir='ls -d */ .*/'
The trailing slash instructs ksh to only include directories in the glob expansion. And you get to piggy-back on how ls formats into columns.
The answer to your question is that you're using the wrong option for column:
ls -1d */ .*/ | column -c $(tput cols)
column -t pretty-prints the output, from
1
a b
foobar baz
into
1
a b
foobar baz
See the column man page

How to view last created file?

I have uploaded a file to a Linux computer. I do not know its name. So how to view files through their last created date attribute ?
ls -lat
will show a list of all files sorted by date. When listing with the -l flag using the -t flag sorts by date. If you only need the filename (for a script maybe) then try something like:
ls -lat | head -2 | tail -1 | awk '{print $9}'
This will list all files as before, get the first 2 rows (the first one will be something like 'total 260'), the get the last one (the one which shows the details of the file) and then get the 9th column which contains the filename.
find / -ctime -5
Will print the files created in the last five minutes. Increase the period one minute at a time to find your file.
Assuming you know the folder where you'll be searching it, the most easy solution is:
ls -t | head -1
# use -A in case the file can start with a dot
ls -tA | head -1
ls -t will sort by time, newest first (from ls --help itself)
head -1 will only keep 1 line at the top of anything
Use ls -lUt or ls -lUtr, as you wish. You can take a look at the ls command documentation typing man ls on a terminal.

Resources