Linux Tips and Tricks


(1) 查看文件夹大小Check Folder/Directory Total Size

Tested on: Ubuntu

The command du “summarizes disk usage of each FILE, recursively for directories,” e.g.,

1
du -hs /path/to/directory

-h is to get the numbers “human readable”, e.g. get 140M instead of 143260 (size in KBytes)
-s is for summary (otherwise you’ll get not only the size of the folder but also for everything in the folder separately)

(2) 命令别名 Command Alias

对当前用户,修改 ~/.bashrc
对所有用户,修改 /etc/bashrc

1
2
3
4
5
6
7
cd ~
ls -a
touch .profile
vi .profile
add alias ll='ls -l'
save/exit
source .profile

Or use this command:
echo "alias ll='ls -hal'" >> ~/.bashrc
and make sure that this file is executed whenever an user enters a shell by adding the following in ~/.bashrc:

1
2
3
# Source global definitions
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrcfi

(3) Show login history

1
sudo less /var/log/auth.log

(4) How to use Screen

1
2
3
4
screen
screen -list
screen -r xxxxx
screen -X -S [session # you want to kill] quit

(5) ls -hal 超级好用

Linux ‘ls’ Flags Quick Reference Table
| FLAG | DESCRIPTION | EXAMPLE |
|:–:|–|–|
| -l | Displays file types, owner, group, size, date and filename. |ls -l|
|-a|Displays all files, including hidden files (suffixed with “.”).|ls -a|
|-h|Displays files with human readable format, e.g. 1k, 100MB, 1G.|ls -h|

(6) Check reboot/shutdown history

Use the last command.

1
2
last -x | grep shutdown
last -x | grep reboot

You can pipe this to the head to read the last n instances. For example, in your case:

1
last -x | grep shutdown | head -n 10

You also should set BOOTLOGD_ENABLE=Yes in the /etc/default/bootlogd file (it could be No by default).

(7) Scp files recursively

Unlike ssh, scp uses the uppercase P switch to set the port instead of the lowercase p:

1
scp -r -P 22 . user@192.168.1.1:ftp_dir

(8) Show groups

1
2
groups
cut -d: -f1 /etc/group #show entire group list

Change Apache Document Root Directory on Mac OS


Method 1

Tested Okay by Aiden Fang!

First I modified the following lines in /etc/apache2/httpd.conf:

1
2
3
4
5
6
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
Options FollowSymLinks Multiviews
AllowOverride None
</Directory>

to:

1
2
3
4
5
6
DocumentRoot "<CUSTOM_PATH>"
<Directory "<CUSTOM_PATH>">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
</Directory>

The above will set a custom DocumentRoot, enable directory listing and allow configurations to be overridden by .htaccess files.

Then I restarted apache by executing

1
sudo apachectl restart


Method 2

Not tested

Another approach would be to set up a virtual host. First make sure so that the following line is uncommented in your /private/etc/apache2/httpd.conf file:

1
2
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

Then you can add the following in the httpd-vhosts.conf file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost *:80>
ServerAdmin webmaster@example.local
DocumentRoot "/Library/WebServer/Documents"
ServerName example.local
ErrorLog "/private/var/log/apache2/example.local-error_log"
CustomLog "/private/var/log/apache2/example.local-access_log" common
<Directory "/Library/WebServer/Documents">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

The above will setup a document root for a new virtual host named example.local and enable directory listing and allow configurations to be overridden by .htaccess files. Of course your also will need to restart apache for the changes to take effect:

1
sudo apachectl restart

Reference:
http://stackoverflow.com/questions/26486782/change-localhost-directory-for-yosemite-apache-2-4