Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   Share you Bash Command Line Tips & Tricks (https://thetfp.com/tfp/tilted-technology/55282-share-you-bash-command-line-tips-tricks.html)

Marburg 05-11-2004 12:15 PM

Share you Bash Command Line Tips & Tricks
 
I've been a linux user for a fair amount of time now, but it's just now that I'm getting really used to the command line interface.
I've picked up a few cool tidbits here and there, and I'm sure a lot of you have too, so let's hear 'em.

-----

Here's a flexible sequntial download (think ezpics)
Code:

for NUM in `seq -f %02g 00 30`; do URL="http://www.coasar.com/newgalleries/pbc/ahv/03/images/fullimage/0$moo.jpg"; wget $NUM 2>/dev/null && echo Downloaded $URL || echo "    Failed $URL" ;  done
you can type that into console and away we go. (by the way, that link is NSFW)

-----

This one I picked up on the net somewhere.
I use firefox almost exclusively, so I set up aMSN to open up links with firefox. The major problem with that though is that if I'm already "surfin' the web" firefox will ask me to launch with a different profile. :(
Here's a fix that will make it so that if firefox is already open it'll open the link in a new tab. Open up your favorite text editor *cough*VI*cough* and paste this:
Code:

#!/bin/bash
/etc/firefox/firefox -remote "openURL($@, new-tab)" >/dev/null || exec /etc/firefox/firefox "$@" >/dev/null;

Make sure to save that file somewhere in your $PATH
Now you can do stuff like this (i called mine firefox_newtab)
Code:

firefox_newtab www.tfproject.org
I'm pretty sure that if you use mozilla you can just replace every instance of "firefox" with "mozilla".

-----

This is likely common knowledge but I'm going to write in anyway, just incase someone finds it usefull.

In my tips i used && (and) and || (or).

If two commands are seperated by && then the the second will only be executed if the first one is sucessfull. For example:
Code:

./configure && make
if configure fails (which it does so often) make won't even start.

If two commands are sperated by || (two pipes, shift + \ ) the second command will only execute if the first fails. E.g.:
Code:

wget http://www.moo.com/somefile.tar || echo Download Failed
If you follow a command with >/dev/null it won't print it's standard output to the screen. Compares these:
Code:

echo Beyond here lie dragons
echo Beyond here lie dragons >/dev/null

Anyway, I'm very much still new at this and I'm curious about something that other people have picked up over the years.

nash 05-11-2004 03:08 PM

Probably not what you're looking for, but, some jokers made a pizza-ordering command line utility. You sign up for an account on Domino's website and the utility does everything else! I've never tried it, but it looks like it works.

http://www.beigerecords.com/cory/pizza_party/

Holo 05-11-2004 06:22 PM

a lil simple bash script that will create an individual gzip tar of each sub-dir in the dir that it executed in. each tar has the name of the dir that it has compressed.



Code:

find . -type d -mindepth 1 -maxdepth 1 -exec tar zcf {}.tar.gz {} \;

Rawb 05-11-2004 09:45 PM

Search command history
 
Ctrl+R will search backwards through your previous commands, just press Ctrl+R, then start typing part of the command you want to recall (any substring, it doesn't have to be from the beginning).

Ctrl+A will go to the start of the line.

Ctrl+E will go to the end

Ctrl+U will nuke from the cursor to the start of the line

Ctrl+W will nuke the word to the left of the cursor

mgcloud 05-11-2004 10:29 PM

Here are some simple things that are really invaluable - I use them several times per day :)

'cd -' will go 'back' a directory (eg. say you were in /usr/src/linux, then went to ~/kernelconfigs ... you can quickly get back to /usr/src/linux by typing cd -)

when you're typing in a file name, you can enter the first few letters and then <TAB>. It'll either automatically be completed for you or it'll show you a list of potential matches.

and if you want to perform batch operations on files in a directory, you can try this:

Code:

for i in *; do operation_1_on $i; operation_2_on $i; done
EX:
Code:

for i in *; do mpg123 -w "${i/.mp3}.wav" "$i"; toolame -v 5 "${i/.mp3}.wav" "${i/.mp3}.mp2"; done
is something I just used to convert a few mp3s into mp2 format for easier editing. I know I can use something like transcode or ffmpeg, but they don't like to compile with GCC 3.4 yet =)

Latch 05-12-2004 01:58 AM

set permissions on files only:
Code:

find . -type f -exec chmod 0644 {} \;
set permissions on dirs only:
Code:

find . -type d -exec chmod 0755 {} \;

recursively set permissions on something depending on if it's a file or a directory (note: chmod -R 0755 would make EVERYTHING (files AND directories) 755.. I don't want that).

Of course that can be changed to chown or pretty much anything you want hehe..

I have some other scripts, but they're only relevant for work..

crony 05-18-2004 05:20 PM

One I use at work often is to kill multiple processes at once.

Code:

kill `ps -eaf | grep ^username | cut -c10-14`
Modify the ps command to list whatever needs to be killed.

yotta 05-19-2004 08:24 AM

Quote:

Originally posted by crony
One I use at work often is to kill multiple processes at once.

Code:

kill `ps -eaf | grep ^username | cut -c10-14`
Modify the ps command to list whatever needs to be killed.

Code:

pkill -u username
That's much cleaner :)

By default, pkill kills by process name.

ssh_agent 05-19-2004 02:05 PM

Type

! followed by the beginning of a command you've already used...and hit enter. it will auto exec that command in full...you ran last. obvoiusly be careful with somethings.

i also presume everyone knows ~userid prefers to userid's home directory.

also if on the command line and have vi editor mode activated ( bash -o vi ( works with ksh too ) ) then go for a ESC V and that will bring the command line straight into a full vi session

crony 05-19-2004 02:22 PM

Quote:

Originally posted by yotta
Code:

pkill -u username
That's much cleaner :)

By default, pkill kills by process name.

Yeah, the processes I need to kill vary. Just an example of how to get the pid's and pass it to kill. :)

sailor 05-19-2004 04:39 PM

Dont know... I just wrote a couple of shell scripts to backup stuff (one backs up MySQL, another backs up my website, another backs up stuff edited in the past day/week/month) and FTP them to backup. Kinda long to be posting on here though.

keyshawn 03-21-2005 05:19 PM

here's a few random ones i have:
Create your own short user name:

Code:

#!/usr/bin/env python
from random import choice

# Vowels
vowels = ['a','e','i','o','u']
# Beginning hard consonants
startcons =  ['b','c','d','g','j','k','m','n','p','q','t','v','x','z']
# Ending hard consonoants
endcons = ['b','c','d','g','k','m','n','p','t','x','z']
# Soft Consonants -- note v and z are listed twice
softcons = ['f','h','l','r','s','v','w','z']

print choice(startcons) + choice(vowels) + choice(softcons) + choice(endcons)

merge your multiple firefox bookmarks [in html] to one central big one:
Code:

#!/bin/bash

# by Tom Law, September 2003
# "ub" = "Unique Bookmarks"

# Compare two bookmark files, and create for each one a new html file
# that contains the bookmarks unique to the other one.

        # You'll need to edit the following two lines to show where
        # the two existing bookmark files (that you want to compare) are.

BA="/home/keyshawn2/august 18 2004 bookmarks -win.html"
BB="/home/keyshawn2/august 18 2004 bookmarks-linux.html"

#################################

        # sort each bookmark file, while removing extra spaces,
        # and adding a browser identifier (I put the identifier
        # at the end of the title to help with debugging.)
cd /tmp
sort -b $BA | sed -e 's/<\/A>/---A<\/A>/g' | sed -e 's/  */ /g' > utmpA
sort -b $BB | sed -e 's/<\/A>/---B<\/A>/g' | sed -e 's/  */ /g' > utmpB

        # merge the two files, while removing duplicate lines,
        # as well as unwanted lines and characters

sort -bd utmpA utmpB | uniq -uW 2 | grep HREF | grep -vE "(about:config|>---|N---A|N---B|uniqA|uniqB)" | sed s/\<DT\>//g | sed s/$/\<br\>/g > utmpC

        # split the merged file back into two, while removing the identifiers

cat utmpC | grep -e ---A | sed -e 's/---A//g'> utmpA
cat utmpC | grep -e ---B | sed -e 's/---B//g'> utmpB

        # convert them into html files

echo "<html><body>"  > uhead
echo "</body></html>" > utail

cat uhead utmpA utail > uniqA.html
cat uhead utmpB utail > uniqB.html

rm utmpA utmpB utmpC uhead utail

        # After running this command, load the "/tmp/uniq?.html" files
        # into their respective browsers (file A into browser B,
        # and file B into browser A), and drag each link to somewhere
        # in the bookmark file. It doesn't matter if you put them
        # in different folders than you did in the other bookmark file.
       
        # Run ub at bootup or by cron, and then every day or so look at
        # the "uniq" file (which you should bookmark) in each browser, and
        # update the browser's bookmark file.

detect how much space is your drive taking up and where:
Code:

for dirs in `ls /`
do
if [ -d /$dirs ]
then
echo "Listing of /$dirs" >> /tmp/disk_usage.txt
echo "################################" >> /tmp/disk_usage.txt
du -h /$dirs >> /tmp/disk_usage.txt
echo "" >> /tmp/disk_usage.txt
fi
done


Fallon 03-21-2005 08:02 PM

You can use PHP on the CLI which I love. I use that now as a way to list usage on my game hosting server so I know where I am on my usage. I've also set it up so that it'll kill certain processes or start them up.
Those are for linux
I've made scripting tips in Windows(I know, not bash, but still useful batch files that I wanna brag about) that'll list all installed applications on a computer and another one that can set the ip address.
If anyone would like any of them, let me know and I'll post them.

skaven 03-21-2005 11:56 PM

I have written some serious one-liner doozies in the past...usually when trying to do something somewhat complicated with a bunch of files. For example, one time I did a one-liner that recursively went through a directory tree and renamed files with certain names to match the directory name, or something like that.

Other one-liners I've used for doing recursive media conversion (WMA->MP3 for example), and of course for downloading Pr0n from various places with nice sequential filenames.

Probably my most frequently used BASH construct is the "while read" one:

Code:

cat /etc/passwd | awk -F: '{ print $1 }' | while read line; do echo $line; done;
And yes, I know that example does absolutely nothing useful, but it's an example.

ßeastie 03-22-2005 03:49 AM

Kind of like the cd - trick above, but pushd and popd are invaluable. You can create a whole stack of directories to switch from. The command dirs will give you a list of whats on the current stack. Very useful if you keep going back and forth, just pushd dir then popd and then to keep swapping !push and !pop to get back.

Bash is great.


All times are GMT -8. The time now is 02:49 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360