Table of Contents

Linux Tips, Frequently Used Commands

More on regular expressions and tools.

Permissions and groups

Set all permissions for group to read write and sticky:

chmod g+rws . -R

Set group recursively without touching user:

chown :groupname . -R

Find in files

Find text in files (source)

find -name '*.txt' | xargs grep 'txtpattern'

Finding a word in XML files and returning only a certain attribute with only the text value.

find | xargs grep "stringval" | cut -d '"' -f 2,9 --output-delimiter='    ' | cut -d '<' -f 1 | cut -d '>' -f 1,2 --output-delimiter=''

Press <ctrl>+v, <tab> to write a real tab into the konsole.

SVN on steroids

After manually deleting a file from an svn working copy, the file remains marked as missing. Manually svn rm-ing the file is one option, but automatically is also possible. The following command perform the preparations for a simple (but stupid) backup. Retrieve the list from svn status and perform svn rm on all missing files or perform svn add for all new files. Put it in a batch file and $1 may be used as location, $2 as optional parameter.

svn st --ignore-externals $1 $2 | grep ! | cut -f 2 -d '!' | xargs -t -I{} svn rm {}
svn st --ignore-externals $1 $2 | grep ? | cut -f 2 -d '?' | xargs -t -I{} svn add {}

Are you wgetting this?

Use wget to download a directory from ftp site without creating host directories etc. Cutdirs is number of dirs in <pathtofolder>.

wget -rc -nH --timestamping --server-response  --cut-dirs=1 ftp://<user>:<passwrd>@<host>:<port>/<pathtofolder>/<folder>

Use wget to recursively download a directory from http site.

wget -k -i http://server.com/path
find -name "index.html" | xargs -i{} wget -c -F -x -nH -i {}

or

wget -m http://server.com/path

This also creates directory server.com and subsequent directories.

I'm touched

The touch command can also set the time to a certain value or copy time from a reference file.

touch -t "200806021936.05" example.txt

Finding files modified in period

Create a batch file (for example /usr/bin/findinperiod), inspired by jim mcnamara:

tmpfrom=/tmp/findperiodfrom
tmpto=/tmp/findperiodto
touch -t $2 $tmpfrom
touch -t $3 $tmpto
find $1 -newer $tmpfrom ! -newer $tmpto
rm $tmpfrom
rm $tmpto

Use like this, sort using pipe to the sort command:

findinperiod . 200803220000 200803232359 | sort

All to lowercase (SVN)

Rename svn files to lowercase:

find -name "*.*" | sed 's/\(.*\)/\1\n\L\1/' | xargs -L 2 svn mv 

Unrar and remove archives

This will find all rar files, extract them and remove all rar and r01, r02 etc. files.

find -name '*.rar' | sed 's/\(.*\)\/\(.*\)\(..\)/unrar e \1\/\2\3 \1\/ \&\& rm \1\/\2??/' | xargs -I{} -i bash -c '{}'

This involves some risks, so be careful! To test what will happen:

find -name '*.rar' | sed 's/\(.*\)\/\(.*\)\(..\)/unrar e \1\/\2\3 \1\/ \&\& rm \1\/\2??/' | xargs -I{} -i bash -c 'echo "{}"'

List unversioned with filesize

svn st --ignore-externals | grep ? | cut -d '?' -f 2 | xargs -I{} du -sh {}

Show filesize of new SVN files

svn st --ignore-externals | grep ? | cut -c 8- | xargs -I{} du -sh {} 

Pipe replace/escape spaces

Escape spaces (' ' ⇒ '\ ')

 | sed 's, ,\\&,g'

Remove file extension from string

 | sed 's/\(.*\).ps/\1/'

Find .nfo files containing 'trance'

find -name '*.nfo' | xargs -I{} -i bash -c 'grep trance "{}" && echo "{}"'

Bash autocomplete from history

In /etc/inputrc, make sure it contains:

...
# mappings for "page up" and "page down" to step to the beginning/end
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history

# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
...

reference