Home » Linux » Bash tricks
        

Bash tricks

These are a collection of my Bash tricks, and snippets from my .bash_history and .bashrc. I hope they come in handy for you.

 

There is, of course, ctrl+r that allows you to do a reverse search through your bash history. Type to match, then hit enter to put it on the command line.

 

The history command allows you to modify your history, or just edit .bash_history.

 

To add a proxy to a CLI command:

http_proxy='http://192.168.0.1:80' wget http://ftp.somesite.net/pub/file.tar.gz

 

To find files of the form <filename>(1).flac, such as duplicate music files. (substitute .mp3 for .flac if you need)

find </in/this/path/> -regex .*([0-9]+)\.flac -exec echo {} +

 

!! reruns the last command issued, !<string> runs the last command that started with <string>, and pwd shows you where you are.

 

When you are affecting groups of files, you can use a regex, such as vi file[1234].txt to open file1.txt, file2,txt, etc. Of course, in vi, you need to use :n to move to the next file.

 

A command like rm file[1234].txt or rm file[1-4] will delete all file* that end in 1, 2 ,3 4. You can also do rm file[1-4]+ to catch file12 and file33. The “+” means match one or more “[1234]’s.”

This is similar to how the rename command works, though it uses perl regex instead.

 

Finally, one brain buster:

If I do more somefile and I intended to (or then need to) do vi somefile, in bash, you can do ^more^vi^ to make a substitution on the previous command only, to run vi somefile/

 

The command works in the syntax of ^find^replace^

 
Edited Apr 2nd. Thanks anna-.
Till next week,
-weetabix

Leave a Reply