viewing the largest file in the directory using linux? [duplicate] - linux

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed 7 years ago.
1.How to view the largest file in the directory using linux commands.
2.As i followed the following command ls -lh.
3.Is any other way to use the linux commands to view the largest file inside the directory with its size in human readable format.

Try:
$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head
It will give you the top 10 in your directory. And if you just want the largest one:
$ find . -type f | xargs ls -1S | head -n 1
With its "parameters/attributes" (size, permissions, creation date & time):
$ find . -type f | xargs ls -lS | head -n 1
And if youy want to use ls without find try:
$ ls -S . | head -1

ls -Slh | tail +2 | head -1
which uses ls to list your files in size order, long format with human readable sizes. tail +2 removes the first line of your output which is a total size and head gives you the largest file.

I solved the question using this command:
ls -Slh | head -2
This lists by size and selects the first two results.

Related

Listing only name and size of the file with largest size (UNIX) [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed last year.
How can I only list the name and the size of the largest file size in a directory I used this command but It didn't work when I tried on a different directories.
find . -type f -exec ls -lS {} \; | sort -t" " -n -k5 | cut -d" " -f5,10 | tail -n1
This should work
find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head -n 1
the number 1 after head -n means how many of the largest files it'll output and if you want to find files in sub-directories as well you can do that by removing the -maxdepth 1 or changing the number to a larger one.
for more info see the replies in this earlier post: How to find the largest file in a directory and its subdirectories?
For posts about this kind of stuff i suggest tagging them as bash, sh or shell.

Counting files in a huge directory [duplicate]

This question already has answers here:
What is the best way to count "find" results?
(6 answers)
Closed 3 years ago.
Related to this question.
How do I count the number of files in a directory so huge that ls returns too many characters for the command line to handle?
$ ls 150_sims/combined/ | wc -l
bash: /bin/ls: Argument list too long
Try this:
$ find 150_sims/combined/ -maxdepth 1 -type f | wc -l
If you're sure there are no directories inside your directory, you can reduce the command to just:
$ find 150_sims/combined/ | wc -l
If there are no newlines in file names, a simple ls -A | wc -l tells you how many files there are in the directory. Please note that if you have an alias for ls, this may trigger a call to stat (Example: ls --color or ls -F need to know the file type, which requires a call to stat), so from the command line, call command ls -A | wc -l or \ls -A | wc -l to avoid an alias.
ls -A 150_sims/combined | wc -l
If you are interested in counting both the files and directories you can try something like this:
\ls -afq 150_sims/combined | wc -l
This includes . and .., so you need subtract 2 from the count:
echo $(\ls -afq 150_sims/combined | wc -l) - 2 | bc

Find Most Recent File in a Directory That Matches Certain File Size

I need to find the most recently modified file in a directory that matches 3.0 MB.
First Attempt
ls -t /home/weather/some.cool*.file | head -n +1 | grep "3.0M"
Second Attempt
find /home/weather/ -maxdepth 1 -type f -name "some.cool*.file" -size 3M -exec ls -t "{}" +; | head -n +1
Am I close?
I hope this is of some use -
ls -ltr --block-size=MB | grep 3MB
The latest modified files will be displayed at the bottom of the output.
The -r flag shows the output in reverse order and the --block-size=MB will show the size of files in MB.
This should work:
ls -lh --sort=time /path/to/directory/*.file | grep "3.0M" | head -n =1

Bash - Find largest file and print its path [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed 5 years ago.
I need to find the largest file of chosen directory and sub-directories and print the path of that file
I can find the biggest file (I think so)
find . -type f | wc -l
However, I'm stuck on printing the path
find . -type f | xargs ls -l | sort -nk 5 | tail -n1 | awk '{print $NF}'
What i would do :
find . -type f -exec du {} \; | sort -n | awk 'END{$1=""; print}'

Get latest file creation time in Unix [duplicate]

This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 7 years ago.
I've two files FileA and FileB. Can someone please let me know how to get time for latest created file in a folder in Unix?
Both for only two files and the general case of n files, you can use find:
find -type f -printf '%T# \n' | sort -n | tail -1
If the files need to match a pattern, you can use something like:
find -type f -name 'example*.txt' -printf '%T# \n' | sort -n | tail -1
This prints all modification times of files in the working directory, sorts them, then selects the last (largest) one.

Resources