Octal

Shell tricks

86 posts in this topic

here's my terminal doing some tricks B) lol

term.jpg.xs.jpg

0

Share this post


Link to post
Share on other sites

I don't think these really qualify as 'tricks' but

echo "/blah/whatever" | xargs ls -ln

cat /dev/null > somefile

cat /dev/ttyS0 (reading from a serial device)

0

Share this post


Link to post
Share on other sites

Whenever I get bored with the music selection I have I improvise:

dd if=/dev/urandom of=/dev/dsp

Ctrl + C to stop listening...

0

Share this post


Link to post
Share on other sites
Whenever I get bored with the music selection I have I improvise:

dd if=/dev/urandom of=/dev/dsp

Ctrl + C to stop listening...

Is it bad if that tells you kill specific people :blink:

my "trick"

echo `curl -s http://www.binrev.com/forums/ | tr "
" "\ " | tr "" "\ " | grep "users online"`

:lol:

0

Share this post


Link to post
Share on other sites

DONT LOOK AT THIS IF YOU THINK YOU MIGHT ACTUALLY TRY IT

as root....

alias selfdestruct='rm -rf * /'

... i typed this on purpose the other night before i reformatted, all my shell commands were deleted

-1

Share this post


Link to post
Share on other sites

or dd if=/dev/zero of=/dev/hda

for a lowlevel format / self-destruct basically; sure is a good way to securely delete your HD

hey you could even try dd if=/dev/urandom of=/dev/dsp same command using I/O redirection is cat /dev/urandom > /dev/dsp

0

Share this post


Link to post
Share on other sites

Sorry if this was mentioned before:

Anything within `` gets executed, e.g.:

$ HELLO="`echo Hello`"
$ echo "$HELLO"
Hello

0

Share this post


Link to post
Share on other sites

i use this script, below, to unpack archives. i made an alias for it, so i only have to type 'unpack whatever_archive'

this is the alias -

alias unpack='/home/iceni60/scripts/unpack2dir.sh'

#! /bin/sh
# #############################################################################

NAME_="unpack2dir"
HTML_="uncompress unpack script"
PURPOSE_="unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z to a dir of the same name as archive prefix"
SYNOPSIS_="$NAME_ [-vhlr] <file> [file...]"
REQUIRES_="standard GNU commands"
VERSION_="1.2"
DATE_="1999-09-20; last update: 2006-02-03"
AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
URL_="www.comp.eonworks.com"
CATEGORY_="compress"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

# HISTORY:
# 2006-02-03 v1.2 - added the -C flag to tar options. Otherwise tar would not
# extract to a dir with different name then the one found in the
# archive.

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-r, remove the compressed file after extraction
-v, verbose
-h, usage and options (help)
-l, see this script"
exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

case $options in
r) rmf=on;;
v) verbose=on;;
h) usage;;
l) more $0; exit 1;;
\?) echo invalid argument, type $NAME_ -h for help; exit 1;;
esac

done
shift $(( $OPTIND - 1 ))

mkdirf() {

# usage: fnc <file_prefix> <file>

[ -d $1 ] && { echo "${NAME_}: skipping ${2} - dir ${1} already exist"; continue; }
#echo $1
mkdir $1
# [[ $verbose ]] && echo "${NAME_}: unpacking "$2
}

file_getDirname() {

local _dir="${1%${1##*/}}"
[ "${_dir:=./}" != "/" ] && _dir="${_dir%?}"
echo "$_dir"

}

file_getBasename() {

local _name="${1##*/}"
echo "${_name%$2}"

}

clean() {

# usage <exit_status> <dir_to_rm>

[[ $1 != 0 ]] && rmdir $2 # remove empty dir if unpacking went wrong
[[ $1 == 0 && $verbose ]] && echo "${NAME_}: unpacking " ${dir}/${a}
[[ $rmf ]] && rm -f -- $a

}

start_dir=$(pwd)

for a in "$@"; do

cd $start_dir
fname=$(file_getBasename $a)
dir=$(file_getDirname $a)
cd $dir
a=$fname

case $a in


# zip
*.[zZ][iI][pP])
mkdirf ${a/.[zZ][iI][pP]/} $a
unzip -qq $a -d ${a/.[zZ][iI][pP]/}
clean $? ${a/.[zZ][iI][pP]/}
;;

# tar
*.[tT][aA][rR])
mkdirf ${a/.[tT][aA][rR]/} $a
tar -xf $a -C ${a/.[tT][aA][rR]/}/
clean $? ${a/.[tT][aA][rR]/}
;;

# tgz
*.[tT][gG][zZ])
mkdirf ${a/.[tT][gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][gG][zZ]/}
clean $? ${a/.[tT][gG][zZ]/}
;;

# tar.gz
*.[tT][aA][rR].[gG][zZ])
mkdirf ${a/.[tT][aA][rR].[gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][aA][rR].[gG][zZ]/}/
clean $? ${a/.[tT][aA][rR].[gG][zZ]/}
;;

# tar.bz2
*.[tT][aA][rR].[bB][zZ]2)
mkdirf ${a/.[tT][aA][rR].[bB][zZ]2/} $a
tar -xjf $a -C ${a/.[tT][aA][rR].[bB][zZ]2/}/
clean $? ${a/.[tT][aA][rR].[bB][zZ]2/}
;;

# tar.z
*.[tT][aA][rR].[zZ])
mkdirf ${a/.[tT][aA][rR].[zZ]/} $a
tar -xZf $a -C ${a/.[tT][aA][rR].[zZ]/}/
clean $? ${a/.[tT][aA][rR].[zZ]/}
;;


*) echo "${NAME_}: $a not a compressed file or lacks proper suffix";;

esac

done

0

Share this post


Link to post
Share on other sites

here are some more aliases i've got -

# screenshots
alias screenshot='import -window root ~/Desktop/`date +%Y%m%d%H%M`.png'

# System info
alias cpuu="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'"
alias memu='ps -e -o rss=,args= | sort -b -k1,1n | pr -TW$COLUMNS'
alias pg='ps aux | grep' #requires an argument

# interactive
alias cp='cp -vi'
alias mv='mv -vi'
alias rm='mv --target-directory=$HOME/.Trash/'

# network
alias net1='watch --interval=2 "sudo netstat -apn -l -A inet"'
alias net2='watch --interval=2 "sudo netstat -anp --inet --inet6"'
alias net3='sudo lsof -i'
alias ping='ping -c 10'
alias currports='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/currports/cports.exe'
alias winwhois='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/win32whois_0_9_13/win32whois.exe'
alias xnews='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/XNews/XNEWS.EXE'
alias whois='whois -H'

# chmod and permissions commands
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'
alias perm='stat --printf "%a %n \n "' # requires a file name e.g. perm file

# lynx web browser
alias bbc='lynx http://news.bbc.co.uk/text_only.stm'
alias google='lynx http://google.co.uk'

here are some functions

function	ff			   { find . -name $@ -print; }

function mfloppy { mount /dev/fd0 /mnt/floppy; }
function umfloppy { umount /mnt/floppy; }

function mdvd { mount -t iso9660 -o ro /dev/dvd /mnt/dvd; }
function umdvd { umount /mnt/dvd; }

function mcdrom { mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom; }
function umcdrom { umount /mnt/cdrom; }

function psa { ps aux $@; }
function psu { ps ux $@; }

# clock - A bash clock that can run in your terminal window.
clock ()
{
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done
}

netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'

# /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}

0

Share this post


Link to post
Share on other sites
DONT LOOK AT THIS IF YOU THINK YOU MIGHT ACTUALLY TRY IT

as root....

alias selfdestruct='rm -rf * /'

... i typed this on purpose the other night before i reformatted, all my shell commands were deleted

So when you make an alias the command gets executed?

Anyway, that doesn't delete all the files on the harddrive. It skips a few, because they are being used.

0

Share this post


Link to post
Share on other sites
Try echo -n >file instead. No newline.

touch file

>file

Edited by duper
0

Share this post


Link to post
Share on other sites

Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#

0

Share this post


Link to post
Share on other sites
Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#

cool... it works..

0

Share this post


Link to post
Share on other sites
Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#

If you want to keep track of multiple folders you've been through, try pushd / popd. You basically get a 'directory stack' to push and pop from at your heart's desire.

0

Share this post


Link to post
Share on other sites

Often forget to use sudo and then having to up-arrow and add it is a bitch. Instead do a

sudo !!

!! - denotes last command in history

echo $?

returns the exit code of the last command, which is often 0 for successful or 1 for failure. perhaps someone could elaborate.

cd ~

~ - denotes home directory. again not a groundbreaking trick just something beginners might find useful

Edited by .solo
0

Share this post


Link to post
Share on other sites

Here's a yum specific trick:

I typically work with Redhat-related (Redhat, Fedora, CentOS, etc) systems, and use yum as my desired mechanism for managing installation/dependencies of packages. I also do a lot of dev work in Perl, which means installing lots of modules from CPAN. If the distro provides one, the sanest way for me to install packages is usually through my distro's repositories. This isn't always the easiest to do, though, as packages are rarely named Some::Module in the repos, nor are they always called perl-Some-Module.

Take the following example (from Fedora 8) for a common module, LWP::Simple:

$ yum search LWP::Simple
No Matches found
$ yum search perl-LWP-Simple
No Matches found

It's not that Fedora 8 doesn't have it's own package for LWP::Simple, it's just that it's bundled with something else, and named in a way you wouldn't necessarily expect. If you instead run this command:

$ yum whatprovides 'perl(LWP::Simple)'
perl-libwww-perl.noarch : A Perl interface to the World-Wide Web

Voila! We now know that 'perl-libwww-perl' is actually the package responsible for the LWP::Simple module.

0

Share this post


Link to post
Share on other sites

Here's my "trick"

$? holds the return value for the last command. E.g.

$ curl urlthatdoesntexist.com
curl: (6) Couldn't resolve host 'urlthatdoesntexist.com'
$ echo $?
6

$ curl www.google.com > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6538 0 6538 0 0 18957 0 --:--:-- --:--:-- --:--:-- 55232
$ echo $?
0

EDIT: Oops, just realized this has already been covered, hopefully my elaboration will add something ;)

pushd stores the current direction and changes to a new one. popd returns to the last stored directory.

$ pwd
/home/baphomet
$ pushd /
/ ~
$ pwd
/
$ popd
~
$ pwd
/home/baphomet

Edited by Baphomet
0

Share this post


Link to post
Share on other sites

View most-used commands:

history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c | sort -n | tail | sort -nr

0

Share this post


Link to post
Share on other sites
View most-used commands:

history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c | sort -n | tail | sort -nr

I think you took a few extra steps in there. The second awk doesn't do anything, only the first word of the command will get through the first awk, and there aren't bound to be any pipe characters in that so the second awk is essentially a no-op. You also do an extra sort.

history | awk '{print $2}' | sort | uniq -c | sort -nr | head

0

Share this post


Link to post
Share on other sites

At work I had to delete email for about 50 users on a server running courier imap as the inbound email service. Somehow they had over 100GB of email, so I was told to delete it all, but avoid destroying the Maildir directories. It uses system commands, so it has the inability to crash unless there is a big problem with the OS, but runs slower than a script in perl/python/ruby.

		   localhost:~$ cat read.sh 
#!/bin/bash

workingdir='/tmp/test'
cd $workingdir

array=(`ls`)
len=${#array[*]}
i=0

while [ $i -lt $len ]; do
echo "${array[$i]}" >> $HOME/output.txt
let i++
done

cat $HOME/output.txt | while read line; do
ls $workingdir/"${line}"/cur/* >> $HOME/result.txt #change this to ls $workingdir/"${line}"/cur/* | xargs rm -rf
done

This will work for any scenario that you want to delete large quantities of files with a specific directory structure. I thought I would post this because it contains quite a few shell tricks.

You can play with this if you make a directory structure in /tmp like this:

localhost:~$ ls /tmp/test/*/*

/tmp/test/1/cur:

file

/tmp/test/2/cur:

file

/tmp/test/3/cur:

file

/tmp/test/4/cur:

file

0

Share this post


Link to post
Share on other sites

You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?

0

Share this post


Link to post
Share on other sites
You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?

Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.

0

Share this post


Link to post
Share on other sites
You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?

Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.

Well with the interesting parts gutted, this script does essentially nothing. I'm just wondering why you posted it.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now