Useful Command Line Snippets

09-11-2018

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.

1
ls -l | wc -l

List all files in a directory/ directories recursively

1
ls -LR

Find all files in a directory based on a pattern

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

And remove all files that match that pattern

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

Move multiple files to a directory

1
mv -t path/to/DESTINATION file1 file2

OR Using a wildcard(*) to match file names

1
mv *.jpg path/to/DESTINATION

Decode a Base64 string in the command line

1
echo < base64 string > | base64 --decode

Search your bash history

1
history

Search the last N number of results from you history

1
history 5

Grep your history

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

Execute command found in history

1
2
!<history id>
!5