In this guide, you will learn to Counting Files in a Linux Directory with Examples. You can easily use the Linux Commands to count your files in a directory. Here we show you how to use ls, find, rsync, and tree commands to do this. To show you the guide steps, we will create an example directory and files in it and then show you how to count the files.
Steps To Counting Files in a Linux Directory with Examples
To complete this guide, you must have access to your Linux server as a root or non-root user with sudo privileges. For this purpose, you can visit the Orcacore website and search for the initial server setup guides.
Now to show the guide steps, we want to create a directory and files in it. To do this, you can run the following commands:
# mkdir test_dir # cd test_dir # mkdir first_subdir second_subdir # touch file1.py file2.js file3.c
Then, switch to the first sub-directory and create files into it:
# cd first_subdir # touch file4.txt file5.txt file6.txt
Also, for the second one do it so:
# cd .. # cd second_subdir # mkdir third_subdir # touch file7.txt
Now switch to your Test directory and follow the steps below to see how to count files and directories in Linux.
Step 1 – Use the ls command for Counting Files and Directories
At this point, you can use the ls command to count files and directories in Linux.
To count files in the current directory, you can run the command below:
ls | wc -l
Also, you can count files in a specific file or directory by using the command below:
ls <directory name or full path> | wc -l
For example, we want to count our first sub-directory:
ls first_subdir | wc -l
Now you can use the ls command to show only files. For example, we switch to our home directory and count files in the test directory with the command below:
# cd .. # ls -1Up test_dir | grep -v / | wc -l
The options used in the command mean:
- -1: returns one entry per line,
- -U: returns an unsorted output,
- -p: appends slash/indicator to directories, and
- grep -v: excludes the directories.
Step 2 – Use the find command for Counting Linux Files and Directories
Another command that you can use to count files and directories is the find command.
To count all the files and directories in a specific directory, you can use the command below in Linux:
find <directory> | wc -l
For example, we count the files and directories in the test directory:
find test_dir | wc -l
If you want to count the files in your directory, you can use the command below:
find test_dir -type f | wc -l
Step 3 – Use the rsync command to List and Count Files and Directories
At this point, you can use the rsync command to display the files and directories. For example, we use it for our test directory with the command below:
rsync --stats --dry-run -a test_dir
In the output, you will see:
Output drwxr-xr-x 4,096 2023/09/11 06:25:28 test_dir -rw-r--r-- 0 2023/09/11 06:25:28 test_dir/file1.py -rw-r--r-- 0 2023/09/11 06:25:28 test_dir/file2.js -rw-r--r-- 0 2023/09/11 06:25:28 test_dir/file3.c drwxr-xr-x 4,096 2023/09/11 06:25:38 test_dir/first_subdir -rw-r--r-- 0 2023/09/11 06:25:38 test_dir/first_subdir/file4.txt -rw-r--r-- 0 2023/09/11 06:25:38 test_dir/first_subdir/file5.txt -rw-r--r-- 0 2023/09/11 06:25:38 test_dir/first_subdir/file6.txt drwxr-xr-x 4,096 2023/09/11 06:25:59 test_dir/second_subdir -rw-r--r-- 0 2023/09/11 06:25:59 test_dir/second_subdir/file7.txt drwxr-xr-x 4,096 2023/09/11 06:25:54 test_dir/second_subdir/third_subdir Number of files: 11 (reg: 7, dir: 4) Number of created files: 0 Number of deleted files: 0 Number of regular files transferred: 0 Total file size: 0 bytes Total transferred file size: 0 bytes Literal data: 0 bytes Matched data: 0 bytes File list size: 0 File list generation time: 0.001 seconds File list transfer time: 0.000 seconds Total bytes sent: 352 Total bytes received: 838 sent 352 bytes received 838 bytes 2,380.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN)
This is a great tool to get detailed information about your directory and files.
Step 4 – View the Linux Directory Structure with the tree command
Now you can also use another tool named tree command to give you the structure of the directory and counting the files.
For example, to view the test directory, run the command below:
Output test_dir ├── file1.py ├── file2.js ├── file3.c ├── first_subdir │ ├── file4.txt │ ├── file5.txt │ └── file6.txt └── second_subdir ├── file7.txt └── third_subdir 3 directories, 7 files
Also, you can use the -a option in the command to show you the hidden files.
tree -a test_dir
That’s it, you are done.
At this point, you have learned to use the Linux commands for Counting Files and Directories in a specific Linux directory with Examples. The commands you can use include ls, find, rsync, and tree commands. Hope you enjoy it.
Do you have any suggestions? Please comment for us.
Also, you may be interested in these articles: