r/bashtricks • u/QuantumCow • May 17 '11
Execute previous command as root
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 • u/QuantumCow • May 17 '11
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 • u/riddley • May 09 '11
$ type type
type is a shell builtinr/bashtricks • u/BooeySays • Apr 24 '20
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 • u/ychaouche • Jun 30 '13
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 • u/wishmegood • Dec 10 '19
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 • u/toddler-ai • Mar 02 '23
r/bashtricks • u/[deleted] • Jun 09 '22
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 • u/wishmegood • Dec 19 '19
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 • u/ttttttttttt302 • Dec 03 '19
r/bashtricks • u/feoh • May 08 '14
r/bashtricks • u/[deleted] • Jun 10 '22
Below two webpages that explain a given shell command. Usefull for beginners, lazy days or ..
r/bashtricks • u/lozanomatheus • Feb 12 '21
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 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 • u/lozanomatheus • Dec 30 '20
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 • u/UnicornMolestor • Nov 26 '19
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 • u/sempervent • Nov 25 '19
r/bashtricks • u/downdiagonal • Feb 13 '10
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 • u/[deleted] • Jun 09 '22
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 • u/lozanomatheus • Feb 17 '21
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 my Website: https://www.lozanomatheus.com/post/an-easy-way-to-switch-between-runtime-versions-nodejs-terraform-and-279