- Count Number of Files in a Directory in Linux
- Count number of files in directory in Linux
- Count number of files and directories (without hidden files)
- Count number of files and directories including hidden files
- Count number of files and directories including the subdirectories
- Count only the files, not directories
- Count only the files, not directories and only in current directory, not subdirectories
- Linux find largest file in directory recursively using find/du
- Linux find largest file in directory recursively using find
- Linux find a biggest files in /
- Linux find large files quickly with bash alias
- Finding largest file recursively on Linux bash shell using find
- Great! I found the largest files on my disk. What next?
- Conclusion
- How to Find Out Top Directories and Files (Disk Space) in Linux
- How to Find Biggest Files and Directories in Linux
- Find Largest Directories in Linux
- Find Out Top File Sizes Only
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to find a folder in Linux using the command line
- Command to find a folder in Linux
- How to find folder on Linux using find command
- Finding a folder named Documents
- How to search for case incentive folder names
- How to search a folder named /etc/ in the root (/) file system
- How to hide “Permission denied error messages” when using find command
- How do I find a directory called python.projects?
- Understanding find command options
- Search folder in Linux using locate command
- Conclusion
Count Number of Files in a Directory in Linux
I presume you are aware of the wc command for counting number of lines. We can use the same wc command with ls command to count the number of files in a directory.
This task seems simple but could soon turn slightly complex based on your need and definition of counting files. Before I confuse you further, let’s see about various use cases of counting the number of files in Linux.
Count number of files in directory in Linux
Let me first show you the content of the test directory I am going to use in this tutorial:
You can see that it has 9 files (including one hidden file) and 2 sub-directories in that directory. But you don’t have to do it manually. Let’s count the number of files using Linux commands.
Count number of files and directories (without hidden files)
You can simply run the combination of the ls and wc command and it will display the number of files:
This is the output:
There is a problem with this command. It counts all the files and directories in the current directories. But it doesn’t see the hidden files (the files that have name starting with a dot).
This is the reason why the above command showed me a count of 10 files instead of 11 (9 files and 2 directories).
Count number of files and directories including hidden files
You probably already know that -a option of ls command shows the hidden files. But if you use the ls -a command, it also displays the . (present directory) and .. (parent directory). This is why you need to use -A option that displays the hidden files excluding . and .. directories.
This will give you the correct count of files and directories in the current directory. Have a look at the output that shows a count of 11 (9 files and 2 directories):
You can also use this command to achieve the same result:
Note that it the option used is 1 (one) not l (L). Using the l (L) option displays an additional line at the beginning of the output (see ‘total 64’ in the directory output at the beginning of the article). Using 1 (one) lists one content per line excluding the additional line. This gives a more accurate result.
Count number of files and directories including the subdirectories
What you have see so far is the count of files and directories in the current directory only. It doesn’t take into account the files in the subdirectories.
If you want to count the number of files and directories in all the subdirectories, you can use the tree command.
This command shows the directory structure and then displays the summary at the bottom of the output.
As you can see in the output, it shows that there are 7 directories and 20 files in total. The good thing about this result is that it doesn’t count directories in the count of files.
Count only the files, not directories
So far, all the solutions we have seen for counting the number of files, also take directories into account. Directories are essentially files but what if you want to count only the number of files, not directories? You can use the wonderful find command.
You can run this command:
The above command searched for all the files (type f) in current directory and its subdirectories.
Count only the files, not directories and only in current directory, not subdirectories
That’s cool! But what if you want to count the number of files in the current directory only excluding the files in the subdirectories? You can use the same command as above but with a slight difference.
All you have to do is to add the ‘depth’ of your find. If you set it at 1, it won’t enter the subdirectories.
Here’s the output now:
In the end…
In Linux, you can have multiple ways to achieve the same goal. I am pretty sure there can be several other methods to count the number of files in Linux. If you use some other command, why not share it with us?
I hope this Linux tutorial helped you learn a few things. Stay in touch for more Linux tips.
Источник
Linux find largest file in directory recursively using find/du
I have 500GB SSD installed on my Linux server. My web server is running out of the disk space. I need to find a biggest or largest file concerning file size on the disk. How do I find largest file in a directory recursively using the find command?
To find a big file concerning file size on disk is easy task if you know how to use the find, du and other command. The du command used to estimate file space usage on Linux system. The output of du passed on to the sort and head command using shell pipes. Let us see how to find largest file in Linux server using various commands.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux |
Est. reading time | 3 minutes |
Linux find largest file in directory recursively using find
The procedure to find largest files including directories in Linux is as follows:
- Open the terminal application
- Login as root user using the sudo -i command
- Type du -a /dir/ | sort -n -r | head -n 20
- du will estimate file space usage
- sort will sort out the output of du command
- head will only show top 20 largest file in /dir/
Linux find a biggest files in /
Linux find large files quickly with bash alias
One can hunt down disk space hogs with ducks bash shell alias
How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD
Finding largest file recursively on Linux bash shell using find
One can only list files and skip the directories with the find command instead of using the du command, sort command and head command combination:
$ sudo find / -type f -printf «%s\t%p\n» | sort -n | tail -1
$ find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10
Here is what I got on my systems:
Where, find command options are as follows:
- $HOME – Directory search for files.
- -type f – Search for regular files only.
- -printf ‘%s %p\n’ – Force find to use print format on the scren, interpreting \ escapes and % directives. The %s will print file’s size in bytes. Show file name using %p . This speailized output makes it easy to sort out file names using the sort command.
The -n is for numeric sort and the -r passed to sort will reverse the result of comparisons. The head command is used to control and show the first part of files. In other words, only display the top 10 results from previous commands.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Great! I found the largest files on my disk. What next?
Depend upon file/dir type you can either move or delete the file. For example, you cannot remove or move the Linux kernel or diver directories. To delete unwanted file on Linux use the rm command:
rm -i -v /path/to/file
To get rid of all files and its sub-directories recursively use following command:
rm -rf /path/to/folderName
To move file to a usb pen mounted at /mnt/usb/, run the mv command:
mv /path/to/large/file/ /mnt/usb/
Conclusion
You just learned how to search, find and list largest or biggest directories/files in Linux using the combination of du/find and other commands. For more info see this page or man pages of du and find commands:
man du
man find
man sort
man head
man tail
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Find Out Top Directories and Files (Disk Space) in Linux
As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up them from your hard disk.
This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.
How to Find Biggest Files and Directories in Linux
Run the following command to find out top biggest directories under /home partition.
Find Largest Directories in Linux
The above command displays the biggest 5 directories of my /home partition.
Find Largest Directories in Linux
If you want to display the biggest directories in the current working directory, run:
Find Biggest Directories Only
Let us break down the command and see what says each parameter.
- du command: Estimate file space usage.
- a : Displays all files and folders.
- sort command : Sort lines of text files.
- -n : Compare according to string numerical value.
- -r : Reverse the result of comparisons.
- head : Output the first part of files.
- -n : Print the first ‘n’ lines. (In our case, We displayed the first 5 lines).
Some of you would like to display the above result in human-readable format. i.e you might want to display the largest files in KB, MB, or GB.
Find Top Directories Sizes in Linux
The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.
To display the largest folders/files including the sub-directories, run:
Find Largest Folder and Subdirectories
Find out the meaning of each option using in above command:
- du command: Estimate file space usage.
- -h : Print sizes in human-readable format (e.g., 10MB).
- -S : Do not include the size of subdirectories.
- -s : Display only a total for each argument.
- sort command : sort lines of text files.
- -r : Reverse the result of comparisons.
- -h : Compare human readable numbers (e.g., 2K, 1G).
- head : Output the first part of files.
Find Out Top File Sizes Only
If you want to display the biggest file sizes only, then run the following command:
Find Top File Sizes in Linux
To find the largest files in a particular location, just include the path beside the find command:
Find Top File Size in Specific Location
The above command will display the largest file from /home/tecmint/Downloads directory.
That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How to find a folder in Linux using the command line
Command to find a folder in Linux
- find command – Search for files and folder in a directory hierarchy
- locate command – Find files and folders by name using prebuilt database/index
How to find folder on Linux using find command
The syntax is:
find /where/to/look/up/ criteria action
find /folder/path/to/look/up/ criteria action
find /folder/path/ -name «folder-name-here»
find /search/path/ -name «folder-name-here» -print
find /search/path/ -name «folder-name-here» -ls
find /folder/ -name «pattern»
Finding a folder named Documents
To find a folder named “Documents” in your home directory ($HOME i.e. /home/vivek/ home directory), run:
find $HOME -type d -name «Documents»
OR
find
-type d -name «Documents»
OR
find /home/vivek/ -type d -name «Documents»
find command in action on Linux
How to search for case incentive folder names
You can force find command interpret upper and lowercase letters as being the same. For example match Documents, DOCUMENTS, DocuMEnts and so on by passing the -iname option:
find $HOME -type d -iname «Documents»
OR
find
-type d -iname «Documents»
OR
find /home/vivek/ -type d -iname «Documents»
Sample outputs:
How to search a folder named /etc/ in the root (/) file system
When searching / (root) file system, you need to run the find command as root user:
# find / -type d -name «etc»
OR
$ sudo find / -type d -name «etc»
OR
$ sudo find / -type d -iname «etc»
How to hide “Permission denied error messages” when using find command
The find will show an error message for each directory/file on which you don’t have read permission. To avoid those messages, append 2>/dev/null at the end of each find command:
$ find /where/to/look/ criteria action 2>/dev/null
$ sudo find / -type d -iname «etc» 2>/dev/null
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I find a directory called python.projects?
Try:
find / -type d -iname «python.projects» -ls
OR
find / -type d -name «python.projects» -ls
It is also possible to use the bash shell wild cards, run:
find / -type d -name «python.*»
sudo find / -type d -name «?ython.*»
Understanding find command options
- -name : Base of file name (the path with the leading directories removed) matches shell pattern.
- -iname : Perform a case insensitive search for given pattern
- -print : Print the full file name on the standard output (usually screen), followed by a newline.
- -ls : Display current file in ls -dils format on standard output i.e. your screen.
- -type d : Only list folders or directories.
- -type f : Only list files.
Search folder in Linux using locate command
To search for a folder named exactly dir1 (not *dir1*), type:
$ locate -b ‘\dir1’
$ locate -b ‘\folder2’
Just search for file name matching Pictures, type:
$ locate Pictures
For more info see “UNIX Find A File Command“.
Conclusion
In this tutorial, you learned how to find a folder on the Linux system using find and locate commands. For more info see gnu find command help page here.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник