Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

14 votes
12 answers
11414 views
Count the number of blank lines at the end of file
I have a file with blank lines at the end of the file. Can I use `grep` to count the number of blank lines at the end of the file with the file name being passed as variable in the script?
I have a file with blank lines at the end of the file. Can I use grep to count the number of blank lines at the end of the file with the file name being passed as variable in the script?
Raghunath Choudhary (153 rep)
Nov 30, 2017, 09:43 AM • Last activity: Jun 14, 2025, 08:57 PM
2 votes
1 answers
131 views
grep -c gives a different count to grep | wc -l
I expected these two commands to give the same count of how many lines in my file contain a letter: ``` grep -c '[A-Z,a-z]' archive_for_TO.050225 11873 grep '[A-Z,a-z]' archive_for_TO.050225 | wc -l 11859 ``` The file is too large to post here, but I wonder if there are some common cases/explanation...
I expected these two commands to give the same count of how many lines in my file contain a letter:
grep -c '[A-Z,a-z]' archive_for_TO.050225
11873
grep '[A-Z,a-z]' archive_for_TO.050225 | wc -l
11859
The file is too large to post here, but I wonder if there are some common cases/explanations why the second command apparently finds 14 fewer matching lines than the first. For example, does it indicate that the text file has some inconsistencies in how lines are separated (character return or line feed or both)?
Tim Osborn (31 rep)
Feb 13, 2025, 12:22 PM • Last activity: Mar 11, 2025, 02:01 PM
4 votes
4 answers
34665 views
How to display the number of lines, words and characters, on separate lines?
I want to display the number of lines, words and characters, in a file, on separate lines. I don't know any more than using `wc filename` for this.
I want to display the number of lines, words and characters, in a file, on separate lines.  I don't know any more than using wc filename for this.
Gaurav (143 rep)
Sep 8, 2014, 07:15 AM • Last activity: Feb 26, 2025, 08:10 AM
15 votes
4 answers
42556 views
How to skip the first line of my output?
Here is the issue, I would like to count the number of jobs I have in the hpc, but it is not one of the readily provided features. So I made this simple script ```csh squeue -u user_name | wc -l ``` where `squeue` prints all the jobs like the following ```csh > squeue -u user_name JOBID PARTITION NA...
Here is the issue, I would like to count the number of jobs I have in the hpc, but it is not one of the readily provided features. So I made this simple script
squeue -u user_name | wc -l
where squeue prints all the jobs like the following
> squeue -u user_name
   JOBID PARTITION NAME     USER ST       TIME  NODES NODELIST(REASON)
 8840441    theory cteq      fxm PD       0:00      1 (Resources)
 8840442    theory cteq      fxm PD       0:00      1 (Priority)
 8840443    theory cteq      fxm PD       0:00      1 (Priority)
 8840444    theory cteq      fxm PD       0:00      1 (Priority)
which would be piped to wc and the number of lines would be counted. However, the first line is not an entry of the job. How may I instruct wc to skip the first line when counting? Or should I just take the output of wc and minus one to it? Thanks in advance!
zyy (321 rep)
Jan 10, 2020, 04:04 AM • Last activity: Oct 17, 2024, 03:26 PM
4 votes
1 answers
133 views
Why does "cat | wc -c" shows different size than "ls -l" for a specific file?
The distro is Ubuntu 22.04 and I'm running a newly-created `ext4` filesystem that I have copied my setup to (using rsync on a different machine). I was running a manually written (python) filesystem performance test, which worked normally on different machines. Absolutely acidentally I discovered so...
The distro is Ubuntu 22.04 and I'm running a newly-created ext4 filesystem that I have copied my setup to (using rsync on a different machine). I was running a manually written (python) filesystem performance test, which worked normally on different machines. Absolutely acidentally I discovered some sort of incorrect behavior in wc (or ls) which manifests itself only for a particular file on my system - as far as I have discovered now - /usr/bin/pkexec. If this is related, the box is running official Ubuntu's coreutils version 8.32-4. This is an example of the action I'm talking about for any different file:
$ ls -l /usr/bin/top
-rwxr-xr-x 1 root root 379389 Jun 11 12:04 /usr/bin/top
$ cat /usr/bin/top | wc -c
379389
$
Note the size shown by ls and wc. It is the same. Now, there is a file /usr/bin/pkexec. Which is, according to man page,
pkexec - Execute a command as another user
This happens with it
$ ls -l /usr/bin/pkexec
-rwsr-xr-x 1 root root 519851 Jun  11 12:04 /usr/bin/pkexec
$ cat /usr/bin/pkexec | wc -c
32145
$
The size is different. Quite really different.
I thought that it is related to some filesystem corruption, and I ran fsck in recovery mode. Nothing have changed.
Then I took the disk to the machine where the rsync was originally done. (It is running Arch Linux). I mounted the Ubuntu's root partition there. Wow!
# ls -l /mnt/disk/usr/bin/pkexec
-rwsr-xr-x 1 root root 519851 Jun  11 12:04 /mnt/disk/usr/bin/pkexec
# cat /mnt/disk/usr/bin/pkexec | wc -c
519851
#
Here the sizes are shown equal. I do not understand. I have a different Ubuntu box with the same version where the information about the above file is this:
$ ls -l /usr/bin/pkexec
-rwsr-xr-x 1 root root 32145 Jul  26 14:45 /usr/bin/pkexec
$ cat /usr/bin/pkexec | wc -c
32145
$
The size matches here. And I see that the correct size for this file in this version of Ubuntu is "32145". But on the first machine I see sizes which don't even match each other in wc and ls. I had these explanations possibly - Machines had different updates installed. But why are the sizes shown different between utilities? - This is some sort of runtime protection for this /usr/bin/pkexec binary file, as I see that it's SUID - maybe OS is protecting it by showing incorrect data to users? But why this behavior is seen only on one machine then and not the others? Thanks for help and explanations. I think I don't understand something important about Linux. **P.S.** The problem also exists if I read dir "/usr/bin" in some langs like Python, and retrieve metadata of files. Then I read each file and check the result length. With pkexec - in metadata it's 519851 bytes. But if I read the file , it's much shorter - only 32145 bytes. Thanks.
melonfsck - she her (150 rep)
Sep 16, 2024, 10:08 PM • Last activity: Sep 18, 2024, 08:49 PM
0 votes
3 answers
98 views
Pass output of command line to awk variable
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading [these](https://unix.stackexchange.com/questions/475008/command-line-argument-in-awk) [questions](http://mywiki.wooledge.org/BashFAQ/082), I thought this could work: ```lang-shell awk...
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading [these](https://unix.stackexchange.com/questions/475008/command-line-argument-in-awk) [questions](http://mywiki.wooledge.org/BashFAQ/082) , I thought this could work:
-shell
awk -v num=$(wc -l my_first_file.bed) '{print $1, $2, $3, $4/num}' my_other_file.bed
but it throws this error:
awk: cmd. line:1: my_first_file.bed
awk: cmd. line:1:              ^ syntax error
Protecting the . with a backslash doesn't change anything, nor does using backticks instead of $(). How can I use the output of wc -l as an awk variable? This will all be happening inside a snakemake pipeline, so I am somewhat limited in terms of flexibility. Contents of my_other_file.bed:
chrUn_KI270548v1	0	50	0.00000
chrUn_KI270548v1	50	192	1.00000
chrUn_KI270548v1	192	497	0.00000
chrUn_KI270548v1	497	639	1.00000
chrUn_KI270548v1	639	723	0.00000
chrUn_KI270548v1	723	860	1.00000
chrUn_KI270548v1	860	865	2.00000
chrUn_KI270548v1	865	879	1.00000
chrUn_KI270548v1	879	991	2.00000
chrUn_KI270548v1	991	1002	3.00000
chrUn_KI270548v1	1002	1021	2.00000
chrUn_KI270548v1	1021	1093	1.00000
chrUn_KI270548v1	1093	1133	2.00000
chrUn_KI270548v1	1133	1222	1.00000
chrUn_KI270548v1	1222	1235	2.00000
chrUn_KI270548v1	1235	1364	1.00000
chrUn_KI270590v1	0	16	4.00000
chrUn_KI270590v1	16	46	5.00000
chrUn_KI270590v1	46	48	6.00000
chrUn_KI270590v1	48	95	7.00000
chrUn_KI270590v1	95	117	8.00000
chrUn_KI270590v1	117	130	9.00000
chrUn_KI270590v1	130	136	8.00000
chrUn_KI270590v1	136	138	7.00000
chrUn_KI270590v1	138	139	6.00000
Whitehot (245 rep)
Jun 20, 2024, 09:21 AM • Last activity: Jun 20, 2024, 01:19 PM
0 votes
2 answers
73 views
wc to get character count of a file but exclude the initial characters of each line and the first line of the file?
I have some text files the looks something like this: Introduction and some meta data [00:00.000 --> 00:04.380] Lorem ipsum dolor sit amet, consectetur adipiscing elit. [00:04.980 --> 00:07.200] Sed mattis varius ligula vel egestas. I want to count the characters but exclude the first line and the t...
I have some text files the looks something like this: Introduction and some meta data [00:00.000 --> 00:04.380] Lorem ipsum dolor sit amet, consectetur adipiscing elit. [00:04.980 --> 00:07.200] Sed mattis varius ligula vel egestas. I want to count the characters but exclude the first line and the time stamp, that is, only count the characters in Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mattis varius ligula vel egestas. The length of the timestamps varies (there might be hours as well, in the example above it is just minutes). How do I do this?
d-b (2047 rep)
May 28, 2024, 06:35 PM • Last activity: May 29, 2024, 12:39 AM
0 votes
0 answers
53 views
When using 'wc \-l' to count the number of file lines, the result is less one
I want to count the number of file lines, so i use the command `wc -l file`. However, I notice that the result is incorrect which one less than the real number of lines. For example, the number of lines is 96: [![enter image description here][1]][1] But the result is 95 when using `wc -l`[![enter im...
I want to count the number of file lines, so i use the command wc -l file. However, I notice that the result is incorrect which one less than the real number of lines. For example, the number of lines is 96: enter image description here But the result is 95 when using wc -lenter image description here So what's wrong with the command and is there any other method better to count the number of lines? By the way, my Linux distribution is RedHat and the shell is bash.
Bioinfotec (1 rep)
Mar 8, 2024, 05:20 AM • Last activity: Mar 8, 2024, 07:12 AM
0 votes
0 answers
75 views
Why is wc with multiple paths much faster than one by one execution?
Running `wc` with multiple files is an order of magnitude faster than running it file by file. For example: ``` > time git ls-files -z | xargs -0 wc -l > ../bach.x real 0m2.765s user 0m0.031s sys 0m2.531s > time git ls-files | xargs -I {} wc -l "{}" > ../onebyone.x real 0m57.832s user 0m0.156s sys 0...
Running wc with multiple files is an order of magnitude faster than running it file by file. For example:
> time git ls-files -z | xargs -0 wc -l > ../bach.x

real    0m2.765s
user    0m0.031s
sys     0m2.531s


> time git ls-files | xargs -I {} wc -l "{}" > ../onebyone.x

real    0m57.832s
user    0m0.156s
sys     0m3.031s
*(The repo contains ~10_000 files so xargs runs wc few times, not just once, but that's not material in this context)* In my naivety I think that wc needs to open and process each file so the speedup must be from multi-threading only. However, I've read that there may be some extra files system magic going on here. Is three file system magic going on here or is it all multi-threading or is it something else? ---- ### Startup penalty Following up on @muru's comment I can see that a) a single execution is ~8ms and b) running wc in a loop scales linearly:
> time wc -l ../x.x > /dev/null

real    0m0.008s
user    0m0.000s
sys     0m0.016s


>  time for run in {1..10}; do wc -l ../x.x; done > /dev/null

real    0m0.076s
user    0m0.000s
sys     0m0.000s

> time for run in {1..100}; do wc -l ../x.x; done > /dev/null

real    0m0.689s
user    0m0.000s
sys     0m0.063s
Since the multi-file run is much faster per file (*10_000f@3_000ms* => *1f@0.3ms*) there seem to be a (huge?) startup penalty for wc which is not related to actually counting the \ns.
tmaj (101 rep)
Mar 8, 2024, 06:27 AM • Last activity: Mar 8, 2024, 07:05 AM
0 votes
1 answers
2340 views
word count redirect
[![enter image description here][1]][1] I can't seem to answer this question: >Use the command `wc` to count the number of words in the file `/course/linuxgym/gutenberg/0ws0310.txt`. Store the exact output of the wc command when used with the "word counts" option into the file `count.txt`. Ensure th...
enter image description here I can't seem to answer this question: >Use the command wc to count the number of words in the file /course/linuxgym/gutenberg/0ws0310.txt. Store the exact output of the wc command when used with the "word counts" option into the file count.txt. Ensure that the filename within the file contains the full pathname.
william (1 rep)
Mar 30, 2020, 01:43 PM • Last activity: Feb 10, 2024, 11:08 AM
0 votes
2 answers
210 views
Counting Files in Remote location using sftp in shell script
Getting Error with the below command to check on the Remote directory a specific type of files. The requirement is to get the count of the specific files. file_exists=$(sftp $FTP_UNAME@$FTP_HOST ls *$FILE_ID*.txt | wc -l) Is this not properly written? Please help... Below is the Usage Tip - usage: s...
Getting Error with the below command to check on the Remote directory a specific type of files. The requirement is to get the count of the specific files. file_exists=$(sftp $FTP_UNAME@$FTP_HOST ls *$FILE_ID*.txt | wc -l) Is this not properly written? Please help... Below is the Usage Tip - usage: sftp [-1246aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher] [-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-R num_requests] [-S program] [-s subsystem | sftp_server] host sftp [user@]host[:file ...] sftp [user@]host[:dir[/]] sftp -b batchfile [user@]host
MALABIKA GARAI (1 rep)
Feb 6, 2024, 03:23 PM • Last activity: Feb 6, 2024, 06:30 PM
9 votes
7 answers
5826 views
How to efficiently split up a large text file wihout splitting multiline records?
I have a big text file (~50Gb when gz'ed). The file contains ``4*N`` lines or ``N`` records; that is every record consists of 4 lines. I would like to split this file into 4 smaller files each sized roughly 25% of the input file. How can I split up the file at the record boundary? A naive approach w...
I have a big text file (~50Gb when gz'ed). The file contains `4*N lines or N` records; that is every record consists of 4 lines. I would like to split this file into 4 smaller files each sized roughly 25% of the input file. How can I split up the file at the record boundary? A naive approach would be `zcat file | wc -l to get the line count, divide that number by 4 and then use split -l file`. However, this goes over the file twice and the line-counte is extremely slow (36mins). Is there a better way? This comes close but is not what I am looking for. The accepted answer also does a line count. **EDIT:** The file contains sequencing data in fastq format. Two records look like this (anonymized): @NxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxGCGA+ATAGAGAG xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTTTATGTTTTTAATTAATTCTGTTTCCTCAGATTGATGATGAAGTTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + AAAAA#FFFFFFFFFFFFAFFFFF#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF /dev/null`` takes 31mins. **EDIT3:** Onlye the first line starts with `@`. None of the others will ever. See here . Records need to stay in order. It's not ok to add anything to the resulting file.
Rolf (932 rep)
Jun 16, 2015, 07:55 AM • Last activity: Nov 27, 2023, 07:55 AM
0 votes
0 answers
72 views
How to extract the number of lines from the wc command output
When running `wc -l models.txt`, we get the output which looks like `2113 models_work.txt`. I wonder how to get the `2113` from that output using the `cut` command or something along those lines. It doesn't work when combined with the `cut` command like `wc -l models.txt | cut -f1`. Any help would b...
When running wc -l models.txt, we get the output which looks like 2113 models_work.txt. I wonder how to get the 2113 from that output using the cut command or something along those lines. It doesn't work when combined with the cut command like wc -l models.txt | cut -f1. Any help would be appreciated! Tks!
Tung (171 rep)
Nov 1, 2023, 10:05 AM
0 votes
3 answers
1931 views
Remove new line, space from file
I have many files in a directory each like so: AAA AA AAAAAA A AAAA I want to end up with this: AAAAAAAAAAAAAAAA So that when I run: find ./ -name '*' -exec wc -m {} + I get back 16, not 20+ depending on how many new line/spaces are counted. Basically, I want to remove EVERYTHING from a file unless...
I have many files in a directory each like so: AAA AA AAAAAA A AAAA I want to end up with this: AAAAAAAAAAAAAAAA So that when I run: find ./ -name '*' -exec wc -m {} + I get back 16, not 20+ depending on how many new line/spaces are counted. Basically, I want to remove EVERYTHING from a file unless it is a letter.
Chris (1 rep)
Apr 10, 2019, 04:06 PM • Last activity: Jul 10, 2023, 05:44 PM
0 votes
3 answers
2138 views
"wc -l" takes very long for my files, is there a faster alternative?
I need help in writing script that outputs the number of lines in each of a number of large files. `wc -l` is taking a long time, so I'm looking to use `awk` command to display the last line number for all files that match `abd*2020-09-21*` * `ls -l abd*2020-09-21*` displays 22 long (big) files * I...
I need help in writing script that outputs the number of lines in each of a number of large files. wc -l is taking a long time, so I'm looking to use awk command to display the last line number for all files that match abd*2020-09-21* * ls -l abd*2020-09-21* displays 22 long (big) files * I need to find the wc -l result of each file. * Currently im using
wc -l abd*2020-09-21.txt > CCNC_UNIX_COUNTS.txt
but this is very time consuming. I need help in improving it. I tried
sed -n '$='
Now I need this to work in loop for all files matchinv abd*2020-09-21.txt and output the results to a file CCNC_UNIX_COUNTS
Rekha Mallam (11 rep)
Sep 28, 2020, 02:59 PM • Last activity: Jul 4, 2023, 10:17 AM
6 votes
3 answers
5932 views
Running external commands in vim
It seems that if I run the following inside the `vim`: `:w !wc -w` I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the word count of a paragraph and not of the whole file?
It seems that if I run the following inside the vim: :w !wc -w I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the word count of a paragraph and not of the whole file?
Cratylus (529 rep)
Jun 16, 2013, 10:36 AM • Last activity: Jun 2, 2023, 02:34 PM
51 votes
6 answers
38346 views
List files sorted by the number of lines they contain
How can I list the number of lines in the files in `/group/book/four/word`, sorted by the number of lines they contain? The `ls -l` command lists the files but does not sort them.
How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain? The ls -l command lists the files but does not sort them.
Ken R (549 rep)
Nov 27, 2014, 12:31 PM • Last activity: May 16, 2023, 02:30 PM
0 votes
1 answers
54 views
Moves files with the same name to a directory and writes the counted number to the directory name
find /volume1/file/* -type f \( -name "*DF*" -a -name "*LIVE*" \) -print0 | while IFS= read -d '' file do # extract the name of the directory to create dirName="${file%.E*}" count=$(find "$file" -type f | wc -l;) # create the directory if it doesn't exist [[ ! -d "$dirName$count" ]] && mkdir "$dirNa...
find /volume1/file/* -type f \( -name "*DF*" -a -name "*LIVE*" \) -print0 | while IFS= read -d '' file do # extract the name of the directory to create dirName="${file%.E*}" count=$(find "$file" -type f | wc -l;) # create the directory if it doesn't exist [[ ! -d "$dirName$count" ]] && mkdir "$dirName$count" mv "$file" "$dirName$count" done Hi there, I'm using another coder's shell script, but I modified it because I wanted to add more features, but it doesn't work modified part count=$(find "$file" -type f | wc -l;) For example, there are about 10,000 files in my Linux HDD, and among those 10,000 files, I read the file title before ".E" and create a directory with that title to move the files. Here, I want to put the number of files in that directory into the directory name. EX) artist.E01.DF.LIVE artist.E02.DF.LIVE artist.E03.DF.LIVE artist.E04.DD.LIVE directory name = "artist3" I want to do this, but with the script I modified, only 1 is counted
anisphia (3 rep)
Mar 13, 2023, 08:46 AM • Last activity: Mar 13, 2023, 11:53 AM
6 votes
2 answers
967 views
Why does wc and stat produce different results for /proc/[pid]/cmdline?
I am trying to understand why `wc` and `stat` report different things for `/proc/[pid]/cmdline`. `wc` says my shell's `cmdline` file is 6 bytes in size: ``` $ wc --bytes /proc/$$/cmdline 6 /proc/10425/cmdline ``` `stat` says the file is 0 bytes in size: ``` $ stat --format='%s' /proc/$$/cmdline 0 ``...
I am trying to understand why wc and stat report different things for /proc/[pid]/cmdline. wc says my shell's cmdline file is 6 bytes in size:
$ wc --bytes /proc/$$/cmdline
6 /proc/10425/cmdline
stat says the file is 0 bytes in size:
$ stat --format='%s' /proc/$$/cmdline
0
file agrees with stat:
$ file /proc/$$/cmdline
/proc/10425/cmdline: empty
cat gives this output:
$ cat -vE /proc/$$/cmdline
-bash^@
All of this is on Linux rather than on any other *nix OS. Do the stat and wc programs have a different algorithm for computing the number of bytes in a file?
Shane Bishop (509 rep)
Feb 1, 2023, 07:34 PM • Last activity: Feb 2, 2023, 09:50 AM
0 votes
2 answers
141 views
How do I count all the files at different subfolder using for loop
I want to know could I wc -l files are in subfolder.If only one file I can use code like below. ``` find ./calss{1..20}/ -name 'C1.student' | xargs wc -l ``` In fact, I have 20 folders , every folder contain C1.student to C50.student files.I want to use ```wc``` to count multiple files at different...
I want to know could I wc -l files are in subfolder.If only one file I can use code like below.
find ./calss{1..20}/ -name 'C1.student' | xargs wc -l
In fact, I have 20 folders , every folder contain C1.student to C50.student files.I want to use
to count multiple files at different sub folders. I try this code but got all 0, Did I miss something ? Thanks for your time.
for i in $(seq 1 50); do
find ./calss{1..20}/ -name 'C${i}.student' | xargs wc -l
done
Roq (15 rep)
Jan 11, 2023, 05:45 AM • Last activity: Jan 11, 2023, 11:55 AM
Showing page 1 of 20 total questions