List files sorted by the number of lines they containCount number of lines in files then compare which has more (BASH)How to list files sorted by modification date recursively (no stat command available!)List the files accessed by a programCompressing all files that contain a particular stringFinding all files containing a word and then counting the number of linesList files with line count and dateCount and list down + advanced featuresscript that lists all files in a directory and subdirectories sorted by size, listing only file names not complete pathsSearch for files that contain a string and list their names sorted by the modified dateRecursively list all directories that contain one or more jpg image filesList files sorted according to specific line of contents
Do I need a multiple entry visa for a trip UK -> Sweden -> UK?
Modify casing of marked letters
How to be diplomatic in refusing to write code that breaches the privacy of our users
Can somebody explain Brexit in a few child-proof sentences?
Personal Teleportation as a Weapon
Can a monster with multiattack use this ability if they are missing a limb?
Is expanding the research of a group into machine learning as a PhD student risky?
How does a character multiclassing into warlock get a focus?
Applicability of Single Responsibility Principle
Irreducibility of a simple polynomial
What defines a dissertation?
Can criminal fraud exist without damages?
Teaching indefinite integrals that require special-casing
What will be the benefits of Brexit?
What is difference between behavior and behaviour
Is it correct to write "is not focus on"?
Lay out the Carpet
Is HostGator storing my password in plaintext?
Why does John Bercow say “unlock” after reading out the results of a vote?
Print name if parameter passed to function
Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?
Using parameter substitution on a Bash array
What's the purpose of "true" in bash "if sudo true; then"
Should my PhD thesis be submitted under my legal name?
List files sorted by the number of lines they contain
Count number of lines in files then compare which has more (BASH)How to list files sorted by modification date recursively (no stat command available!)List the files accessed by a programCompressing all files that contain a particular stringFinding all files containing a word and then counting the number of linesList files with line count and dateCount and list down + advanced featuresscript that lists all files in a directory and subdirectories sorted by size, listing only file names not complete pathsSearch for files that contain a string and list their names sorted by the modified dateRecursively list all directories that contain one or more jpg image filesList files sorted according to specific line of contents
How can I list the number of lines in the files in /group/book/four/word
, sorted by the number of lines they contain?
ls -l
command lists them down but does not sort them
bash shell files wc
add a comment |
How can I list the number of lines in the files in /group/book/four/word
, sorted by the number of lines they contain?
ls -l
command lists them down but does not sort them
bash shell files wc
1
Do you want the files listed by number of lines, or list the number of lines in the files or both?ls -l
doesn't give the number of lines.ls -lS
sorts file by size with somels
implementations (size being number of bytes in the content).
– Stéphane Chazelas
Nov 27 '14 at 12:59
add a comment |
How can I list the number of lines in the files in /group/book/four/word
, sorted by the number of lines they contain?
ls -l
command lists them down but does not sort them
bash shell files wc
How can I list the number of lines in the files in /group/book/four/word
, sorted by the number of lines they contain?
ls -l
command lists them down but does not sort them
bash shell files wc
bash shell files wc
edited Feb 28 '16 at 20:40
don_crissti
51.7k15141168
51.7k15141168
asked Nov 27 '14 at 12:31
Ken RKen R
149126
149126
1
Do you want the files listed by number of lines, or list the number of lines in the files or both?ls -l
doesn't give the number of lines.ls -lS
sorts file by size with somels
implementations (size being number of bytes in the content).
– Stéphane Chazelas
Nov 27 '14 at 12:59
add a comment |
1
Do you want the files listed by number of lines, or list the number of lines in the files or both?ls -l
doesn't give the number of lines.ls -lS
sorts file by size with somels
implementations (size being number of bytes in the content).
– Stéphane Chazelas
Nov 27 '14 at 12:59
1
1
Do you want the files listed by number of lines, or list the number of lines in the files or both?
ls -l
doesn't give the number of lines. ls -lS
sorts file by size with some ls
implementations (size being number of bytes in the content).– Stéphane Chazelas
Nov 27 '14 at 12:59
Do you want the files listed by number of lines, or list the number of lines in the files or both?
ls -l
doesn't give the number of lines. ls -lS
sorts file by size with some ls
implementations (size being number of bytes in the content).– Stéphane Chazelas
Nov 27 '14 at 12:59
add a comment |
5 Answers
5
active
oldest
votes
You should use a command like this:
find /group/book/four/word/ -type f -exec wc -l + | sort -rn
find
: search for files on the path you want. If you don't want it recursive, and yourfind
implementation supports it, you should add-maxdepth 1
just before the-exec
option.exec
: tells the command to executewc -l
on every file.sort -rn
: sort the results numerically in reverse order. From greater to lower.
(that assumes file names don't contain newline characters).
Note that when passed more than one file (or with some implementations, more than one file that it can read),wc
will also print atotal
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe togrep /
to remove them.
– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because ofsort
command
– Francisco
Sep 14 '18 at 23:43
add a comment |
Non-recursive
Probably the simplest version if you don't need recursivity :
wc -l /group/book/four/word/*|sort -n
wc
counts lines (option -l
) in every (but hidden) (*
) files under /group/book/four/word/
, and sort
sorts the result (through the pipe |
) numerically (option -n
).
Recursive
Someone made a comment to this answer mentioning grep -rlc
, before to suppress it. Indeed grep
is a great alternative, especially if you need recursivity :
grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2
will count (option -c
) recursively (option -r
) lines matching (grep
) '^'
(that is, beginning of lines) in the directory /group/book/four/word/
. Then you have to replace the colon by a space, e.g. using tr
, to help sort
, which you want to sort numerically (option -n
) on the second column (option -k2
).
Update : See Stephane's comment about possible limitations and how you can actually get rid of tr
.
1
grep -c .
counts the lines that contain at least one valid character. Usegrep -c '^'
to count all the lines (will also count trailing characters after the last newline with somegrep
implementations). Note that not allgrep
implementations support a-r
and behaviour varies among those that do. You don't need to translate:
s (colon, not semicolon) to spaces forsort
. Just use-t:
. Note that that assumes file names don't contain:
or blank or newline characters.
– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't knowwc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe tosort
is really clean.
– Qcom
Sep 25 '15 at 21:21
add a comment |
With zsh
:
lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)
We define a new sorting function lines
that replies with the number of lines in the file. And we use the o+lines
glob qualifier which together with n
(for numeric sort), defines how the results of the glob are ordered. (.
also added to only check regular files).
That makes no assumption on what character the file names may contain other than hidden files (those starting with .
) are omitted. Add the D
glob qualifier if you want them as well.
2
OP is tagged withbash
only...
– l0b0
Nov 27 '14 at 13:15
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
add a comment |
You don't specify whether you also want the files in any subdirectories of /group/book/four/word
. The find
solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:
for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
If your file names can contain newlines, you can use something like:
for file in ./*; do
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'
Finally, if you do want to descend into subdirectories, you can use this in bash
4 or above:
shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
Note that versions of bash
prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh
's or tcsh
's ***/*
).
Also, all solutions above will ignore hidden files (those whose name starts with a .
, use shopt -s dotglob
to include them) and will also include the line count of symbolic links (which the find
approach will not).
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f
in GNU find or*(-.)
in zsh) and will omit hidden files.
– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the%lu
inprintf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?
– terdon♦
Nov 27 '14 at 13:59
2
If the wc output is empty (for instance because the file is not readable), then that will expand to0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed.%lu
sounds like the safest bet, but it doesn't probably matter as if you have2^31
lines, that will take ages anyway.
– Stéphane Chazelas
Nov 27 '14 at 14:01
add a comment |
If you want to install fd
a really fast file finder written in Rust (you should install it, it's great to have anyway)
fd --type=file . | xargs wc -l | sort -n
Basically fd
lists the files, xargs will pass the list of files to wc
(stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n
.
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f170275%2flist-files-sorted-by-the-number-of-lines-they-contain%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use a command like this:
find /group/book/four/word/ -type f -exec wc -l + | sort -rn
find
: search for files on the path you want. If you don't want it recursive, and yourfind
implementation supports it, you should add-maxdepth 1
just before the-exec
option.exec
: tells the command to executewc -l
on every file.sort -rn
: sort the results numerically in reverse order. From greater to lower.
(that assumes file names don't contain newline characters).
Note that when passed more than one file (or with some implementations, more than one file that it can read),wc
will also print atotal
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe togrep /
to remove them.
– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because ofsort
command
– Francisco
Sep 14 '18 at 23:43
add a comment |
You should use a command like this:
find /group/book/four/word/ -type f -exec wc -l + | sort -rn
find
: search for files on the path you want. If you don't want it recursive, and yourfind
implementation supports it, you should add-maxdepth 1
just before the-exec
option.exec
: tells the command to executewc -l
on every file.sort -rn
: sort the results numerically in reverse order. From greater to lower.
(that assumes file names don't contain newline characters).
Note that when passed more than one file (or with some implementations, more than one file that it can read),wc
will also print atotal
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe togrep /
to remove them.
– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because ofsort
command
– Francisco
Sep 14 '18 at 23:43
add a comment |
You should use a command like this:
find /group/book/four/word/ -type f -exec wc -l + | sort -rn
find
: search for files on the path you want. If you don't want it recursive, and yourfind
implementation supports it, you should add-maxdepth 1
just before the-exec
option.exec
: tells the command to executewc -l
on every file.sort -rn
: sort the results numerically in reverse order. From greater to lower.
(that assumes file names don't contain newline characters).
You should use a command like this:
find /group/book/four/word/ -type f -exec wc -l + | sort -rn
find
: search for files on the path you want. If you don't want it recursive, and yourfind
implementation supports it, you should add-maxdepth 1
just before the-exec
option.exec
: tells the command to executewc -l
on every file.sort -rn
: sort the results numerically in reverse order. From greater to lower.
(that assumes file names don't contain newline characters).
edited Nov 27 '14 at 12:41
Stéphane Chazelas
311k57587945
311k57587945
answered Nov 27 '14 at 12:34
jherranjherran
2,29931328
2,29931328
Note that when passed more than one file (or with some implementations, more than one file that it can read),wc
will also print atotal
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe togrep /
to remove them.
– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because ofsort
command
– Francisco
Sep 14 '18 at 23:43
add a comment |
Note that when passed more than one file (or with some implementations, more than one file that it can read),wc
will also print atotal
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe togrep /
to remove them.
– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because ofsort
command
– Francisco
Sep 14 '18 at 23:43
Note that when passed more than one file (or with some implementations, more than one file that it can read),
wc
will also print a total
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep /
to remove them.– Stéphane Chazelas
Feb 28 '16 at 20:47
Note that when passed more than one file (or with some implementations, more than one file that it can read),
wc
will also print a total
line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep /
to remove them.– Stéphane Chazelas
Feb 28 '16 at 20:47
upvote because of
sort
command– Francisco
Sep 14 '18 at 23:43
upvote because of
sort
command– Francisco
Sep 14 '18 at 23:43
add a comment |
Non-recursive
Probably the simplest version if you don't need recursivity :
wc -l /group/book/four/word/*|sort -n
wc
counts lines (option -l
) in every (but hidden) (*
) files under /group/book/four/word/
, and sort
sorts the result (through the pipe |
) numerically (option -n
).
Recursive
Someone made a comment to this answer mentioning grep -rlc
, before to suppress it. Indeed grep
is a great alternative, especially if you need recursivity :
grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2
will count (option -c
) recursively (option -r
) lines matching (grep
) '^'
(that is, beginning of lines) in the directory /group/book/four/word/
. Then you have to replace the colon by a space, e.g. using tr
, to help sort
, which you want to sort numerically (option -n
) on the second column (option -k2
).
Update : See Stephane's comment about possible limitations and how you can actually get rid of tr
.
1
grep -c .
counts the lines that contain at least one valid character. Usegrep -c '^'
to count all the lines (will also count trailing characters after the last newline with somegrep
implementations). Note that not allgrep
implementations support a-r
and behaviour varies among those that do. You don't need to translate:
s (colon, not semicolon) to spaces forsort
. Just use-t:
. Note that that assumes file names don't contain:
or blank or newline characters.
– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't knowwc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe tosort
is really clean.
– Qcom
Sep 25 '15 at 21:21
add a comment |
Non-recursive
Probably the simplest version if you don't need recursivity :
wc -l /group/book/four/word/*|sort -n
wc
counts lines (option -l
) in every (but hidden) (*
) files under /group/book/four/word/
, and sort
sorts the result (through the pipe |
) numerically (option -n
).
Recursive
Someone made a comment to this answer mentioning grep -rlc
, before to suppress it. Indeed grep
is a great alternative, especially if you need recursivity :
grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2
will count (option -c
) recursively (option -r
) lines matching (grep
) '^'
(that is, beginning of lines) in the directory /group/book/four/word/
. Then you have to replace the colon by a space, e.g. using tr
, to help sort
, which you want to sort numerically (option -n
) on the second column (option -k2
).
Update : See Stephane's comment about possible limitations and how you can actually get rid of tr
.
1
grep -c .
counts the lines that contain at least one valid character. Usegrep -c '^'
to count all the lines (will also count trailing characters after the last newline with somegrep
implementations). Note that not allgrep
implementations support a-r
and behaviour varies among those that do. You don't need to translate:
s (colon, not semicolon) to spaces forsort
. Just use-t:
. Note that that assumes file names don't contain:
or blank or newline characters.
– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't knowwc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe tosort
is really clean.
– Qcom
Sep 25 '15 at 21:21
add a comment |
Non-recursive
Probably the simplest version if you don't need recursivity :
wc -l /group/book/four/word/*|sort -n
wc
counts lines (option -l
) in every (but hidden) (*
) files under /group/book/four/word/
, and sort
sorts the result (through the pipe |
) numerically (option -n
).
Recursive
Someone made a comment to this answer mentioning grep -rlc
, before to suppress it. Indeed grep
is a great alternative, especially if you need recursivity :
grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2
will count (option -c
) recursively (option -r
) lines matching (grep
) '^'
(that is, beginning of lines) in the directory /group/book/four/word/
. Then you have to replace the colon by a space, e.g. using tr
, to help sort
, which you want to sort numerically (option -n
) on the second column (option -k2
).
Update : See Stephane's comment about possible limitations and how you can actually get rid of tr
.
Non-recursive
Probably the simplest version if you don't need recursivity :
wc -l /group/book/four/word/*|sort -n
wc
counts lines (option -l
) in every (but hidden) (*
) files under /group/book/four/word/
, and sort
sorts the result (through the pipe |
) numerically (option -n
).
Recursive
Someone made a comment to this answer mentioning grep -rlc
, before to suppress it. Indeed grep
is a great alternative, especially if you need recursivity :
grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2
will count (option -c
) recursively (option -r
) lines matching (grep
) '^'
(that is, beginning of lines) in the directory /group/book/four/word/
. Then you have to replace the colon by a space, e.g. using tr
, to help sort
, which you want to sort numerically (option -n
) on the second column (option -k2
).
Update : See Stephane's comment about possible limitations and how you can actually get rid of tr
.
edited Nov 28 '14 at 20:26
answered Nov 27 '14 at 20:17
Skippy le Grand GourouSkippy le Grand Gourou
1,1351122
1,1351122
1
grep -c .
counts the lines that contain at least one valid character. Usegrep -c '^'
to count all the lines (will also count trailing characters after the last newline with somegrep
implementations). Note that not allgrep
implementations support a-r
and behaviour varies among those that do. You don't need to translate:
s (colon, not semicolon) to spaces forsort
. Just use-t:
. Note that that assumes file names don't contain:
or blank or newline characters.
– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't knowwc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe tosort
is really clean.
– Qcom
Sep 25 '15 at 21:21
add a comment |
1
grep -c .
counts the lines that contain at least one valid character. Usegrep -c '^'
to count all the lines (will also count trailing characters after the last newline with somegrep
implementations). Note that not allgrep
implementations support a-r
and behaviour varies among those that do. You don't need to translate:
s (colon, not semicolon) to spaces forsort
. Just use-t:
. Note that that assumes file names don't contain:
or blank or newline characters.
– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't knowwc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe tosort
is really clean.
– Qcom
Sep 25 '15 at 21:21
1
1
grep -c .
counts the lines that contain at least one valid character. Use grep -c '^'
to count all the lines (will also count trailing characters after the last newline with some grep
implementations). Note that not all grep
implementations support a -r
and behaviour varies among those that do. You don't need to translate :
s (colon, not semicolon) to spaces for sort
. Just use -t:
. Note that that assumes file names don't contain :
or blank or newline characters.– Stéphane Chazelas
Nov 28 '14 at 18:07
grep -c .
counts the lines that contain at least one valid character. Use grep -c '^'
to count all the lines (will also count trailing characters after the last newline with some grep
implementations). Note that not all grep
implementations support a -r
and behaviour varies among those that do. You don't need to translate :
s (colon, not semicolon) to spaces for sort
. Just use -t:
. Note that that assumes file names don't contain :
or blank or newline characters.– Stéphane Chazelas
Nov 28 '14 at 18:07
Thanks for posting your non-recursive solution; I didn't know
wc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort
is really clean.– Qcom
Sep 25 '15 at 21:21
Thanks for posting your non-recursive solution; I didn't know
wc
gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort
is really clean.– Qcom
Sep 25 '15 at 21:21
add a comment |
With zsh
:
lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)
We define a new sorting function lines
that replies with the number of lines in the file. And we use the o+lines
glob qualifier which together with n
(for numeric sort), defines how the results of the glob are ordered. (.
also added to only check regular files).
That makes no assumption on what character the file names may contain other than hidden files (those starting with .
) are omitted. Add the D
glob qualifier if you want them as well.
2
OP is tagged withbash
only...
– l0b0
Nov 27 '14 at 13:15
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
add a comment |
With zsh
:
lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)
We define a new sorting function lines
that replies with the number of lines in the file. And we use the o+lines
glob qualifier which together with n
(for numeric sort), defines how the results of the glob are ordered. (.
also added to only check regular files).
That makes no assumption on what character the file names may contain other than hidden files (those starting with .
) are omitted. Add the D
glob qualifier if you want them as well.
2
OP is tagged withbash
only...
– l0b0
Nov 27 '14 at 13:15
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
add a comment |
With zsh
:
lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)
We define a new sorting function lines
that replies with the number of lines in the file. And we use the o+lines
glob qualifier which together with n
(for numeric sort), defines how the results of the glob are ordered. (.
also added to only check regular files).
That makes no assumption on what character the file names may contain other than hidden files (those starting with .
) are omitted. Add the D
glob qualifier if you want them as well.
With zsh
:
lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)
We define a new sorting function lines
that replies with the number of lines in the file. And we use the o+lines
glob qualifier which together with n
(for numeric sort), defines how the results of the glob are ordered. (.
also added to only check regular files).
That makes no assumption on what character the file names may contain other than hidden files (those starting with .
) are omitted. Add the D
glob qualifier if you want them as well.
edited Nov 27 '14 at 12:50
answered Nov 27 '14 at 12:38
Stéphane ChazelasStéphane Chazelas
311k57587945
311k57587945
2
OP is tagged withbash
only...
– l0b0
Nov 27 '14 at 13:15
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
add a comment |
2
OP is tagged withbash
only...
– l0b0
Nov 27 '14 at 13:15
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
2
2
OP is tagged with
bash
only...– l0b0
Nov 27 '14 at 13:15
OP is tagged with
bash
only...– l0b0
Nov 27 '14 at 13:15
7
7
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
@l0b0 that doesn't mean that the next person who needs this will also be running bash.
– terdon♦
Nov 27 '14 at 13:40
add a comment |
You don't specify whether you also want the files in any subdirectories of /group/book/four/word
. The find
solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:
for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
If your file names can contain newlines, you can use something like:
for file in ./*; do
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'
Finally, if you do want to descend into subdirectories, you can use this in bash
4 or above:
shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
Note that versions of bash
prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh
's or tcsh
's ***/*
).
Also, all solutions above will ignore hidden files (those whose name starts with a .
, use shopt -s dotglob
to include them) and will also include the line count of symbolic links (which the find
approach will not).
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f
in GNU find or*(-.)
in zsh) and will omit hidden files.
– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the%lu
inprintf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?
– terdon♦
Nov 27 '14 at 13:59
2
If the wc output is empty (for instance because the file is not readable), then that will expand to0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed.%lu
sounds like the safest bet, but it doesn't probably matter as if you have2^31
lines, that will take ages anyway.
– Stéphane Chazelas
Nov 27 '14 at 14:01
add a comment |
You don't specify whether you also want the files in any subdirectories of /group/book/four/word
. The find
solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:
for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
If your file names can contain newlines, you can use something like:
for file in ./*; do
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'
Finally, if you do want to descend into subdirectories, you can use this in bash
4 or above:
shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
Note that versions of bash
prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh
's or tcsh
's ***/*
).
Also, all solutions above will ignore hidden files (those whose name starts with a .
, use shopt -s dotglob
to include them) and will also include the line count of symbolic links (which the find
approach will not).
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f
in GNU find or*(-.)
in zsh) and will omit hidden files.
– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the%lu
inprintf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?
– terdon♦
Nov 27 '14 at 13:59
2
If the wc output is empty (for instance because the file is not readable), then that will expand to0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed.%lu
sounds like the safest bet, but it doesn't probably matter as if you have2^31
lines, that will take ages anyway.
– Stéphane Chazelas
Nov 27 '14 at 14:01
add a comment |
You don't specify whether you also want the files in any subdirectories of /group/book/four/word
. The find
solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:
for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
If your file names can contain newlines, you can use something like:
for file in ./*; do
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'
Finally, if you do want to descend into subdirectories, you can use this in bash
4 or above:
shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
Note that versions of bash
prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh
's or tcsh
's ***/*
).
Also, all solutions above will ignore hidden files (those whose name starts with a .
, use shopt -s dotglob
to include them) and will also include the line count of symbolic links (which the find
approach will not).
You don't specify whether you also want the files in any subdirectories of /group/book/four/word
. The find
solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:
for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
If your file names can contain newlines, you can use something like:
for file in ./*; do
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'
Finally, if you do want to descend into subdirectories, you can use this in bash
4 or above:
shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n
Note that versions of bash
prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh
's or tcsh
's ***/*
).
Also, all solutions above will ignore hidden files (those whose name starts with a .
, use shopt -s dotglob
to include them) and will also include the line count of symbolic links (which the find
approach will not).
edited Dec 3 '15 at 17:04
answered Nov 27 '14 at 13:39
terdon♦terdon
133k32264444
133k32264444
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f
in GNU find or*(-.)
in zsh) and will omit hidden files.
– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the%lu
inprintf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?
– terdon♦
Nov 27 '14 at 13:59
2
If the wc output is empty (for instance because the file is not readable), then that will expand to0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed.%lu
sounds like the safest bet, but it doesn't probably matter as if you have2^31
lines, that will take ages anyway.
– Stéphane Chazelas
Nov 27 '14 at 14:01
add a comment |
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f
in GNU find or*(-.)
in zsh) and will omit hidden files.
– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the%lu
inprintf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?
– terdon♦
Nov 27 '14 at 13:59
2
If the wc output is empty (for instance because the file is not readable), then that will expand to0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed.%lu
sounds like the safest bet, but it doesn't probably matter as if you have2^31
lines, that will take ages anyway.
– Stéphane Chazelas
Nov 27 '14 at 14:01
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (
-xtype f
in GNU find or *(-.)
in zsh) and will omit hidden files.– Stéphane Chazelas
Nov 27 '14 at 13:55
Note that other differences from jherran's solution is that yours will also consider symlink to regular files (
-xtype f
in GNU find or *(-.)
in zsh) and will omit hidden files.– Stéphane Chazelas
Nov 27 '14 at 13:55
@StéphaneChazelas thanks, clarified. Why the
%lu
in printf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?– terdon♦
Nov 27 '14 at 13:59
@StéphaneChazelas thanks, clarified. Why the
%lu
in printf
? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?– terdon♦
Nov 27 '14 at 13:59
2
2
If the wc output is empty (for instance because the file is not readable), then that will expand to
0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu
sounds like the safest bet, but it doesn't probably matter as if you have 2^31
lines, that will take ages anyway.– Stéphane Chazelas
Nov 27 '14 at 14:01
If the wc output is empty (for instance because the file is not readable), then that will expand to
0
instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu
sounds like the safest bet, but it doesn't probably matter as if you have 2^31
lines, that will take ages anyway.– Stéphane Chazelas
Nov 27 '14 at 14:01
add a comment |
If you want to install fd
a really fast file finder written in Rust (you should install it, it's great to have anyway)
fd --type=file . | xargs wc -l | sort -n
Basically fd
lists the files, xargs will pass the list of files to wc
(stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n
.
New contributor
add a comment |
If you want to install fd
a really fast file finder written in Rust (you should install it, it's great to have anyway)
fd --type=file . | xargs wc -l | sort -n
Basically fd
lists the files, xargs will pass the list of files to wc
(stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n
.
New contributor
add a comment |
If you want to install fd
a really fast file finder written in Rust (you should install it, it's great to have anyway)
fd --type=file . | xargs wc -l | sort -n
Basically fd
lists the files, xargs will pass the list of files to wc
(stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n
.
New contributor
If you want to install fd
a really fast file finder written in Rust (you should install it, it's great to have anyway)
fd --type=file . | xargs wc -l | sort -n
Basically fd
lists the files, xargs will pass the list of files to wc
(stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n
.
New contributor
New contributor
answered yesterday
JustGageJustGage
1113
1113
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f170275%2flist-files-sorted-by-the-number-of-lines-they-contain%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-bash, files, shell, wc
1
Do you want the files listed by number of lines, or list the number of lines in the files or both?
ls -l
doesn't give the number of lines.ls -lS
sorts file by size with somels
implementations (size being number of bytes in the content).– Stéphane Chazelas
Nov 27 '14 at 12:59