Beruflich Dokumente
Kultur Dokumente
Add the following alias and function to your profile to be able to copy and paste
files at the command line:
ccopy(){ cp $1 /tmp/ccopy.$1; }
alias cpaste="ls /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./
%"
blackbird:~/tst2 tks1$ ls
1.txt 2.txt
function mtube {
video_id=$(curl -s $1 | sed -n "/watch_fullscreen/s;.*\(video_id.\
+\)&title.*;\1;p");
mplayer -fs $(echo "http://youtube.com/get_video.php?$video_id");
}
mtube http://www.youtube.com/watch?v=evQ0QNi2CzA
To create a set of backed up files with the current date added at the end of the
file name try the following:
cp 1.txt 1.txt.20082703
cp 2.txt 2.txt.20082703
cp 3.txt 3.txt.20082703
The following alias will print the directory structure from the current directory
in tree format.
Selective content replace on files. For example to replace '<?' with '<?php' in all
PHP files:
find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;
Add the following sed commands to cal to get a calendar with the current date
marked:
cal | sed "s/^/ /;s/$/ /;s/ $(date +%e) / $(date +%e | sed 's/./#/g') /"
sed '/ *#/d; /^ *$/d' file
The following command creates in the /usr/project directory, a copy of the current
working directory structure:
And want to change 'hello world' to 'hello shell-fu' we need to replace across
lines. This can be done with the following command:
:~$ cat foo | sed '/hello$/N;s/hello\nworld/hello\nshell-fu/'
This is a sample hello
shell-fu file.
Here sed first looks for lines which end with 'hello' then reads the next line,
finally replacing 'hello\nworld' with 'hello\nshell-fu'.
This also has a lot of other uses, for example converting double line spaced files
to single:
cat doublespace | sed '/^$/N;s/\n$//g'
This will print the contents of the file from the line that matches /start/ until
the line that matches /end/
Number each line of a file
Tags: sed
Save this to del.icio.usDigg this!Reddit this!Tweet this!
PATHS="/export/home /home"
AWK=/usr/bin/awk
DU="/usr/bin/du -ks"
GREP=/usr/bin/grep
DF="/usr/bin/df -k"
TR=/usr/bin/tr
SED=/usr/bin/sed
CAT=/usr/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mailx
mailto="all@company.com"
do
fi
done
Get examples of ways a command can be used direct from shell-fu by adding the
following alias:
function examples { lynx -width=$COLUMNS -nonumbers -dump "http://www.shell-
fu.org/lister.php?tag=$1" | \
sed -n '/^[a-zA-Z]/,$p' | egrep -v '^http|^javas|View Comm|HIDE|] \+|to Share|^
+\*|^ +[HV][a-z]* l|^ .*efu.*ep.*!$' | \
sed -e '/^ *__*/N;s/\n$//g' | less -r; }
This pulls out the tips tagged by the given command. (Make sure you tag any tips
you submit!)
Use the following command to give a history listing without the numbers for easier
copy and pasting:
mkdir -p tmp/a/b/c
Use && to run a second command if and only if a first command succeeds:
ps | grep 'ss[h]'
The regular expression 'ss[h]' matches the literal string 'ssh' when it appears in
the process list, but does not accidentally match the string 'ss[h]' when it
appears in the process list as 'grep ss[h]'.
It can be useful to not only know what has gone to stdout and stderr but also where
they occurred with respect to each other.
Allow stderr to go to err.txt, stdout to out.txt and both to mix.txt
((./program 2>&1 1>&3 | tee ~/err.txt) 3>&1 1>&2 | tee ~/out.txt) > ~/mix.txt 2>&1
Running a second command with the same arguments as the previous command, use '!*'
to repeat all arguments or '!:2' to use the second argument. '!$' uses the final
argument.
$ cd /home/user/foo
$ mkdir !*
mkdir /home/user/foo
Use awk to change the file extension for a group of files. For example to change
all .htm files to .php:
This can be tested first by leaving the '| sh' off to give a list of the commands
that will be executed.
Will show you a diff of files in the root of dir_1 and dir_2
For example, let's say you have a "bsmith" account on a host called "apple". You
want to copy those files into your "bobsmith" account on a host called "pear".
You'd log into your "bobsmith@pear" account and type the following:
This technique is useful when you have insufficient disk space on the source
machine to make an intermediate tarball.
tail +2 file
swap files
Tags: bash
Save this to del.icio.usDigg this!Reddit this!Tweet this!
# Swap 2 files/dirs that are in the same dir
# Usage: sw f1 f2
function sw {
f1=$1
f2=$2
if [ "x$f2" = "x" ]; then
echo "Usage: sw file1 file2"
echo " swap name of 2 files"
else
d1=$(dirname $f1)
d2=$(dirname $f2)
if [ "$d1" != "." -o "$d2" != "." ]; then
echo "sw: Can swap only files in current directory"
else
if [ -e "$f1" -a -e "$f2" ]; then
mv $f1 .sw.$f1
mv $f2 $f1
mv .sw.$f1 $f2
else
echo "sw: '$f1' and '$f2' must exist"
fi
fi
fi
}
Find and Grep
Tags: bash grep find
Save this to del.icio.usDigg this!Reddit this!Tweet this!
Find all files with given name (you can use Bash expansion if you'd like), and Grep
for a phrase:
find . -name -exec grep "phrase" {} \;
Or, use Grep options to print the filename and line number for each match:
find . -name -exec grep -Hn "phrase" {} \;
The string `{}` is replaced by the current filename being processed everywhere it
occurs in the arguments to the command. See the `find` man page for more
information.
In this example 'last-archive.tgz' is the last tarball for the given project.
-newer finds files newer that than last-archive.tgz, this way you can tar up only
the changed files.
By executing "sleep 60" remotely, the tunnel stays alive for at least 60 seconds,
and assuming your application has connected by then, the tunnel will continue to
stay alive until the application disconnects.
The options given above are perfect for executing this command from a script; it is
quiet (-q) and goes to background after prompting for a password (-f). This
particular example forwards the VNC protocol so that when your VNC client connects
to localhost, it connects securely to remotehost.com over the tunnel.
ssh proxy forwarding
Tags: bash alias
Save this to del.icio.usDigg this!Reddit this!Tweet this!
ssh -l <login> -L <port>:<destination:port> <proxymachine> <local addy>
example
ssh -l foo -L 5000:192.168.5.2:443 192.168.1.1 https://localhost:5000/
du -h
To calculate the current directory size you are in (-s stand for summary)
du -sh
To show all the 1 level sub directories size (which you are not interested at sub
sub directories.)
du -sh *
du -sh /home/*
The '.bak' will create copies of your original files with the .bak extension added
incase of mistakes. Be careful of running this twice though as the backups will get
overwritten.
find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}'
| sort | uniq -c | sort -nr
(You may want to add this as an alias rather than type it in each time!)
Using comm
Tags: diff perl comm
Save this to del.icio.usDigg this!Reddit this!Tweet this!
You can use diff to see the differences between two files, but it can be useful to
see what is the same and more clearly how they differ. This is where comm comes in
useful.
comm tells you what information is common to two lists and what information appears
uniquely in one or the other.
The first column shows lines only in the first file, the second column lines from
the second file and the third column lines from both.
$ ls
1.txt 2.txt 3.txt 4.txt
$ rm `ls | grep -v 4\.txt`
$ ls
4.txt
Example usage:
$ sd mydir
...
$ cd mydir
This method works for csh but not bash. For bash try the following function in your
profile:
sd(){ export $1=$PWD; }
Then:
$ sd abc
...
$ cd $abc
For example:
$ IFS=: ; for D in $PATH; do for F in $D/*text*; do [ -x $F ] && echo $F; done;
done
/usr/bin/gettext
/usr/bin/glib-gettextize
/usr/bin/gnome-text-editor
/usr/bin/xgettext
$ tail -f stuff | awk ' /now/ { system("mail -s \"Now Occured\" mail@foo.com <
msg") }'
Open port
Tags: No tags
Save this to del.icio.usDigg this!Reddit this!Tweet this!
iptables -A INPUT -p tcp -i eth0 --dport X -j ACCEPT
Make a backup of existing files, afterwards copy new files from somedir:
1. Go to proddir
ls /update-200805/ |xargs -n1 -I xxx cp xxx xxx.`date +%Y%m%d` ; cp /update-
200805/* .
alias lsusers='getent passwd | tr ":" " " | awk "\$3 >= $(grep UID_MIN
/etc/login.defs | cut -d " " -f 2) { print \$1 }" | sort'
#!/bin/bash
cr='*'
if [ -z $3 ]; then cr=$3; fi