Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [head]

Use for questions on the head command which outputs the first x number of bytes or lines of a file

0
votes
1answer
28 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
2answers
31 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
4answers
112 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
2answers
50 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
2
votes
1answer
130 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
1answer
39 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/ -...
0
votes
1answer
76 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 ...
3
votes
1answer
59 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
2answers
38 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
4answers
53 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
1answer
86 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
4answers
185 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
2answers
359 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 ...
0
votes
0answers
27 views

tail/head piping remove newlines [duplicate]

If have the following file /tmp/f1.txt file coming from the stdout redirection of the result of a PostgreSQL bash script execution : id ----- 517 518 519 520 521 522 (6 rows) INSERT 0 6 I ...
15
votes
3answers
1k views

head eats extra characters

The following shell command was expected to print only odd lines of the input stream: echo -e "aaa\nbbb\nccc\nddd\n" | (while true; do head -n 1; head -n 1 >/dev/null; done) But instead it just ...
2
votes
1answer
1k views

Why does head; tail on a large file sometimes take a long time and sometimes not?

I am writing a bash script that reads the first 10 lines and the last 10 lines of a .txt file. It looks for started (head) and completed (tail) and compares the number of occurrences using grep. The ...
1
vote
1answer
3k views

Is cat the only reliable command that can read data from serial?

I have a scale that continuously send data via serial port, 2 times per second. The only reliable way on reading this data happened to be cat command. The following works: cat /dev/ttyUSB0 But the ...
1
vote
2answers
98 views

How to pass the contents of a file to an option/parameter of a function

I have a set of .txt file-pairs. In each pair of files, File1 contains a single integer and File2 contains many lines of text. In the script I'm writing, I'd like to use the integer in File1 to ...
4
votes
1answer
4k views

sort: write failed | broken pipe

Good evening, The following is a piece of the code I'm using in a script. Launching from a SSH sesssion works fine, however, when it runs via cron, it displays broken pipe errors on screen. I can't ...
0
votes
1answer
77 views

Retrieve file from list as nc input

I'm trying to create a simple script (one liner, actually) that takes the content of the a file, makes it random, and provides the output to netcat as the connection destination. This is what I ...
0
votes
3answers
504 views

How can I append a command

I can't remember how to append a command to a shell script. I searched for append, add, concat, and more without success. My google-fu doesn't help today. Basically I have belly = tail -n +"$HEAD" "...
2
votes
3answers
206 views

Using pipes to list the first two and last two users on the system?

I have to use the who command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like: who | head -5 | tail -2 . ...
0
votes
1answer
140 views

How to get nth to n+ath bytes across all rows form a text file in *nix?

I have a large text file (>200MB). I want to read [n, n+a] bytes across all rows. Suppose there are 1000 rows in the original text file. The output file would be 1000 rows. What I know head -c349 ...
2
votes
1answer
64 views

Use head command or similar to determine when bytes change in a file

If we use the tail command we can listen for events representing changes to the files length (in bytes). I am looking for a way to listen for events when the content of the file changes but the ...
0
votes
1answer
609 views

Running multiple commands for multiple files on one line [duplicate]

As the title suggests I'm attempting to run multiple commands on the same line for multiple files. I have little experience with Linux and I've run into an issue with this command: tail -n +3 "/usr/...
1
vote
1answer
48 views

10th previous line from a match [duplicate]

How can I get the 10th previous line from a match? I think using grep with the option B and then piping it into head would work, but is there a simpler way to do this? For example, if I had a text ...
6
votes
3answers
2k views

limit find output AND avoid signal 13

I have a directory with ~1M files and need to search for particular patterns. I know how to do it for all the files: find /path/ -exec grep -H -m 1 'pattern' \{\} \; The full output is not desired (...
0
votes
1answer
3k views

cat filename | cut -f2 | head -1 > newfile contains more characters than expected

When I run: cat filename | cut -f3 | head -1 I get the following result: apple However when I save this to a file by using: cat filename | cut -f3 | head -1 > newfile I then open this using ...
-1
votes
1answer
678 views

how to find the output for a file if head number and tail number is given

If file foo contains 9 lines, each of which is the one-digit line number of the line in the file (1 through 9), what is the output on your screen of this command: sort foo foo | tail -2 | head -1 ...
0
votes
2answers
148 views

Copying latest three files from remote server

There is a collection of .doc files, in addition to other types of files, on a remote server (which supports SCP). I saw a script: FILE=`ssh abc@xyz ls -ht /tmp/*.doc | head -n 1` rsync -avz -e ssh ...
2
votes
1answer
77 views

How can I display the first few line of a file with updates?

When watching the last few lines of a text files for changes, I can use tail -f to continue updating my display. How can I achieve the same thing with head? Is there some solution which behaves like ...
0
votes
3answers
372 views

echo lines of file - but no more than N characters per line [duplicate]

I would like to print the tail of a file (could be also head or cat in general) to the screen but restrict the number of characters per line. So if a file contains ... abcdefg abcd abcde abcdefgh .....
13
votes
1answer
1k views

Alternative to 'head' that doesn't exit?

I need a utility that will print the first n lines, but then continue to run, sucking up the rest of the lines, but not printing them. I use it to not overwhelm the terminal with the output of a ...
0
votes
1answer
405 views

Recursive Directory search, Showing most recent file in folder

Can someone point out where am going wrong please. Am doing a directory search (Based on the folder structure and file type) The folder structure is the same for every customer. Just the customer ...
3
votes
2answers
128 views

why does it take so long to read the top few lines of my file?

I have a huge text file ~ 33Gb and due to its size, I wanted to just read the first few lines of the file to understand how the file is organized. I have tried head, but it took like forever to even ...
14
votes
3answers
5k views

How can I work with binary in bash, to copy bytes verbatim without any conversion?

I am ambitiously trying to translate a c++ code into bash for a myriad of reasons. This code reads and manipulates a file type specific to my sub-field that is written and structured completely in ...
2
votes
2answers
1k views

What's the POSIX way to read an exact number of bytes from a file?

Just hit this problem, and learned a lot from the chosen answer: Create random data with dd and get "partial read warning". Is the data after the warning now really random? Unfortunately ...
1
vote
1answer
260 views

How can I get my bash script to remove the first n and last n lines from a variable?

I'm making a script to preform "dig ns google.com" and cut off the all of the result except for the answers section. So far I have: #!/bin/bash echo -n "Please enter the domain: " read d echo "You ...
0
votes
4answers
6k views

How do I insert the first line of one file into the first line of another?

I've got a head command to get the first line of a file and a sed command to insert before the first line of a file. How do I combine these so I can insert the extracted first line of one file before ...
-1
votes
1answer
106 views

Searching for most recent file (Display Unique)

If i have a Folder structure with 10 Sub dirs Each sub folder has the possibility to hold a version of a file (Older or newer) "Poor folder structure." I was looking to use Find to locate all files ...
2
votes
0answers
717 views

how to copy a piece of a text file byte-by-byte to another text file? dd, head, or?

I need to grab the first lines of a long text file for some bugfixing on a smaller file (a Python script does not digest the large text file as intended). However, for the bugfixing to make any sense, ...
3
votes
3answers
468 views

does head input > output copy all invisible characters to the new file?

I need to grab the first lines of a long text file for some bugfixing on a smaller file (a Python script does not digest the large text file as intended). However, for the bugfixing to make any sense, ...
3
votes
1answer
92 views

Head Script output explanation

So this shell script: #!/bin/bash head >/dev/null; head; almost always gives the same output when called with sequential numbers (e.g. seq 10000 | ./sscript) OUPUT: //blank line 1861 1862 ...
10
votes
2answers
9k views

Command to remove the first N number of lines in input

Background I am running an SSH server and have this user that I want to delete. I cannot delete this user because he is currently running a few processes that I need to kill first. This is the ...
0
votes
1answer
178 views

Head and Tail of a directory, set by the user

I have this script that will show the user the head or tail of the files that are inside a set directory (currently that is set as '~/bin/new/*'). This script works but i would like to allow the user ...
3
votes
3answers
388 views

List the first 4 lines of all the files in a given directory

What command could I create that will list the first 4 lines of all the files in a given directory?
-7
votes
1answer
6k views

Head and tail command

How can i do this exercise in unix? Create the following command: lshead.bash – list the first few lines of every file in a directory specified by the argument. This command should also allow options ...
1
vote
1answer
383 views

tail: illegal option — -53\r error while using head and tail on same file in single script

I am using a=`head -1 Rt.in` to get first value from my file and I am saving it as a variable then I am using same variable to tail -$a to tail last lines from my input. But it is giving an error: ...
1
vote
2answers
5k views

How can I print the second to last line of many files into one file? [duplicate]

I have many CSV files in one directory which have various lengths. I'd like to put the second to last line of each file into one file. I tried something like tail -2 * | head -1 > file.txt, then ...
2
votes
2answers
97 views

why my first pipe operation didn't remove newline character?

My nextermCensus.lis contains newline character and I am only interested in the first line, I'd like to remove newline character. but it doesn't work. so I have to use second method to remove it. $(...