LINUX , UNIX , BASH AND CYGWIN TIPS AND TRICKS
This document contains linux unix bash cygwin related
code snippets
1. FILE AND DIRS MANAGEMENT
This section contains tips and tricks on files and
dirs management.
1.1. how-to search for string
recursively in bash
Run the following command
for file in `find . \( -name '*.pm' -or -name
'*.pl' \) -type f `; do grep -nHP boo $file ; done ;
1.2. how-to create a symlink
how-to create a symlink
# START === create symlink
export link_path=/vagrant
export target_path=/mnt/hgfs/vagrant
mkdir -p `dirname $link_path`
unlink $link_path
ln -s "$target_path"
"$link_path"
ls -la $link_path;
# STOP === create symlink
1.3. how-to search and replace
line feeds in a selection in excel
1. Select the cells.
2. Ctrl + H to present the find and replace dialog
3. For search: press the Alt num and from the Num keyboard type : 013 or 011
4. For replace: type the char to replace with
5. Replace all the occurrencies by : Alt + A
1.4. how-to search and replace
strings in file contents recursively
In order to search and replace strings in the file and
dir contents perform the copy paste the following code in your bash shell
terminal.
# START -- how-to search and replace recursively
export dir=/var/company/3rdparty/docs/docx;
export to_search="emric"
export to_replace="t24"
#-- search and replace in file contents
find "$dir/" -type f -exec perl -pi -e
"s#$to_search#$to_replace#g" {} \;
find "$dir/" -type f -name '*.bak' |
xargs rm -f
# STOP --
how-to rename files recursively
1.5. how-to search and replace
strings in file and dirnames recursively
In order to search and replace strings in the file and
dir paths perform the copy paste the following code in your bash shell
terminal.
# START -- how-to search and replace recursively
export dir=/var/company/3rdparty/docs/docx;
export to_search="emric"
export to_replace="t24"
#-- search and replace in file names
find "$dir/" -type d |\
perl -nle '$o=$_;s#'"$to_search"'#'"$to_replace"'#g;$n=$_;`mkdir
-p $n` ;'
find "$dir/" -type f |\
perl -nle
'$o=$_;s#'"$to_search"'#'"$to_replace"'#g;$n=$_;rename($o,$n)
unless -e $n ;'
Bunch of commands for packing and unpacking on Linux
2.1.
how-to unpack a *.tar.gz package
Run the followint commands in the bash shell:
export dir=<>
cd $dir
# upack !!!
gzip -dc *.tar.gz | tar xvf -
2.2.
how-to unpack a tar package
how-to unpack a tar package
#how-to unpack tar file
tar xvf $file
3.1.
how-to check the type of processors on the System
how-to check the type of processors on the System
4.1.
how-to check which ports are listening
how-to check which ports are listening + others netstta
# which processes are listening on my system
netstat --tcp --listening --programs
netstat --tcp
netstat --route
4.2.
how-to implement public private key authentication - pkk
Read the comments
You migth wanto to:
export ssh_usesr=some_user_name
export ssh_server=some_server_dns_or_ip
4.2.1.
how-to implement rsa public private key authentication
Start on the client , check the comments when to move on the server.
# START === how-to implement public private key ( pkk ) authentication
# create pub priv keys on server
# START copy
ssh-keygen -t rsa
# copy the rsa pub key to the ssh server
scp ~/.ssh/id_rsa.pub $ssh_user@$ssh_server:/home/$ssh_user/
# STOP copy
# Hit enter twice
# START copy
cat id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys
chmod -v 0700 ~/.ssh
chmod -v 0600 ~/.ssh/authorized_keys
chmod -v 0600 ~/.ssh/id_rsa
chmod -v 0644 ~/.ssh/id_rsa.pub
find ~/.ssh -exec stat -c "%U:%G %a %n" {} \;
rm -fv ~/id_rsa.pub
# STOP COPY
4.2.2.
how-to implement dsa public private key authentication
Start on the client , check the comments when to move on the server.
# START copy
ssh-keygen -t dsa
# STOP copy
# Hit enter twice
# START copy
cat id_dsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys
chmod -v 0700 ~/.ssh
chmod -v 0600 ~/.ssh/authorized_keys
chmod -v 0600 ~/.ssh/id_dsa
chmod -v 0644 ~/.ssh/id_dsa.pub
find ~/.ssh -exec stat -c "%U:%G %a %n" {} \;
rm -fv ~/id_dsa.pub
# STOP COPY
# STOP === how-to implement public private key authentication
5.1.
how-to use while loop in bash
This is an example usage of the while loop in bash
find `pwd` | { while read -r file ; do echo "$file" ; done ; }
5.2.
how-to use for loop in bash
This is an example usage of the while for in bash. Note the find command
herewith searches for 2 types of files
for file in `find / -type f \( -name "*.pl" -or -name
"*.pm" \) -exec file {} \; | grep text | perl -nle 'split /:/;print
$_[0]' `; do grep -i --color -nH 'string_to_search' $file ; done ;