Questions tagged [head]

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

Filter by
Sorted by
Tagged with
4
votes
6answers
235 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
3answers
72 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
1answer
31 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
1answer
72 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
1answer
52 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
2answers
412 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
2answers
90 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
2answers
68 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
3answers
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. ...
3
votes
2answers
130 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
1answer
54 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 ...
1
vote
1answer
1k 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
2answers
48 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
4answers
581 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 ...
0
votes
1answer
36 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
1answer
48 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
42 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
150 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
92 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
3
votes
1answer
4k 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
105 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
2k 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
1answer
130 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
197 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
91 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
503 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
1k 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
513 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
28 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
3k 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 ...
4
votes
1answer
7k 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
179 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 ...
5
votes
1answer
5k 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
114 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
1k 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. Basically I have belly = tail -n +"$HEAD" "$1" | head -n $((TAIL-HEAD+1)) ...
2
votes
3answers
438 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
396 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
75 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
1k views

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

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/local/tmp/csce215/...
1
vote
1answer
59 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
3k 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
1k 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
367 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 ...
3
votes
1answer
159 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
889 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
685 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
134 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 ...