r/bashtricks • • May 17 '11

Execute previous command as root

14 Upvotes

If you type out a long command and forget to sudo it, it's a pain to type it out again. Instead:

sudo !!

r/bashtricks • • May 10 '11

List of all bash commands

Thumbnail
oreilly.com
12 Upvotes

r/bashtricks • • May 09 '11

Poorman's "Which"

12 Upvotes
$ type type
type is a shell builtin

r/bashtricks • • Apr 24 '20

Just sharing one of my MUST HAVE functions

9 Upvotes

Hey guys, just sharing a function that I MUST HAVE for all my terminals. It's a very simple one, but I can't live with out it.

function cd(){
    builtin cd $*
    \ls -af --color=always --group-directories-first
}

as you can see, it is a modified version of the "cd" command. All it does is list the current directory's contents every time you change into a new directory.

When I am navigating somewhere, I have a habit of always executing "ls" after I "cd" somewhere to see where I am, and what directory I should go to next... It won't matter if I already know the directory tree structure, I would always execute "ls" after "cd".

So.. I starting using this to break the habit.

Now I can't live with out it.. Just figure I share in case there is someone else out there who does the same thing.

Cheers!

**EDIT**

I just typed in a general "ls" command for line 3 of the function. You guys can of course set the function to execute the "ls" command with the options of your liking. But for me personally, I like to have the listings:

  • in color

  • list hidden directories

  • have the directories grouped all together and listed first before the other files

So yea.. edit to your liking..


r/bashtricks • • Jun 30 '13

A one-liner to change the extension of many files at once (eg. JPG -> jpeg)

8 Upvotes

I had to upload some pics to a website but the form processing logic was so miserably poor that it only accepted files with png, jpeg or gif extensions. Since my files were .JPG, it didn't know that they were jpegs, because (I guess) it would litterally look for the jpeg string in the filename.

So, to rename my files from .JPG to .jpeg, I had to use this bash trick called substring substitution.

for file in *; do mv $file ${file%.JPG}.jpeg; done;

Explanation :

${file} prints the filename ${file%something} scans the string contained in the $flle variable from the left until it finds "something", it then returns the rest of the string (again, it scans from left to right).

So for example :

file=abcdef.ghijkl

echo ${file%i} 

would print

abcdef.gh, without the i.

r/bashtricks • • Sep 21 '14

Bash Productivity Tips

Thumbnail lauris.github.io
7 Upvotes

r/bashtricks • • Dec 10 '19

Some easy way to generate password

6 Upvotes

Generating Password one way:

cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w 10 | head -n 1

Or password easy to remember, another way

(shuf  -n 2  /usr/share/dict/words | sed 's/./\u&/'  ; shuf -i 10-999 -n 1 ) | tr -d '\n'

Run it couple of times if you find password you are looking..

Output looks like $ (shuf  -n 2  /usr/share/dict/words | sed 's/./\u&/'  ; shuf -i 10-999 -n 1 ) | tr -d '\n'
BananalandPenelopa485

$ (shuf  -n 2  /usr/share/dict/words | sed 's/./\u&/'  ; shuf -i 10-999 -n 1 ) | tr -d '\n'
AmtracksLechery310

$  (shuf  -n 2  /usr/share/dict/words | sed 's/./\u&/'  ; shuf -i 10-999 -n 1 ) | tr -d '\n'
SansiSurfier546

$ cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w 10 | head -n 1
0aE7bDba64

$ cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w 10 | head -n 1
f455ffFf28

$ cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w 10 | head -n 1
d3B5C2aD0f


r/bashtricks • • Mar 02 '23

Download Toddle for FREE: Toddler is a bash script that extracts all the TODO comments across your codebase into a markdown file.

Thumbnail
github.com
5 Upvotes

r/bashtricks • • Jan 26 '23

stail.sh - Short Tail

Thumbnail self.bash
5 Upvotes

r/bashtricks • • Jun 09 '22

Invoke the editor to write or modify a command

5 Upvotes

Control-x Control-e (or: Control-x e) invokes the editor to edit the current content on the command line. In Bash typing fc does the same. After editing you return to the command line where you can press 'return' to execute the command. Usefull for writing of modifying long and tricky commands.

It uses the editor that is set with environment variable $EDITOR. On some sytems it doesn't work with an empty command line.


r/bashtricks • • Dec 19 '19

Grep only once on ps

5 Upvotes

When looking for a process but get two of them

$ ps -ef | grep firefox

user1 2330 1 1 Jan29 ? 00:27:27 /usr/lib/firefox/firefox ...

user1 15820 618 0 03:58 pts/1 00:00:00 grep --color=auto firefox

try

$ ps -ef | grep [f]irefox

user1 2330 1 1 Jan29 ? 00:27:27 /usr/lib/firefox/firefox ...

Now you don't get bothered by extra grep or don't need to do grep -v grep ..

The command grep [a-zA-Z0-9]irefox would even find all processes that start with exactly one letter or number and end in "irefox".


r/bashtricks • • Dec 03 '19

Hi guys and gals I've opened a twitter to share my bash tricks with you! I would appreciate a follow or even a like on this post😊 thanks! 🐧🐧🐧

Thumbnail
twitter.com
5 Upvotes

r/bashtricks • • Sep 05 '19

why the single quote act differently ?

5 Upvotes

r/bashtricks • • May 08 '14

Some of this is review but there are a LOT of tips in here I wasn't aware of before and I've been slinging bash for 20 years.

Thumbnail
robertmuth.blogspot.com
5 Upvotes

r/bashtricks • • Feb 11 '14

BASH data analysis

Thumbnail
blog.xebia.com
5 Upvotes

r/bashtricks • • Jun 30 '13

Handy Bash reference cards

Thumbnail tldp.org
5 Upvotes

r/bashtricks • • Jun 10 '22

Explain a shell command

4 Upvotes

Below two webpages that explain a given shell command. Usefull for beginners, lazy days or ..

https://www.mankier.com/explain

https://explainshell.com/


r/bashtricks • • Feb 12 '21

[Blog] Bash variables — Things that you probably don’t know about it

4 Upvotes

Hey,

Two days ago I wrote this blog exploring the scope of the Bash variables and the types. I'm explaining their differences, how and when to use each one of them. Feel free to give any feedback/suggestion/* :)

See on Medium: https://medium.com/unboxing-the-cloud/bash-variables-things-that-you-probably-dont-know-about-it-8a5470887331?sk=26693ca772a0c54c99d3712303560ed4

See on my Website: https://www.lozanomatheus.com/post/bash-variables-things-that-you-probably-don-t-know-about-it

Sorry, I'm pretty new on Reddit, I accidentally deleted the original post...


r/bashtricks • • Dec 30 '20

Some ticks/tips for Bash script - HTTP request with a Bash native function && Get IP Address without ip/ifconfig

4 Upvotes

Hello there,

A few weeks ago I started my blog which I'll focus on Tips and Tricks. I wrote only two posts until now, soon I'll bring more. The idea is to bring some stuff that I use/know on a daily basis and it may be useful for others too. I'm trying to keep as simple as possible with some practical examples (+ external links that could explain in-deep, in case someone wants to know more :) ).

Any feedback is more than welcome :)

How to send a TCP request with Bash native feature (e.g.: you can do test an HTTP endpoint without installing cURL / Wget / etc)
https://medium.com/bash-tips-and-tricks/part01-tcp-udp-request-with-a-native-bash-feature-and-without-curl-wget-9dcef59c30aa

How to get the Linux IP address without ip/ifconfig
https://medium.com/bash-tips-and-tricks/getting-the-linux-ip-address-without-any-package-ifconfig-ip-address-etc-7b1363077964


r/bashtricks • • Nov 26 '19

Bash script to identify new info on a webpage and download it?

4 Upvotes

My son loves a few different youtube channels and i want to build a library of his favorite content. I have a bash script that will download the video with curl then use it ffmpeg to save it in a watchable format.. what I'm trying to do exactly is give it a set of links to the main channel page for a fre different channels and have it go through that list once a day and download any new content if any if available. Anyone know if thats possible? Would i be better off using python or something?


r/bashtricks • • Nov 25 '19

A nifty alias to quickly create a bash script

Thumbnail
gist.github.com
4 Upvotes

r/bashtricks • • Aug 16 '10

Common Bash Pitfalls

Thumbnail mywiki.wooledge.org
4 Upvotes

r/bashtricks • • Feb 13 '10

Get the weather forecast for the next 24 to 48 for your location.

4 Upvotes
weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}

Note that "\ "'} in the above should be "\n"'}. Reddit is not displaying it correctly. See here if you don't understand.

This shell function grabs the weather forecast for the next 24 to 48 hours from weatherunderground.com. Replace <YOURZIPORLOCATION> with your zip code or your "city, state" or "city, country", then calling the function without any arguments returns the weather for that location. Calling the function with a zip code or place name as an argument returns the weather for that location instead of your default.

Requires: perl, curl


r/bashtricks • • Jun 09 '22

Edit a file when viewing it with a pager

3 Upvotes

Edit the file that you are viewing with a pager.

Pressing v in the pager less and more opens the current file with $EDITOR, while most uses the e and E keys. After closing the editor, it shows to the pager again, with the modified text.


r/bashtricks • • Feb 17 '21

[Blog] A CLI tool that can easily manage more than 281 runtimes, making it easy to keep things updated and/or switching between versions. And it’s open-source :)

3 Upvotes

I just wrote this post. I'm showing an OSS tool that can easily keep things updated on *NIX/Windows and/or easily switch between versions. It's very useful on Bash/Zsh/Fish scripts that you need to switch between versions.

Feedbacks/suggestions/* are always welcome :)

See on Medium: https://medium.com/bash-tips-and-tricks/an-easy-way-to-switch-between-runtime-versions-nodejs-terraform-and-279-53fdfbcb4049?sk=43548418bf394d80e277a39a76070843

See on my Website: https://www.lozanomatheus.com/post/an-easy-way-to-switch-between-runtime-versions-nodejs-terraform-and-279