Commonly used Ubuntu Linux commands 常用的Ubuntu Linux指令

$ = the terminal command prompt against which commands are entered.

sudo = superuser.

<text> = information to be entered, angle brackets themselves are not to be entered.

[abc] = options a, b and c, brackets are not necessary.

Capitals and small letters behave differently.

Edit files

$ sudo nano <path and filename>

or

$ gksudo gedit <path and filename>

Note that the following can work sometimes but may have problem under some desktop environment:

$ sudo gedit <path and filename>

List open ports and their processes id

$ sudo lsof -i

or

$ sudo netstat -lptu

or

$ sudo netstat -lptun

Shutdown and startup

Shutdown the system:

$ sudo shutdown

Shutdown and reboot the system:

$ sudo reboot

Start a service:

$ sudo systemctl start <service name>

or older method:

$ sudo service <service name> start

or even older method:

$ sudo /etc/init.d/<service name> start

Restart a service:

$ sudo systemctl restart <service name>

or older method:

$ sudo service <service name> restart

or even older method:

$ sudo /etc/init.d/<service name> restart

Stop a service:

$ sudo systemctl stop <service name>

or older method:

$ sudo service <service name> stop

or even older method:

$ sudo /etc/init.d/<service name> stop

Checking disk space

Display disk space:

$ df

Display disk space in a more readable format:

$ df -h

Adjust percentage of reserved diskspace (5% by default), useful when a harddisk is full and can no longer accept disk writes, better change it back to 5% after the disk-full problem is solved:

$ sudo tune2fs -m "n" /dev/"sdXX"

where:

n = number of percentage, 1 for 1%

"sdXX" = harddisk device name as shown by $df -h

Find duplicate files across several directories and replace duplicate files with hard links to save space:

$ sudo rdfind -makehardlinks true /<directory 1>/<sub-directory 1>   /<directory 2>/<sub-directory 2>

note: rdfind downloadable at http://rdfind.pauldreik.se/rdfind.1.html#lbAG.

Recover such harddisk space of deleted files not reported by "$ df":

$ cd /<directory name of the harddisk>
​​$ sudo dd if=/dev/zero of=tempfile
$ sudo rm tempfile

Mounting devices

Mount a single device "/media/newbackupdrive":

$ sudo mount /media/newbackupdrive
  • Remounting required after reboot.

Umount single device "/media/newbackupdrive":

​$ sudo umount /media/newbackupdrive

Check devices defined in the filesystem table file "fstab" to be mounted upon booting:

$ cat /etc/fstab

Check devices actually mounted (this would show more than those defined in "fstab"):

$ sudo mount -l

List and change directories

List current directory contents, showing entries starting with ".":

$ ls -a

List current directory contents, with more detailed information:

$ ls -lh

List other directory contents:

$ ls -lh /<directory>/<sub-directory>

Change working directory:

$ cd /<directory>/<sub-directory>

Create directories

Make new directory:

$ mkdir /<directory>

Make new directory and sub-directory in one go, "-p" means making parent directory also:

$ mkdir -p /<directory>/<sub-directory>

Remove files and directories

Remove empty sub-directory:
$ rmdir /<directory>/<sub-directory>

Remove empty sub-directory and its parent directory in one go:

$ rmdir /<parent directory>/<sub-directory>

Remove file:

$ rm /<directory>/<sub-directory>/<filename>

Remove files and directories starting from and below sub-directory, even for empty sub-directory:

$ rm -r /<directory>/<sub-directory>

Copy files and directories

Copy a file within the same directory:

$ cp <source filename> <new filename>

Copy a file across different directories, keeping the same filename:

$ cp /<source directory>/<sub-directory>/<filename> /<target directory>/<sub-directory>/.​

to give:

/<target directory>/<sub-directory>/<filename>

​Copy a file across different directories, to a new filename:

$ cp /<source directory>/<sub-directory>/<filename> /<target directory>/<sub-directory>/<new filename>

to give:

/<target directory>/<sub-directory>/<new filename>

Omit:

/<source directory>/<sub-directory>/

if copying files in the current directory.

Use "*" to represent all files or "filename*" to represent all files with name beginning with "filename". 

Copy directories recursively, keeping the directory name:

$ cp -R /<source directory>/<sub-directory A> /<target directory>/.

to give:

/<target directory>/<sub-directory A>

Copy directories recursively:

$ cp -R /<source directory>/<sub-directory A> /<target directory>/<sub-directory B>

If sub-directory B exists, all files and directories under sub-directory A will be copied under sub-directory B.

If sub-directory B does not exist, it will be created, and all files and directories under sub-directory A will be copied under it.

Backup a directory of files

Archive (-a) all files under a sub-directory (A) and all sub-sub-directories underneath to the same sub-directory name under another sub-directory (B) keeping all the file attributes, symbolic links and time-stamps unchanged, preserving hard-links (-H) and displaying the progress verbosely (-v) and the numbers in human-readable format (-h):

$ sudo rsync -aHvh /<source directory>/<sub-directory A> /<target directory>/<sub-directory B>/​

to give:

/<target directory>/<sub-directory B>/<sub-directory A>

The command can be used repeatedly to update the files in sub-directory A in the new location. If the source files have not been changed, no over-writing copying will be done. This would save time and is better than the cp command.

Move files and directories

Use "mv" instead of "cp" for the above copy commands. No need to use "-R". Recursive move is the default.

Users and groups

Create new group:

$ sudo addgroup <new group name>

Create new user, with a group and a home directory of the same name created if not already existing:

$ sudo adduser <new user name>

To verify:

$ ls -lh /home

shows that the new directory has "drwxr-xr-x" permissions, i.e. "d" for directory with "rwx" owner permissions but "r-x" group and others' permissions.

Add a user to a group:

$ sudo adduser <user name> <group name>

Delete user, and group of the same name, keeping the home directory:

$ sudo deluser <user name>

Delete group:

$ sudo delgroup <group name>

Change file or directory permissions:

$ chmod -R [ugoa][-+=][rwxXst] /<directory>/<sub-directory>/<filename>

where:

"-R" = recursively from and below if is a  

[u=owner, g=group, o=others, a=all] 

[-+= mean minus, add, equal]

[permissions to r=read, w=write and delete, x=execute file or change directory into; Xst=for more special choices] 

e.g. "u+rw" = add read and write permission to owner

Change file or directory ownership:

$ chown -R <owner name>:<group name> /<directory>/<sub-directory>/<filename>

where:

"-R" = recursively from and below if <filename> is a sub-directory

Change password

Change own password:

$ passwd

Change other user 's password:

$ sudo passwd <other user's name>