https://www.linuxfoundation.org/

Mount from remote windows server (windows share)

  1. Login as root
1
$ su -
  1. Create a mount point
1
# mkdir -p /mnt/ntserver
  1. Mount the server
1
# mount -t cifs //192.168.1.100/folder_to_be_mount -o username=jslim89,password=passwd /mnt/ntserver

NOTE

  • -t cifs - File system type to be mount
  • -o - in order to pass in arguments (i.e. username and password)
Reference:

GREP exclude certain keyword

1
$ grep "*wanted*" -v "*dontWant*" *

In this case it will match the keyword wanted but exclude all the word with dontWant

-v option is for excluding what you don’t want

Reference:

find exclude certain filename

1
$ find . -not -iname "*dontWant*" -iname "*wanted*"

In this case it will match the filename with the word wanted but exclude those with the word dontWant

Reference:

svn commit excluding those files you haven’t done

1
$ svn st | grep -v Foo | cut -c9- | xargs svn commit

svn st - show the files that have been modified
grep -v Foo - exclude those file path with the word Foo
cut -c9- - to remove the extra character in front. i.e.
M ./path/to/Foo/bar.php
after cut become ./path/to/Foo/bar.php
xargs svn commit - pass the output from command in front to svn commit

Reference:

Change “localhost” to other name

1
$ vi /etc/sysconfig/network

Just change from here


echo with new line character

1
$ echo -e [ui]\nusername = Your Name <your@email.com>

which will produce:

1
2
[ui]
username = Your Name <your@email.com>

-e - is to enable intepretation of backslash escapes (i.e. \n in this case)

Reference:

Change password for other users

1
$ sudo passwd userfoo
Reference:

Display size of directory

1
$ du -ch /path/to/dir | grep total
Reference:

Start SSH server on bootup

1
$ su -c 'chkconfig sshd on'

yum install without gpg-key

# yum install --nogpgcheck package-name


replace pattern with sed

Example: replace window.location.reload('#page1#&#page2#') to window.location.href='#page1#&#page2#';

1
$ sed -i "s/reload('#\(\w\+\)#&#\(\w\+\)#')/href='#\1#\&#\2#';/" /path/to/file

NOTE: the & in the destination must use \ to escape, otherwise it will refer to the source
i.e.

1
$ sed -i "s/reload('#\(\w\+\)#&#\(\w\+\)#')/href='#\1#&#\2#';/" /path/to/file

will be come

1
window.location.href='#choosepage#reload('#choosepage#&#queryString#')#queryString#';

Replace all occurance in a directory

1
$ grep "reload" -Rl * | xargs sed -i "s/reload('#\(\w\+\)#&#\(\w\+\)#')/href='#\1#\&#\2#';/"

Assumption: reload keyword only occur in this situation.
-R - recursively looking for all files in sub-directories
-l - show only the filename


Sync 2 directories using rsync

1
$ rsync -vr /path/to/source/ /path/to/destination/
  • -v - verbose
  • -r - recursive
Reference:

Forward & Backward search in bash

CTRL + R - backward search
CTRL + S - forward search (Use after typing CTRL + R)
If CTRL + S doesn’t work, use the command below:

1
$ stty stop ^J
Reference:

Remove the trailing dot behind the permission drwxrwxrwx

1
$ find /path/to/dir -print0 | xargs -0 -n 1 sudo setfattr -h -x security.selinux

The last character can be:
(Blank) - no SELinux coverage
. (dot) - ordinary SELinux context only
+ (plus) - SELinux ACLs or other things beyond ordinary context

Permission trailing dot

Reference:

Debugging in Apache configuration

Recently I faced a problem faced a problem in Fedora 17. Here is the question that I post to serverfault.com.

If you faced 403 status in your browser, please check the following:

  1. Permission for your /home/user and subdirectories to your project if you’re using symbolic link
  2. Check your httpd.conf file
    • AllowOverride None change to AllowOverride All to enable .htaccess
    • Options Indexes FollowSymLinks make sure that FollowSymLinks is there if you’re using symbolic link

NOTE: For the 2, the configuration is in document root,
i.e.

1
2
3
4
5
6
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Credit: Kian-Meng, Ang


awk usage

Extract out certain portion of input by a separator

1
$ echo "hello to awk" | awk '{print $3}'

The output will be awk.
By default it will separated by a <space> character

1
$ echo "hello_to_awk" | awk -F _ '{print $3}'

-F - specify separator instead of using <space> by default

Reference:

Rename a file to md5 hash

1
$ md5sum ori_filename | awk '{print $1}' | xargs mv ori_filename

Xmodmap key mapping (fix for NEC Versa E6300)

I had faced a problem when I want to use | (Pipe) it always give me > character, here is a fix.

1
$ echo "keycode 94 = backslash bar" > ~/.Xmodmap

94 is the key for backslash
backslash bar is refer to 2 character, 1 is \ another 1 is |


Run a programming without providing ABSOLUTE PATH

1
$ `which javac` HelloWorld.java

This example is to show that compile a java file without providing absolute path for javac (i.e. /opt/jdk/bin/javac).
Here using which to get the absolute path of javac then use the output as the command.


tree show only the first level

By default it will shows all files, but I definitely cannot have a clear image on that. The command below shows only the first level

1
$ tree /path -L 1

-L - refer to the level you want


Remove trailing space for each line using sed

1
$ sed -i 's/[[:space:]]*$//' filename
Reference:

Continuously watch a file

This is to monitor a file in few seconds interval

1
$ watch -n 3 -d tail file_to_watch.txt

In this case I monitor the file file_to_watch.txt for every 3 second

Reference:

Kill all process from grep

Let say now want to kill all fcgi processes

1
$ ps aux | grep fcgi | awk '{print $2}' | xargs kill -9

If you want to kill other process, just replace the fcgi in the second portion with the process that you wanted to kill.

i.e.

1
$ ps aux | grep <process_name> | awk '{print $2}' | xargs kill -9

Change time zone in Ubuntu Server

1
$ sudo dpkg-reconfigure tzdata

Then choose your Time Zone now.

Reference:

Zip files with password protection

1
$ zip -e zipname.zip file1.txt folder1/*
Reference:

Download a file using curl

1
$ curl -o dest_file.tar.gz http://example.com/file.tar.gz
Reference:

CLear history in terminal

1
$ history -c && history -w
  • history -c - clear the history
  • history -w - write the now history file
Reference:

Change directory & file permission recursively

1
2
3
4
# change directories mode to -rwxr-xr-x
$ find . -type d -exec chmod 755 {} \;
# change files mode to -rw-r--r--
$ find . -type f -exec chmod 644 {} \;
Reference:

Apache error when running a2ensite command

The error message look like ERROR: Site domain.com does not exist

Just have to ensure the file name end with .conf extension.

Reference:

Find a folder by it’s name

1
$ find . -type d -iname folder_name
  • -type d is refer to directory (or folder)
  • -iname is looking for name (case-insensitive)

List all users in a group

1
$ grep ^usergroup /etc/group
Reference:

List all users

1
$ less /etc/passwd
Reference:

Monitor/Watch a file content change

1
$ tail -f thefile.txt
Reference:

Loop through all files recursively

Find all php files recursively

1
2
3
4
5
6
FILES=$(find /path/to/project/ -type f -name *.php)

for file in $FILES
do
echo $file
done

Extract file name without extension

1
filename="${file%.*}"
Reference:

Show current running distro

1
$ lsb_release -a
Reference:

Generate SQL from script and import to db

1
$ php generate_sql.php | mysql -u root -p mydatabase

1
$ chown -h myuser:mygroup mysymbolic

Need a -h option

Reference:

Install/Remove crontab for other user

1
2
3
4
5
# install
$ crontab cron_content.txt -u username

# remove crontab from certain user
$ crontab -r -u username

Kill all process with certain name pattern

1
$ pkill -f foobar
Reference:

Run command with specific user

1
$ sudo -u www-data php run.php
Reference:

Find the filename from a content

1
ls -S /path/to/files_*.csv.gz | xargs -L1 -d'\n' -I{} sh -c "zcat '{}' | grep 'some content' && echo '{}'"