Questions tagged [head]
Use for questions on the head command which outputs the first x number of bytes or lines of a file
125
questions
0
votes
1
answer
101
views
How to move all files whose contents begin with 0?
Here's a command to move all files whose name begin with 0 into a folder called zero :
mv [0]* zero
Question: What is a command for moving all files whose contents begin with 0 into a folder called ...
0
votes
2
answers
117
views
Extract parts of a string using head and tail only
Hello I would like to know if there is a way where I can only use head, tail, and pipes (and redirection eventually) to extract and output the start, middle characters, and end of a string
Example:
...
1
vote
2
answers
107
views
Head/Tail command to grab multiple sets of lines
I have to grab the first two lines, the lines 43 and 44, and the last 2 lines from a file in one conduct of commands.
Is there away to print those while only using head, tail and pipe commands AND ...
14
votes
1
answer
885
views
Can `head` read/consume more input lines than it outputs?
Given the following 3 scripts:
printf 'a\nb\nc\n' > file && { head -n 1; cat; } < file
printf 'a\nb\nc\n' | { head -n 1; cat; }
{ head -n 1; cat; } < <(printf 'a\nb\nc\n')
I'd ...
2
votes
6
answers
172
views
Only pipe output if at least n lines
I often want to grep output from processes that include a few header lines. But if grep would remove all actual content lines, I don't want to display the header lines. For this to work I would need a ...
2
votes
1
answer
257
views
Rounding off negative decimal and positive decimal number
I would like to round off the below numbers to nearest wholenumber using awk command and copy it to another column say col11 and col12. can anyone help
eg 1)
column5,column6,,,,,column11,column12,
...
0
votes
0
answers
37
views
Creating a script which compares the return value from an AT command
I am trying to write a shell script that can save the output of a piped process to a variable. This variable is then compared to a known string in order to discern whether or not my AT modem is ...
0
votes
3
answers
2k
views
How to pipe grep and keep headers?
In the example below, I have the right selection, but not the headers, and conversely. How can I have both?
$ ps aux | grep 'gpart'
erwann 200603 0.0 0.0 2608 72 pts/3 S 12:57 0:00 /...
0
votes
1
answer
49
views
How to output x amount of outputs in grep and skipping repeating ones
So I'm having trouble with a quick shell script I'm currently making, the script consist of putting a youtube channels name, then finding there 5 newest videos.
I'm using the youtube front-end "...
3
votes
1
answer
208
views
aborting previous steps in curl, xargs pipe when head finishes
I'm trying to download a bunch of web pages, and once I've downloaded N lines of html, I want the whole thing to stop. But instead, the previous steps in the pipe just keep going. An example to see ...
3
votes
2
answers
439
views
piping head and tail with globbing
I have several files in the same dir for which I want to print certain lines from a continuous interval, e.g., from 15th to 20th.
For a single file, this works head -n20 file.txt | tail -n6,
but how I ...
0
votes
1
answer
83
views
When storing text on a USB drive, how do I make cat not hang?
Warning: I used these commands on a drive that had nothing on it (/dev/sdb). Do not attempt this on a drive with anything important on it.
I was experimenting some, and I discovered that the ...
0
votes
2
answers
194
views
keep formatting when piping ls to head
when I call ls in ~ i get
Documents Downloads Templates Desktop Music Videos Public Pictures
If i pipe ls to head (e.g. ls | head -30) i get
Desktop
Documents
Downloads
Music
Pictures
Public
...
0
votes
1
answer
287
views
I have removed 'head' manually - how do I reinstall it? [duplicate]
It seems that I've removed head manually from my /usr/bin/
a couple of months ago. Now that I chance to need it I don't have it.
How do I reinstall it without reinstalling the whole distro?
My ...
-1
votes
4
answers
140
views
Combining different terms in third column into one row according to sample code
I have some genomic data with three columns. Column 1 is samples, Column 2 is cancer type, and Column 3 is mechanism. Each sample has one or more gene mechanisms assigned to it (e.g. 6 ecDNA) in the ...
2
votes
1
answer
376
views
How to properly use tail to concatenate all hidden files [duplicate]
Issue
I want to be able to :
concatenate all files in a directory (regular and hidden),
but I would also like to display the title of each file at the beginning of each concatenation.
I found some ...
-1
votes
1
answer
177
views
"head -c " output displays before prompt
head -c 3 sample.txt
The output will display before username in the prompt.
Theuser@localhost:/home$
The is not being printed after $.
Why is my terminal doing this?
1
vote
1
answer
595
views
How to paste multiple commands output into single output file
I have txt file that I have to swap the first paragraph with last one. I did it but now I don't know how to paste everything in a new txt file.
This is my command
tail -14 gl.txt ; head -n 74 gl.txt | ...
3
votes
0
answers
1k
views
How to limit results from ls on sftp
I am using OpenSSH_7.4p1 on Centos 7.7.1908 and want to limit the file results from SFTP.
I have logged into a server via sftp and trying to run ls -t | head -1 i.e. get the most recent file.
However, ...
-1
votes
1
answer
1k
views
ls |head (offset?)
Is there a way to paginate the ls command? For example, to get the first 50 items I can do:
$ ls -U |head -50
How would I then do:
$ ls -U | head -50 "offset 50"
Or, does this require to ...
2
votes
1
answer
2k
views
extract 5 first lines of a text file to a variable
I'm wondering if it's possible to "extract" the first 5 lines of a textfile to a single variable (not an array)
for example:
head -5 test.txt >$variable (which of course doesn't work)
I'm ...
0
votes
6
answers
1k
views
Printing only the file name after searching a pattern in the first line (using grep and head)
So I'm a beginner and i have a project due next week. I have to print only the filename of the files that contain #!/bin/bash on the first line. So far I tried this
head -n 1 $filename | grep -l "...
4
votes
6
answers
6k
views
Move specific line from a position to another
I'm using the ubuntu terminal, I need to move a specific line in a file ( 11th position ) to the first line and then transfer the final result into a new file. The original file contains hundreds of ...
3
votes
3
answers
3k
views
Print first word of the output
I've been looking around and haven't found what I'm trying.
I have to say I'm petty poor with grep, sed and awk though.
I have an alias:
alias upgradable='apt list --upgradable'
and it gets me ...
0
votes
1
answer
237
views
Best way to set a conditional output filter (head or output "as is") [duplicate]
For testing purpose, I want to activate or not a filtering with the command head.
For example, the command below could outputs a lot of lines
#!/bin/bash
produce_a_lot_of_lines
I want to set on ...
0
votes
1
answer
3k
views
syntax for using head with filename stored in variable [duplicate]
I'm trying to store the first 5 lines from a file in the variable url. But I get an error
-n5: command not found
fn_all_urls stores the path to the file /home/urls.txt
My Line is:
url=head -n5 ${...
1
vote
2
answers
327
views
head not returning n lines
When I run the command head -n 445 /etc/snort/snort.conf | nl I expect lines 1-445 to be returned. However, only up to line 371 is returned:
[snip]
370 preprocessor dcerpc2_server: default, policy ...
0
votes
2
answers
7k
views
Show particular lines using only head and tail
I want to show the 3th and the 7th lines in a file only using commands head and tail (I don't want to show the lines between the 3th and the 7th).
-1
votes
2
answers
1k
views
Creating random file in just one line but with specific length
I want to make a file (random content) but with just one 1 MB large line inside it
I have tried using this command but it didn't work :
head -n 1 -c 1m /dev/urandom > /sdcard/file.txt
How ...
0
votes
2
answers
1k
views
List top 5 processes for a specific user
I am trying to get the top five processes of a specific user in a bash:
ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6 | grep <username>
However, this selects the top 5 processes ...
8
votes
3
answers
1k
views
How can I watch the 17th (or last, if less) line in files of a folder?
I'm currently using
watch head -n 17 *
which works, but also shows all lines up to the 17th. Basically, I would like to only show the last line for each file that is shown with my current approach. ...
4
votes
2
answers
951
views
How to display first page of file in terminal?
I would like to display as much from a file as possible which still fits in the terminal window, i.e. like a head with a dynamic number of lines.
If there are no lines which wrap to multiple lines ...
0
votes
1
answer
638
views
"tail" Is Returning the Wrong Requested Number Lines
I'm trying to use the following approach to subset the output of a manual:
man dig | nl | tail -n +389 | head -n 6
However, the output starts at line 304, not line 389. Doing some research, it seems ...
6
votes
2
answers
12k
views
Display line number in head and tail command like `cat -n`
cat without -n
user@linux:~$ cat /etc/sysctl.conf | head -4
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl....
0
votes
2
answers
564
views
How to extract lines knowing start and end lines
I have a file with 1 million lines.
I want to extract lines from line 10001 to 500000
How to do this?
0
votes
4
answers
2k
views
How to find filenames, sort it alphabetically then only head the first 3 characters?
I want to find all files in the current directory, sort them alphabetically before processing their content to extract the first 3 characters in each of them. Doing so will create a sentence that I ...
1
vote
1
answer
377
views
Is it possible to run head & use nl to number the lines?
When i ran head file.txt && nl file.txt it did each command in order of occurance (which makes sense). Is it possible to have the head display with numbered lines, so that this:
word
word
...
0
votes
1
answer
718
views
Getting different results running same commands step by step at a terminal and within a script [duplicate]
pid=$(pgrep 'engrampa') #Get the PID of the engrampa processes .
killpid=$(echo $pid | head -1) #Get only the first line of the $pid variable and put into a new variable called $killpid.
kill $killpid
...
0
votes
2
answers
512
views
Find number of cores available in a given node
I need to fix the following piece of code. The code aims to determine the number of cores available in a given node.
Step 1: Get the number of cores with
TC=$(grep -c process /proc/cpuinfo)
Step 2:...
3
votes
4
answers
191
views
Search recursive for files that contains a specific combination of strings on the first line
I need to find all files that cointains in the first line the strings: "StockID" and "SellPrice".
Here is are some exemples of files :
1.csv :
StockID Dept Cat2 Cat4 Cat5 Cat6 Cat1 ...
-1
votes
2
answers
442
views
How to print the first line which include special word [closed]
Print the first 123 below XXX
AAA
BCB
DDD
123
123
XXX
CCC
123
123
MSM
123
4
votes
1
answer
11k
views
Hex dump only the beginning and the end of a file with xxd [duplicate]
I am looking for a command to display three .png files using xxd, displaying the first two and last two lines of the hexadecimal for each.
I have used the command below to display one file at a time ...
0
votes
1
answer
279
views
z flag for head command in centos 7 [duplicate]
I was reading this answer by @dessert: https://askubuntu.com/a/990771/853133 about how to delete old backup folders from directory using cron in Ubuntu. The command given was:
find /BACKUPDIR/ -...
7
votes
1
answer
12k
views
Why isn't "head -1" equivalent with "head -n -1" but instead it's the same as "head -n 1"?
head -num is the same as head -n num instead of head -n -num (where num is any number)
Example:
$ echo -e 'a\nb\nc\nd'|head -1
a
$ echo -e 'a\nb\nc\nd'|head -n 1
a
$ echo -e 'a\nb\nc\nd'|head -n ...
2
votes
1
answer
494
views
How can I execute an equivalent of `head -z` when I don't have the `-z` option available?
I need head -z for a script (off-topic, but the motivation can be found in this question), but in my CoreOS 835.13.0 I get head: invalid option -- 'z'.
Full head --help output:
Usage: head [OPTION].....
1
vote
2
answers
849
views
Stream contents of directory instead of reading all items [closed]
Is there a unix command I can use to stream the items/contents of a directory? Using Node.js, we can read everything into memory with:
fs.readdir(dir, (err, items) => {});
but I am looking to ...
0
votes
4
answers
188
views
find correct log file out of thousands using head and fgrep
I have thousands of log files - all very large. One of them is going to have an IP address I'm looking for. I know it's very likely to occur near the top of the file. I can do:
head -n 500 *.log | ...
0
votes
1
answer
2k
views
Issues with head command
I am trying to get the top 20 lines of ldconfig -p and piping them to a .txt file. I am doing the below syntax:
head -n 20 ldconfig -p > ex4-2-4a.txt
I am getting an error for invalid option for ...
0
votes
4
answers
4k
views
Quick head/tail command question
This is the output from the file I'm using:
aaaaa
bbbbb
ccccc
ddddd
eeeee
fffff
ggggg
hhhhh
iiiii
jjjjj
I know using tail -n 5 filename will display this:
fffff
ggggg
hhhhh
iiiii
jjjjj
How do I ...
6
votes
2
answers
1k
views
Truncate output after X lines and print message if and only if output was truncated
Say I only need the first 5 lines of an output for logging purposes. I also need to know if and when the log has been truncated.
I am trying to use head to do the job, the seq command below outputs ...