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?
2 Answers
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.
- 1This isn't correct in the presence of files or directories with carriage returns in their filenames.pericynthion– pericynthion2016-03-01 15:43:22 +00:00Commented Mar 1, 2016 at 15:43
- @pericynthion, carriage return wouldn't be a problem, linefeed aka newline would though.Stéphane Chazelas– Stéphane Chazelas2016-03-01 17:02:56 +00:00Commented Mar 1, 2016 at 17:02
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.
.and..entries? Do you want them included?