Grep the number of occurences in a file, counting multiple hits per line
Grep prints one line per match. To return the number if matches, use the -c
switch:
grep -c "something" filename
However, if a word appears more than once in a line, it is only counted once.
To count every match, you can use
sed
Show archive.org snapshot
to force line breaks on multiple matches:
sed 's/something/something\n/g' filename | grep -c "something"
Related cards:
Linux: rename or change extension of multiple files
When you need to bulk rename files you can not call "mv *.foo *.bar
" to change the extension of all .foo
files to bar
(because bash resolves wildcards and replaces them with the list of matched files).
This works on linuxes who use the ...
Working on the Linux command line: Use the `tree` command instead of repeated `cd` and `ls`
The tree
command will show you the contents of a directory and all its sub directories as a tree:
>tree
.
├── a
│ ├── file_1.txt
│ └── file_2.txt
└── b
├── c
│ └── even_more.txt
└── more.txt
3 directories, 4 files
If...
Bash: How to generate a random number within given boundaries
$RANDOM
on bash returns a random integer between 0 and 32767.
echo $RANDOM
9816
echo $RANDOM
30922
If you want to limit that to a certain maximum, you can just compare against the modulus of that maximum + 1. \
For example, the fo...
Dragging a file into your terminal pastes the file path
When you drag a file from a Nautilus window into a terminal window, the file's path will be pasted into the terminal. This also works with multiple files.
Shell script to clean up a project directory
Call geordi clean
from a project root to remove unused and unnecessary files inside it.
This script is part of our geordi gem on github. In Geordi > 1.2 you can call geordi clean
.
How to send a test e-mail from shell
If you want to manually check if e-mail delivery works on a machine by sending an e-mail you can run the following:
mail -s Test someone@example.com < /dev/null
This will send an empty e-mail with "Test" as its subject to `someone@example.com...
Show the status of a running dd copy
When you do a bitwise copy using the dd
tool you will not see any output until it completes or an error occurs.
However, you can send a command signal to the process to have it show its progress so far.
From another terminal, simply call (be ro...
Find the newest file from shell
This can be helpful when you need the latest file inside a directory for processing in a shell script:
ls -1tr * | tail -1
Used switches
The -1
switch makes ls
return 1 file per line, -t
orders by modification time and...