How can I find a number of files in a folder, assign it to a variable, then echo that variable, all in one command line?
Asked
Active
Viewed 230 times
-1
-
Do you want to include sub-directories in this count? – Centimane Mar 01 '16 at 15:40
-
@Dave yes including subdirectories – Warrior4just Mar 01 '16 at 16:12
-
Any type of file? Or just regular files? Excluding directories? What about symlinks (to regular files or others). What about files (hard-)linked several times (or in other words, do you want to count files or directory entries)? What about the `.` and `..` entries? Do you want them included? – Stéphane Chazelas Mar 01 '16 at 16:20
-
@StéphaneChazelas jus regular files within a parent directory and its subfolders . – Warrior4just Mar 01 '16 at 16:28
2 Answers
2
linecount=$(find /folder/name/here/ -type f | wc -l); echo ${linecount}
is the simplest way of doing this. it counts every file in the folder and its sub-folders.
MelBurslan
- 6,836
- 2
- 24
- 35
-
1This isn't correct in the presence of files or directories with carriage returns in their filenames. – pericynthion Mar 01 '16 at 15:43
-
@pericynthion, carriage return wouldn't be a problem, linefeed aka newline would though. – Stéphane Chazelas Mar 01 '16 at 17:02
0
What comes to my head without using semicolon to break and start a new command is to use Parameter substitution with Command substitution
$ echo ${filecount=$(find . -type f | wc -l)}
Then you can echo the variable again and you can confirm the result.
tachomi
- 7,372
- 4
- 25
- 45