MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
files
Search

Using Terminal to Find Large Files and Folders

Tuesday January 18, 2022. 05:00 PM , from MacMost
You can use the Terminal to list files and folders, restrict the lists to only large files and folders, and also just show the ones that use the most space.


Check out Using Terminal to Find Large Files and Folders at YouTube for closed captioning and more options.
Video Transcript: Hi, this is Gary with MacMost.com. Let me show you how to use the Terminal to find the largest files and folders on your drive.
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
So while you can use the Finder to find large files it's a little more difficult to find large folders. It can be faster to do both of those things using the Terminal. So when you launch the Terminal you get a window that looks like this. Now when you're working in Terminal you are always at a certain location. You can use pwd to get that location. In this case I'm in the Users Folder/MacMost. So the top level of my Home directory. You want to start off at a good location for finding files. So starting at the top level isn't very good because you're going to get System files, Library files, Application files, things like that. It's better to start off at a place like your Home folder or maybe your Documents folder. If I were to list the files here, ls for list, then I would see all of the folders in my Home folder. I could then go down into the Documents folder by typing cd and then Documents to go down into it. But you could also type cd and the full path to any place and you could use the Finder as a way to get a quick shortcut to that.
So, for instance, here I've got a Finder window and there's my Documents folder which is where I want to go. So all I need to do here is type cd space and instead of typing the full path if I just drag and drop the folder into the Terminal window anywhere then it will insert the full path. Now I can type Return just as if I typed out all those characters. So now I am at the Documents folder. I can list and see everything there. There's a bunch of subfolders there. If I want a better list I can do ls and then dash l to list everything this way. I'll get to see things like a d here next to anything that is a directory or folder. These are all folders here. I'll also see the file size. Now the file sizes don't make sense for folders.
Let's go into a subfolder here. Like, for instance, let's go into the Projects Folder. Now I can do ls and see there's a bunch of files there. ls-l will give me a full listing. d will denote that this is a subfolder. Anything that's a regular file is a dash there. Then here now the file sizes make sense. That's the number of bytes for this file. Now it can be hard to read these long numbers here. So instead I'm going to use ls-l and also add the parameter h. That gives me human readable results. So m for megs, b for bytes, these's k and g as well. If instead of seeing a list like this with all the data I could just, instead of dash l do dash s. That gives me the sizes for everything.
Now you could also use the Find command to find files. So let's go back up a level. You could go back up a level with two dots like that so I don't have to retype the whole path of the Documents folder. I know it's one level up so two dots will take me there. Now, let's say I want to find a file. I can use the Find command for that. I start off giving it a location. Most of the time you're going to want to use your current location, which is just a single dot. If you're just starting off using the Terminal it's better to go to the actual location where you want to do things and then use the single dot even though you could actually type a full path there instead.
Now let's say I want to find something by name. So I could do dash name and then I'm going to use quotes here. I'm going to use single quotes actually. I'm going to use the name of the file. So I know there's one called Trail.jpg like that and it's going to give me the result which it finds that file in the Projects Folder. Now what if I had typed a lower case t. It wouldn't find it. So case matters. But I could put an i in front of name for insensitive case insensitive so then it will find it regardless of case. Now instead of name or iname I could do find and then -size. Now I could type something here like say 2G or 2M. But I'm going to get no results. The reason I get no results is I'm asking it to find files that are exactly 2M in size. It's unlikely that a file is going to be exactly that. So if I want to find something 2M or larger I just put a + in front of it like that.
By the way notice here that I easily bring up previous commands. I do that using the Up arrow key. The up arrow key goes through your previous history of commands. So when you're modifying your previous command instead of typing it from scratch use an up arrow to bring it up and then you could use the arrow keys to go back and forth and edit it or add things to the end.
So now let's run this and you could see it tells me the files that are 2M or larger. But what about folders? This is great for files but a lot of time you want to find out what folders are taking up a lot of space. So let's use a different command, du for disk utility, and if I just run du right here in the Documents folder it's going to give me a list of all of the folders and the size next to it. So it's already pretty useful. It's actually going to look inside every folder, inside every folder of every folder, and all the way down. So all the folders underneath this. It's a really long list. So what about just limiting that. You can using dev dash d and then giving it a number. Like, for instance, one would just look in the current directory. I actually get the size of the current directory, there's the dot there, and then everything in it. So all the folders in it and the sizes next to it. If I were to repeat the command but d2 it would look one level down. So, for instance, I'd get the Personal folder here but then every folder inside the Personal folder as well.
These numbers are hard to read so we can modify that further with -h and that gives us human readable numbers here. What if we wanted to limit it to only folders that are above a certain amount. We could do that using dash t for threshold. Then you want to give it a human readable number, like say 10G for any folder above 10G, that's going to give us no results here. Let's do 2M. Now we get just the list of everything that's 2M or larger. What if we wanted to sort it. Well we can sort it by sending the results from this command to another command. The way to do that is to pipe it to the command using the pipe symbol. That's the straight line character that you're going to find on US keyboards below the Delete key. You need to use Shift and then backslash to bring up that special character. It's very common when working in Terminal to use this. We're going to send the results of du to the sort command and we're going to say we want to sort by the human readable numbers. So take into account it might be B, K, M, and G, and we also want to do r for reverse. So the largest numbers are at the top. Now we see here we get the largest at the top, which is the current folder so it contains everything, it's always going to be the largest. Then we get the folder that's the largest followed by the next one and then all the way down. If we limited this to d1 we would just get all the folders in the current directory sorted by size, of course as long as they were larger than 2M.
If you ever wanted to limit this because the list was too long you could further pipe this to either the Head or Tail command. The Head command gives you the first items from the list and the Tail gives you the last items from the list. So you could do Head, that's always going to give you the first 10 and there's fewer than 10 here. But if we wanted to do -n 5 it would just give us the first 5 items on the list.
What about doing this with files? You could use the Find command and then you would want to make it pipe the results into a way that would list the files. But there isn't a command to do that. The ls command would do that but you need to send each individual line to the ls command. You do that with -exec and we're going to use the ls command and we're going to do -l to list, human readable format and then you finish up the exec command with curly brackets to represent incoming data from that line and then a backslash and a semi-colon, like that. Now you're going to get a list. It looks like the result of a ls-l command but you get the files that you found with this Find command. So only the files that are greater than 2M. Let's do the same thing and look for files that are greater than 10M. You could see the results are a lot shorter. So if you were doing, say, 1G or 2G you could easily find the largest files that you've got.
Now, instead of using ls let's use du because disk utility will work on files as well as folders. The parameters we want to give it here are hs, human readable format by size, and now we get these results like that. Some nicely formatted stuff here we can see it's not sorted so let's further pipe that to sort and then human readable and reverse order and now we get the largest files first, the result of our find. We could change this number to something larger or smaller if we wanted to only find the largest files starting in the current location which is the Documents folder.
So here's the ultimate command for finding the largest folder. It's du-h for human readable sizes, you do a threshold, in this case 2M, then you sort it, human readable in reverse order so the largest is at the top and you can use Head dash n 5 for the top five, like that.
Here's the ultimate command for finding the largest files. You Find and then the current location that you're at and then dash size and then plus, in this case 10M, 2M, 2G whatever you want, and then you use exec to send those results into du human readable size again and then you sort them human readable reverse order and that gives you the largest files from that location.
So I hope you found this useful or at least encourages you to explore some commands in the Terminal. Thanks for watching.Related Subjects: Finder (233 videos), Terminal (30 videos)
Related Video Tutorials:
Mac Terminal Commands and Apps To Work With Text Files ― Searching For Files In Two Or More Folders At the Same Time On Your Mac ― Productivity Series: Files and Folders ― 8 Better Alternatives To Sending Large Email Attachments

Here’s a list of some examples used in the video:

# change directory (drag and drop a folder from the Finder after it)

cd

# show current directory

pwd

# list files

ls

# list files with details

ls -l

# use human-readable file sizes

ls -lh

# find files starting at current location and containing text in name

find. -iname *test*

# case insensitive

find. -iname *test* -d 3

# find files over a certain size

find. -size +200M

# list directories (folders)

du -d1

# use human-readable sizes

du -h -d1

# look down 3 levels and only show folders above a threshold size

du -h -d3 -t10G

# list directories and sort by size

du -h | sort -hr

# only show top 10

du -h | sort -hr | head -n10

# list directories in current folder, threshold 10G,

# sort with human-readable sizes, top 10

du -h -d1 -t10G | sort -hr | head -n 10

# find files larger than a size, list with details

find. -size +200M -exec ls -lh {};

# find files and folders, list with details

find. -size +200M -exec du -hs {};

# find files and folders, list with details, sort

find. -size +200M -exec du -hs {}; | sort -hr

# find files and folders, list with details, sort, top 10

find. -size +200M -exec du -hs {}; | sort -hr | head -n 10
https://macmost.com/using-terminal-to-find-large-files-and-folders.html
News copyright owned by their original publishers | Copyright © 2004 - 2024 Zicos / 440Network
Current Date
Apr, Sat 20 - 01:49 CEST