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

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.

Related

Search entire server for a certain file type larger than 1GB in size? [duplicate]

This question already has answers here:
Find files with a certain extension that exceeds a certain file size
(3 answers)
Closed 3 years ago.
I have the following linux command I use to determine if a directory is larger than 1GB in size:
du -sh * | sort -hr | awk '$1 ~ /[GT]/
How would I modify this to instead search for any file that has a certain file type, such as .log filetype?
Better use find :
find . -type f -name '*.log' -size +1G
sudo find /www-data -name "*.log" -type f -size +1000000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' | sort

Counting number of files in a directory with an OSX terminal command

I'm looking for a specific directory file count that returns a number. I would type it into the terminal and it can give me the specified directory's file count.
I've already tried echo find "'directory' | wc -l" but that didn't work, any ideas?
You seem to have the right idea. I'd use -type f to find only files:
$ find some_directory -type f | wc -l
If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:
$ find some_directory -maxdepth 1 -type f | wc -l
Open the terminal and switch to the location of the directory.
Type in:
find . -type f | wc -l
This searches inside the current directory (that's what the . stands for) for all files, and counts them.
The fastest way to obtain the number of files within a directory is by obtaining the value of that directory's kMDItemFSNodeCount metadata attribute.
mdls -name kMDItemFSNodeCount directory_name -raw|xargs
The above command has a major advantage over find . -type f | wc -l in that it returns the count almost instantly, even for directories which contain millions of files.
Please note that the command obtains the number of files, not just regular files.
I don't understand why folks are using 'find' because for me it's a lot easier to just pipe in 'ls' like so:
ls *.png | wc -l
to find the number of png images in the current directory.
I'm using tree, this is the way :
tree ph

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

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.

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.

Bash script that writes subdirectories who has more than 5 files

while I was trying to practice my linux skills, but I could not solve this question.
So its basically saying "Write a bash script that takes a name of
directory as a command argument and printf the name of subdirectories
that has more than 5 files in it."
I thought we will use the find command but ı still could not figure it out. My code is:
find directory -type d -mindepth5
but it's not working.
You can use find twice:
First you can use find and wc to count the number of files in a given directory:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | wc -l)
This just asks find to output an x on a line for each file in the directory directory, proceeding non-recursively, then wc -l counts the number of lines, so, really, nb is the number of files in directory.
If you want to know whether a directory contains more than 5 files, it's a good idea to stop find as soon as 6 files are found:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l)
Here nb has an upper threshold of 6.
Now if for each subdirectory of a directory directory you want to output the number of files (threshold at 6), you can do this:
find directory -type d -exec bash -c 'nb=$(find "$0" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); echo "$nb"' {} \;
where the $0 that appears is the 0-th argument, namely {} that find will replaced by the subdirectory of directory.
Finally, you only want to display the subdirectory name if the number of files is more than 5:
find . -type d -exec bash -c 'nb=$(find "$0" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); ((nb>5))' {} \; -print
The final test ((nb>5)) returns success or failure whether nb is greater than 5 or not, and in case of success, find will -print the subdirectory name.
This should do the trick:
find directory/ -type f | sed 's/\(.*\)\/.*/\1/g' | sort | uniq -c | sort -n | awk '{if($1>5) print($2)}'
Using mindpeth is useless here since it only lists directories at at least depth 5. You say you need subdirectories with more then 5 files in it.
find directory -type f prints all files in subdirectories
sed 's/\(.*\)\/.*/\1/g' removes names of files leaving only list of subdirecotries without filenames
sort sorts that list so we can use uniq
uniq -c merges duplicate lines and writes how many times it occured
sort -n sorts it by number of occurences (so you end up with a list:(how many times, subdirectory))
awk '{if($1>5) print($2)}' prints only those with first comlun 1 > 5 (and it only prints the second column)
So you end up with a list of subdirectories with at least 5 files inside.
EDIT:
A fix for paths with spaces was proposed:
Instead of awk '{if($1>5) print($2)}' there should be awk '{if($1>5){ $1=""; print(substr($0,2)) }}' which sets first part of line to "" and then prints whole line without a leading space (which was delimiter). So put together we get this:
find directory/ -type f | sed 's/\(.*\)\/.*/\1/g' | sort | uniq -c | sort -n | awk '{if($1>5){ $1=""; print(substr($0,2)) }}'

Resources