Manage Users

Add User

Add a new user with username:

adduser [USERNAME]

Add the user to the sudo group:

usermod -aG sudo [USERNAME]

Verify:

su - [USERNAME]
sudo su

Remove User

userdel -r mynewuser

Commands

list

List and sort by date:

ll -halt

List with file sizes in MB:

ll --block-size=M

nohup

Run a nohup task and output a log file:

nohup sh your-script.sh > /path/to/custom.out &

scp

scp filename user@host:/path/to/

dhclient

Release the current IP address and obtain a new one:

sudo dhclient -r
sudo dhclient 

du

Display the total size of specific directory:

du -hs .

find

find /home/username/ -name "*.err"

Common Usage

Search filename in specific directory:

find DIRECTORY -type f -name "FILENAME"
find DIRECTORY -type f -iname "*FILENAME*"

Search file which are modified in specific time:

find . -type f -mmin -10 //less than 10 minutes
find . -type f -mmin +10 //more than 10 minutes
find . -type f -mtime -10 //less than 10 days
find . -type f -mtime +10 //more than 10 days

Execute command on the result (it is recommended that running the find before executing):

find . -type f -name "*.jpg" -maxdepth 1
find . -type f -name "*.jpg" -maxdepth 1 -exec rm {} +

Argument

-iname: filename with case insensitive

-type: file (f), directory (d)

find . -type f -name "FILENAME"
find . -type d -name "DIRECTORY"

-maxdepth: the depth of the directory

find . -type f -name "*.jpg" -maxdepth 1

-mmin: modify minutes
-mtime: modify days
-amin: access minutes
-atime: access days
-cmin: last change minutes
-ctime: last change days

find . -type f -mmin -10 //modified in the last 10 minutes
find . -type f -mmin +10 //modified 10 minutes ago
find . -type f -mtime -10 //modified in the last 10 days
find . -type f -mtime +10 //modified 10 days ago

-perm: permission

find . -perm 777

-size: file size

find . -size +5M //over 5 Mb
find . -size -1G //under 1 Gb

Tutorial: https://www.youtube.com/watch?v=KCVaNb_zOuw

grep

grep -r "CloudStack"  .

Search for texts within files on the system.

Short of “Global Regular Expression Print”.

Support RegEx searching.

Mac uses BSD grep, Linux uses GNU grep, a little bit different, check with (grep -V).

Common Usage

Basic format:

grep "KEYWORD" FILENAME

Searching with whole keyword (w), case sensitive (i), line information (n), and context (-C 4):

grep -win -C 4 "KEYWORD" FILENAME

Searching among files:

grep - win -C 4 "KEYWORD" ./dir/*.txt
grep - winr -C 4 "KEYWORD" ./dir/

Searching in the result of other command:

history | grep "ll"
history | grep "git commit" | grep "dot files"

Searching with the RegEx (GNU grep only):

grep -wirlP "\d{3}-\d{3}-\d{4}" .

Argument

-w: Search the whole keyword:

grep -w "KEYWORD" FILENAME

-i: Search the keyword with case sensitive:

grep -i "KEYWORD" FILENAME

-n: Show the line which the result located in:

grep -n "KEYWORD" FILENAME

-A: Show the context (after/before/both) the result:

grep -win -A 4 "KEYWORD" FILENAME
grep -win -B 4 "KEYWORD" FILENAME
grep -win -C 4 "KEYWORD" FILENAME

-r: Do recursive search:

grep -r "KEYWORD" DIRECTORY

-l: List only the file name:

grep -wirl "KEYWORD" DIRECTORY

-c: List how many times a keyword appears in the file:

grep -wirc "KEYWORD" DIRECTORY

Update (Mac)

To get more features such as RegEx searching, grep could be updated to the GNU grep with Homebrew (using –with-default-names to alternate the default grep):

brew install grep --with-default-names

Tutorial https://www.youtube.com/watch?v=VGgTmxXp7xQ

Zip

Zip a file without including the parent directory:

zip -j folder/artifact.zip folder/artifact1.app folder/artifact2.ext2 folder/artifact3.ext3

Reference

  1. What Is Nohup and How Do You Use It?
  2. How do I request a new IP address from my DHCP server using Ubuntu Server?
  3. Zip a file without including the parent directory