240 votes

Sort and count number of occurrence of lines

[your command] | sort | uniq -c | sort -nr The accepted answer is almost complete you might want to add an extra sort -nr at the end to sort the results with the lines that occur most often first ...
Eduard Florinescu's user avatar
163 votes

Command line: How do you rotate a PDF file 90 degrees?

Use PDFtk. For rotating clockwise: pdftk input.pdf cat 1-endeast output output.pdf For rotating anti-clockwise: pdftk input.pdf cat 1-endwest output output.pdf Regarding the installation of PDFtk ...
user163859's user avatar
  • 1,747
157 votes

How can I remove the BOM from a UTF-8 file?

If you're not sure if the file contains a UTF-8 BOM, then this (assuming the GNU implementation of sed) will remove the BOM if it exists, or make no changes if it doesn't. sed '1s/^\xEF\xBB\xBF//' &...
CSM's user avatar
  • 2,180
147 votes

Merge jq output into a comma separated string

Do it in jq, but see @Kusalananda's answer first jq -r '.host_components[].HostRoles.host_name | join(",")' No, that's wrong. This is what you need: jq -r '.host_components | map(....
glenn jackman's user avatar
146 votes

How can I pass a command line argument into a shell script?

On a bash script, I personally like to use the following script to set parameters: #!/bin/bash helpFunction() { echo "" echo "Usage: $0 -a parameterA -b parameterB -c parameterC" echo -e "\...
Rafael Muynarsk's user avatar
139 votes

What is the fastest way to view images from the terminal?

I have created a tool that uses various unicode block graphics characters to improve the resolution (compared to the canonical 2 pixel per characterapproach taken by other solutions posted here). It ...
Stefan Haustein's user avatar
137 votes

How do I get the size of a directory on the command line?

du can be complicated to use since you have to seemingly pass 100 arguments to get decent output. And figuring out the size of hidden folders is even tougher. Make your life easy and use ncdu. You ...
Teque5's user avatar
  • 1,488
133 votes

How do I create a blank PDF from the command line?

convert, the ImageMagick utility used in Ketan's answer, also allows you to write something like convert xc:none -page Letter a.pdf or convert xc:none -page A4 a.pdf or (for horizontal A4 paper) ...
BartekChom's user avatar
  • 1,338
133 votes

Why does argv include the program name?

To begin with, note that argv[0] is not necessarily the program name. It is what the caller puts into argv[0] of the execve system call (e.g. see this question on Stack Overflow). (All other variants ...
countermode's user avatar
  • 7,553
129 votes

How can I remove the BOM from a UTF-8 file?

A BOM doesn't make sense in UTF-8. Those are generally added by mistake by bogus software on Microsoft OSes. dos2unix will remove it and also take care of other idiosyncrasies of Windows text files. ...
Stéphane Chazelas's user avatar
126 votes

Is there a standard dummy executable file that does nothing in Linux?

There's the standard utilities true and false. The first does nothing but return an exit status of 0 for successful execution, the second does nothing but return a non-zero value indicating a non-...
ilkkachu's user avatar
  • 139k
116 votes

How to OCR a PDF file and get the text stored within the PDF?

ocrmypdf does a good job and can be used like this: ocrmypdf in.pdf out.pdf To install: pip install ocrmypdf or sudo apt install ocrmypdf # ubuntu sudo dnf -y install ocrmypdf # fedora
Eduard Florinescu's user avatar
109 votes

How to output only file names (with spaces) in ls -Al?

I wonder why no one mentioned this simple command: ls -a | sort
Ben's user avatar
  • 1,208
107 votes

How can I remove duplicates in my .bash_history, preserving order?

So I was looking for the same exact thing after being annoyed by duplicates, and found that if I edit my ~/.bash_profile or my ~/.bashrc with: export HISTCONTROL=ignoreboth:erasedups It does almost ...
sprite's user avatar
  • 1,191
106 votes

Pass the output of previous command to next as an argument

I tend to use this: command1 | xargs -I{} command2 {} Pass output of command1 through xargs using substitution (the braces) to command2. If command1 is find be sure to use -print0 and add -0 to xargs ...
Michael Anderson's user avatar
102 votes

Command line: How do you rotate a PDF file 90 degrees?

I just stumbled upon this thread and saw that there is no good solution mentioned yet. I found that (at least on Debian and Ubuntu) pdfjam comes with the following commands: pdf90 input.pdf pdf180 ...
derschueddi's user avatar
  • 1,161
100 votes

Zip the contents of a folder without including the folder itself

Use -j; for example: zip -r -j zipped_dir.zip folder/* It won’t zip 'root_folder'.
A.B.'s user avatar
  • 1,081
97 votes

How can I remove the BOM from a UTF-8 file?

Using VIM Open file in VIM: vi text.xml Remove BOM encoding: :set nobomb Save the file and quit: :x For a non-interactive solution, try the following command line: vi -c ":set nobomb" ...
Joshua Pinter's user avatar
89 votes

Why is the password I entered not visible?

What's the simplest way of hiding user input? Not displaying it! Hiding passwords when they're being typed is an old tradition. In makes sense from a security perspective in most contexts: if ...
Gilles 'SO- stop being evil''s user avatar
89 votes

Command line: How do you rotate a PDF file 90 degrees?

In Linux Mint 18.3 (I believe in other Debian derived distributions as well) you have a simple command line tool named qpdf. You can use: qpdf in.pdf out.pdf --rotate=[+|-]angle[:page-range]. From the ...
Royi's user avatar
  • 1,033
83 votes

How to rotate a set of pictures from the command line?

You can use the convert command: convert input.jpg -rotate <angle in degrees> out.jpg To rotate 90 degrees clockwise: convert input.jpg -rotate 90 out.jpg To save the file with the same name: ...
GAD3R's user avatar
  • 66.8k
81 votes

Is there a problem with grep command? I am getting characters that don't match my regular expression

In your code o* means "zero or more occurrences of o". It seems you confused regular expressions with glob syntax (where o* means "one o and zero or more whatever characters"). In ...
Kamil Maciorowski's user avatar
78 votes

How to find a file from any directory

First, an argument to -iname is a shell pattern. You can read more about patterns in Bash manual. The gist is that in order for find to actually find a file the filename must match the specified ...
Arkadiusz Drabczyk's user avatar
72 votes

How do I set time and date from the Internet?

A small command I found to update your time in case you don't want to install anything just to update the date. :) sudo date -s "$(wget --method=HEAD -qSO- --max-redirect=0 google.com 2>&1 ...
Prashant Kabade's user avatar
71 votes

diff within a line

Just another method using git-diff: git diff -U0 --word-diff --no-index -- foo bar | grep -v ^@@ grep -v if not interested in positions of the diffs.
Deepak's user avatar
  • 811
68 votes

Inconsistent color output from `ls` command

It looks as if your prompt-string ($PS1) is setting the bold attribute on characters to make the colors nicer, and not unsetting it. The output from ls doesn't know about this, and does unset bold. ...
Thomas Dickey's user avatar
67 votes

Why does argv include the program name?

Plenty: Bash runs in POSIX mode when argv[0] is sh. It runs as a login shell when argv[0] begins with -. Vim behaves differently when run as vi, view, evim, eview, ex, vimdiff, etc. Busybox, as ...
muru's user avatar
  • 73k
63 votes

How do I check whether my shell is running in a terminal?

isatty is a function for checking this, and the -t flag of the test command makes that accessible from a shell script: -t file_descriptor True if file descriptor number file_descriptor is ...
Michael Homer's user avatar
62 votes

What are the standard error codes in Linux?

The errno command can do this. From man errno: DESCRIPTION errno looks up errno macro names, errno codes, and the corresponding descriptions. For example, if given ENOENT on a Linux system, it ...
terdon's user avatar
  • 242k

Only top scored, non community-wiki answers of a minimum length are eligible