Counting files in a directory
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
4
down vote
favorite
I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.
# Report on Current Status
echo -n "Cropped Files: "
ls "$Destination" | wc -l
My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.
Duplicate file: foo.pdf
Changed name: foo.x.pdf
If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls
output so wc -l
can count the unique files only?
TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?
command-line bash scripts
add a comment |Â
up vote
4
down vote
favorite
I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.
# Report on Current Status
echo -n "Cropped Files: "
ls "$Destination" | wc -l
My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.
Duplicate file: foo.pdf
Changed name: foo.x.pdf
If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls
output so wc -l
can count the unique files only?
TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?
command-line bash scripts
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.
# Report on Current Status
echo -n "Cropped Files: "
ls "$Destination" | wc -l
My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.
Duplicate file: foo.pdf
Changed name: foo.x.pdf
If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls
output so wc -l
can count the unique files only?
TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?
command-line bash scripts
I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.
# Report on Current Status
echo -n "Cropped Files: "
ls "$Destination" | wc -l
My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.
Duplicate file: foo.pdf
Changed name: foo.x.pdf
If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls
output so wc -l
can count the unique files only?
TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?
command-line bash scripts
command-line bash scripts
edited Feb 9 at 21:19
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
wjandrea
7,27742256
7,27742256
asked Feb 9 at 21:05
Aaron Nichols
686
686
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
8
down vote
accepted
To find the number of files in a directory that do not contain .x.pdf
, try:
find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c
To find the number of files in a directory that do not contain period - one or more x
- period - pdf
, try:
find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1
. For example:
find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
Note that because we use -printf '1'
, this method is safe even if the directory contains files whose names contain newline characters.
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminaten
. Thanks.
â John1024
Feb 21 at 18:23
add a comment |Â
up vote
2
down vote
Without subdirectories:
echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))
because:
for file in *.sh ; do echo -n 1+; done; echo 0;
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
add a comment |Â
up vote
0
down vote
You can exclude a file or files that match to a pattern from the ls
command by using (one or more times) the option -I
, --ignore=PATTERN
(reference):
ls -I "*.x*.pdf" "$Destination" | wc -l
Or you could use the subtraction method in this way:
echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
To find the number of files in a directory that do not contain .x.pdf
, try:
find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c
To find the number of files in a directory that do not contain period - one or more x
- period - pdf
, try:
find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1
. For example:
find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
Note that because we use -printf '1'
, this method is safe even if the directory contains files whose names contain newline characters.
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminaten
. Thanks.
â John1024
Feb 21 at 18:23
add a comment |Â
up vote
8
down vote
accepted
To find the number of files in a directory that do not contain .x.pdf
, try:
find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c
To find the number of files in a directory that do not contain period - one or more x
- period - pdf
, try:
find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1
. For example:
find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
Note that because we use -printf '1'
, this method is safe even if the directory contains files whose names contain newline characters.
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminaten
. Thanks.
â John1024
Feb 21 at 18:23
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
To find the number of files in a directory that do not contain .x.pdf
, try:
find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c
To find the number of files in a directory that do not contain period - one or more x
- period - pdf
, try:
find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1
. For example:
find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
Note that because we use -printf '1'
, this method is safe even if the directory contains files whose names contain newline characters.
To find the number of files in a directory that do not contain .x.pdf
, try:
find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c
To find the number of files in a directory that do not contain period - one or more x
- period - pdf
, try:
find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1
. For example:
find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c
Note that because we use -printf '1'
, this method is safe even if the directory contains files whose names contain newline characters.
edited Feb 21 at 18:22
answered Feb 9 at 21:13
![](https://i.stack.imgur.com/En2fP.png?s=32&g=1)
![](https://i.stack.imgur.com/En2fP.png?s=32&g=1)
John1024
9,2441933
9,2441933
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminaten
. Thanks.
â John1024
Feb 21 at 18:23
add a comment |Â
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminaten
. Thanks.
â John1024
Feb 21 at 18:23
1
1
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
â Aaron Nichols
Feb 9 at 21:17
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminate
n
. Thanks.â John1024
Feb 21 at 18:23
@DavidFoerster Yes, that does seem simpler. Answer updated to eliminate
n
. Thanks.â John1024
Feb 21 at 18:23
add a comment |Â
up vote
2
down vote
Without subdirectories:
echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))
because:
for file in *.sh ; do echo -n 1+; done; echo 0;
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
add a comment |Â
up vote
2
down vote
Without subdirectories:
echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))
because:
for file in *.sh ; do echo -n 1+; done; echo 0;
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Without subdirectories:
echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))
because:
for file in *.sh ; do echo -n 1+; done; echo 0;
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0
Without subdirectories:
echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))
because:
for file in *.sh ; do echo -n 1+; done; echo 0;
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0
answered Feb 9 at 23:50
user unknown
4,80622151
4,80622151
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
add a comment |Â
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
1
1
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
â John1024
Feb 10 at 0:31
1
1
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
@John1024: Count all files, count all files with .x*.pdf, subtract.
â user unknown
Feb 10 at 0:51
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
user-unknown, OK. Very good.
â John1024
Feb 10 at 1:46
add a comment |Â
up vote
0
down vote
You can exclude a file or files that match to a pattern from the ls
command by using (one or more times) the option -I
, --ignore=PATTERN
(reference):
ls -I "*.x*.pdf" "$Destination" | wc -l
Or you could use the subtraction method in this way:
echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))
add a comment |Â
up vote
0
down vote
You can exclude a file or files that match to a pattern from the ls
command by using (one or more times) the option -I
, --ignore=PATTERN
(reference):
ls -I "*.x*.pdf" "$Destination" | wc -l
Or you could use the subtraction method in this way:
echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can exclude a file or files that match to a pattern from the ls
command by using (one or more times) the option -I
, --ignore=PATTERN
(reference):
ls -I "*.x*.pdf" "$Destination" | wc -l
Or you could use the subtraction method in this way:
echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))
You can exclude a file or files that match to a pattern from the ls
command by using (one or more times) the option -I
, --ignore=PATTERN
(reference):
ls -I "*.x*.pdf" "$Destination" | wc -l
Or you could use the subtraction method in this way:
echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))
edited Feb 10 at 8:48
answered Feb 10 at 8:33
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Lrlbx.jpg?s=32&g=1)
pa4080
12.3k52357
12.3k52357
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1004680%2fcounting-files-in-a-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password