Useful Command Line Snippets

Sep 11, 2018 min read

Here are some useful command line snippets

Get the number of files in a directory, by using the ls command this will count the result.

ls -l | wc -l

List all files in a directory/ directories recursively

ls -LR

Find all files in a directory based on a pattern

ls | grep -P "^A.*[0-9]{2}$"

And remove all files that match that pattern

ls | grep -P "^A.*[0-9]{2}$" | xargs -d "\n" rm

Move multiple files to a directory

mv -t path/to/DESTINATION file1 file2

OR Using a wildcard(*) to match file names

mv *.jpg path/to/DESTINATION

Decode a Base64 string in the command line

echo < base64 string > | base64 --decode

Search your bash history

history

Search the last N number of results from you history

history 5

Grep your history

history | grep <thing to search for>
history | grep ssh

Execute command found in history

!<history id>
!5