Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of files in a folder as a variable?

Tags:

bash

shell

Using bash, how can one get the number of files in a folder, excluding directories from a shell script without the interpreter complaining?

With the help of a friend, I've tried

$files=$(find ../ -maxdepth 1 -type f | sort -n) $num=$("ls -l" | "grep ^-" | "wc -l") 

which returns from the command line:

../1-prefix_blended_fused.jpg: No such file or directory ls -l :  command not found grep ^-: command not found wc -l:   command not found 

respectively. These commands work on the command line, but NOT with a bash script.

Given a file filled with image files formatted like 1-pano.jpg, I want to grab all the images in the directory to get the largest numbered file to tack onto the next image being processed.

Why the discrepancy?

like image 931
Jason Avatar asked Jun 21 '12 03:06

Jason


People also ask

How do I count files in a folder?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

How will you store in a variable count the total number of lines in a file?

Create a variable to store the file path. Use wc –lines command to count the number of lines. Use wc –word command to count the number of words. Print the both number of lines and the number of words using the echo command.

How do I Count the number of files in a folder?

3. How to count the files in a folder, using Command Prompt (cmd) You can also use the Command Prompt. To count the folders and files in a folder, open the Command Prompt and run the following command: dir /a:-d /s /b "Folder Path" | find /c ":".

How to find the number of subfolders in a folder?

You can directly launch it by pressing Windows Key+E shortcut or go to My Computer and select the correct partition. File Explorer can also be used to quickly find the number of subfolders or files inside a particular folder. Open the folder and select all the subfolders or files either manually or by pressing CTRL+A shortcut.

How do I list the number of files in a directory?

As seen above, at the bottom of the dir output, you'll see how many files and directories are listed in the current directory. To list the count of files in Linux, use the ls command piped into the wc command, as shown below. To prevent any confusion, the above command reads ls <dash><the #1> <pipe> ls <dash><the letter l>.

How to count files and subfolders inside a directory in Linux?

Like the CD command is used to change directory, DIR command is used to work with directories. To count all the files and subfolders inside a parent folder, or directory, type the following command. Don’t worry if you see the text scrolling automatically.


1 Answers

The quotes are causing the error messages.

To get a count of files in the directory:

shopt -s nullglob numfiles=(*) numfiles=${#numfiles[@]} 

which creates an array and then replaces it with the count of its elements. This will include files and directories, but not dotfiles or . or .. or other dotted directories.

Use nullglob so an empty directory gives a count of 0 instead of 1.

You can instead use find -type f or you can count the directories and subtract:

# continuing from above numdirs=(*/) numdirs=${#numdirs[@]} (( numfiles -= numdirs )) 

Also see "How can I find the latest (newest, earliest, oldest) file in a directory?"

You can have as many spaces as you want inside an execution block. They often aid in readability. The only downside is that they make the file a little larger and may slow initial parsing (only) slightly. There are a few places that must have spaces (e.g. around [, [[, ], ]] and = in comparisons) and a few that must not (e.g. around = in an assignment.

like image 64
Dennis Williamson Avatar answered Sep 23 '22 07:09

Dennis Williamson