Archive for the ‘UNIX Shell’ Category

Transferring Files with rsync and ssh from Server to Server

Monday, February 8th, 2010

This rsync Command is really helpful for transferring data to another server.
I need this very often.
rsync -vrlptgoD -e ssh ./yourfile.tar.gz root@XXX.XX.47.73:/root

If you need to change the ssh Port, you can write it as a string:
rsync -vrlptgoD -e 'ssh -p1356' ./yourfile.tar.gz root@XXX.XX.47.73:/root

change owner of files or directories globally by searching for a group

Friday, January 22nd, 2010

Sometimes, thankfully not very often, I need to change the group or the user of many directories or files in a global way. This could happen owing to unfortunate circumstances, e.g when you switch a Serversystem to another and something went wrong or something happens you did not plan before.

With this command I find all directories inside of /var/www with the group-id 501.
then it will execute the command chgrp for changing the group-id to 33:

find /var/www -gid 501 -type d -exec chgrp 33 ‘{}’ ‘;
(You can also write ‘…chgrp www-data’)

I have to say, this command had saved my life, for more than one time.

Removing folders content except specific files or subfolders

Monday, March 16th, 2009

I often need to remove files or folders in a dierctory - but I also often must not delete the whole directory, so here is the shell syntax for removing everything from a directory excepting your searchparameter.

This removes all files and subfolders in your current folder, excepting the cgi-bin directory.

ls | grep -v cgi-bin | xargs rm -R

search a string within a gzip file

Thursday, March 12th, 2009

You can search within possibly compressed files for a regular expression.
This helps me searching for strings in old compressed logfiles.

zgrep [ grep_options ] [ -e ] pattern filename…

This greps “searchstring” (without case sensitivity) from the compressed file in filename.gz
and writes the results to a new file with the name Result.

zgrep -i 'searchstring' ./filename.gz > ./Result

This greps the same string like in the example above, but with the difference that its not just parsing one file, its searching for the string within all gzip files in this directory and its only writing the filename where the searchstring is found to the new Result file.

zgrep -il 'searchString' ./*.gz > ./Result

search and replace strings from file

Tuesday, March 10th, 2009

Searching and replacing strings from a file can be done with sed.

sed 's/'SEARCH_STRING'/'REPLACE_STRING'/g' filename > new_filename

extracting specific files from a tarball

Tuesday, March 10th, 2009

If you have a big tarball and you dont want to extract all files and folders of it, you can use grep to extract only specific files or folders.

tar xfvkp Backup_23-06-07.tar.gz $(tar tf Backup_23-06-07.tar.gz | grep 'mySpecific/Folder')