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

Go to End

Note

3/2/2026: "Recreate deleted user with specific UID" and "Recreate mailbox-only account" sections added.
29/1/2026: "micro" added as an editor. "gksudo gedit" removed.

29/7/2022: Setting up SSH keys added.

18/4/2022: cp -Rav added.

14/4/2022: Command to delete multiple empty sub-directories added.

18/12/2021: Updated.

6/10/2018: Updated.

27/9/2018: Updated.

21/3/2018: Updated.

27/8/2017: Created.

Intro

$ = 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.

View system information

Show who am I:

$ whoami

Show current date and time:

$ date

Show this month's calendar:

$ cal

List open ports and their processes id:

$ sudo lsof -i

or

$ sudo netstat -lptu

or

$ sudo netstat -lptun

Display disk space:

$ df

Display disk space in a more readable format:

$ df -h

Display file contents:

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

Use path and file names

<directory path> = <directory>/<sub-directory> referring from the current directory, without filename stated.

<filename path> = <directory>/<sub-directory>/<filename> referring from the current directory.

Precede with "/" if referring the path from the root directory. Deleting files under a sub-directory but incorrectly referring to the root directory is dangerous. Better avoid referring from the root directory.

Use "*" as a wildcard to represent texts before or after directory or file name, e.g. "*name", "name*" or "*" for any name.

Use "." alone to represent the current directory.

Use "../" to represent the directory immediately above the current directory.

Use "/." at the end to represent all under the stated <directory path>, e.g. "<sub-directory>/."

Search things

Find files with filenames containing <filename> in <directory path>:

$ find <directory path> -name "<filename>"

Use "-iname" to ignore upper or lower case differences.

Find the files and open for editing:

$ find <directory path> -iname "<filename>" -exec micro '{}' \;

Find text in files:

$ grep -[options] <text> <filename path>

where option:

  • -i = ignore upper or lower case differences
  • -r = search sub-directories recursively
  • -h = hide names of files preceding lines of output
  • -w = search for exact text
  • -c = count the number of matches
  • -n = precede lines of output with the numbers of lines containing the text
  • -v = show only lines not containing the text
  • -l = list filenames only
  • --colour = display the output in colour

Adjust disk space

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

$ sudo rdfind -makehardlinks true <directory path 1> <directory path 2>

note: rdfind downloadable at https://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

Edit files

Text editor

$ sudo micro <filename path>

or text editor

$ sudo nano <filename path>

or GUI editor

$ sudo gedit <filename path>

Shutdown and start up

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

Mount 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

Set up SSH keys to access remote computer

Generate public/private rsa key pair:

$ ssh-keygen -t rsa

Enter file in which to save the key (/home/<your user name>/.ssh/id_rsa): <accept or change>

Skip entering passphrase if do not want to use it upon logging in with ssh.

Using $ sudo ssh-keygen -t rsa will prompt to save the key at /root/.ssh/id_rsa.

If the file already exists, answer overwrite or not.

Copy the public key to the remote computer:

$ ssh-copy-id -p <port number> <login name>@<remote computer address>

Enter password to the remote login name.

It will check whether the key has already existed.

Use this if desired to force copy:

$ ssh-copy-id -f -p <port number> <login name>@<remote computer address>

Verify by logging in after successful copying:

ssh -p <port number> <login name>@<remote computer address>

If login successful, you are at the command prompt of the remote computer and can execute commands there.

Exit after use.

(added, 29/7/2022)

Add or delete groups and users

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 ownership

Change file or directory ownership:

$ chown -R <owner name>:<group name> <filename path>

where:

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

Change permissions

Change file or directory permissions:

$ chmod -R [ugoa][-+=][rwxXst] <filename path>

where:

  • -R = recursively from and below if <filename> is a <sub-directory>
  • u=owner, g=group, o=others, a=all
  • -+= mean minus, add or equal permissions
  • 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

An alternative form is:

$ chmod -R <ugo> <filename path>

where:

  • <ugo> is a 3 digit number where u=owner, g=group, o=others
  • each digit can be:
    • 0 for nothing
    • 1 for execute "x"
    • 2 for write "w"
    • 3 = 1 + 2 = "wx"
    • 4 for read "r"
    • 5 = 4 + 1 = "rx"
    • 6 = 4 + 2 = "rw"
    • 7 = 4 + 2 + 1 = "rwx"

therefore:

  • 666 = read and write permissions to all
  • 777 = read, write and execute permissions to all

Change password

Change own password:

$ passwd

Change other user 's password:

$ sudo passwd <other user's name>

Recreate deleted user with specific UID

(section added, 3/2/2026)

To recreate deleted user account:

  • Existing email folders: /home/xyz/Maildir
  • User ID = 1234
  • User name = xyz

Open Command Prompt.

Check the presence of directory xyz and the UID of the Maildir is 1234:

> ls -ls /home
> sudo ls -ld /home/xyz/Maildir

Re-create full user account with the same UID and login shell:

> sudo useradd -u 1234 -d /home/xyz -s /bin/bash xyz
  • -u 1234 → sets the UID to 1234
  • -d /home/xyz → sets the home directory
  • -s /bin/bash → sets the default shell

Set the password for the re-created user:

> sudo passwd xyz

Enter and confirm the password.

Fix ownership of the Maildir (if needed) if the Maildir or home directory files are not owned by UID 1234 after recreating the user:

> sudo chown -R xyz:xyz /home/xyz

Ensure UID is 1234 and Maildir is accessible:

> id xyz
> sudo -u xyz ls /home/xyz/Maildir

If another user already exists with UID 1234, resolve that conflict first (either delete or re-assign that user).
Always back up the Maildir before making changes, just in case ownership or permissions get misapplied.

Recreate mailbox-only account

(section added, 3/2/2026)

To recreate deleted user account for mail delivery:

  • Existing email folders: /home/xyz/Maildir
  • User ID = 1234
  • User name = xyz

Open Command Prompt.

Check the presence of directory xyz and the UID of the Maildir is 1234:

> ls -ls /home
> sudo ls -ld /home/xyz/Maildir

Re-create mailbox-only account with the same UID but without login shell:

> sudo useradd -u 1234 -d /home/xyz -s /usr/sbin/nologin xyz

-s /usr/sbin/nologin → prevents shell login

Set a locked password o nobody can log in interactively:

> sudo passwd -l xyz

Enter and confirm the password.

Fix ownership of the Maildir (if needed) if the Maildir or home directory files are not owned by UID 1234 after recreating the user:

> sudo chown -R xyz:xyz /home/xyz

Ensure UID is 1234 and Maildir is owned by xyz.

> id xyz
> ls -ld /home/xyz/Maildir

If another user already exists with UID 1234, resolve that conflict first (either delete or re-assign that user).
Always back up the Maildir before making changes, just in case ownership or permissions get misapplied.

Optional: Restrict home directory access if you want to prevent accidental browsing:

> sudo chmod 700 /home/xyz
  • Only xyz (and root) can access it.

The account will still be valid for mail delivery and IMAP/POP access using Postfix + Dovecot.

The nologin shell ensures it can’t be used for SSH or console login.

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 path>

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

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

Remove all empty sub-directories and their parent directories in one go:

$ cd <directory>
$ find . -type d -empty -print -delete

If directory is empty, print name then delete.

(added, 14/4/2022)

Remove file:

$ rm <filename path>

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

$ rm -r <directory path>

List and change directories

List names of current directory contents, hiding entries starting with ".":

$ ls

List current directory contents, with more detailed information:

$ ls -[options]

where options

  • l = list also permissions, owners, date and size
  • a = list also entries starting with "."
  • h = to be used in conjunction with "l", show file sizes in "K" or "M"

List other directory contents:

$ ls -[options] <directory path>

Change working directory:

$ cd <directory path>

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.

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.

Copy directories recursively (-R) keeping original attributes (-a) and showing verbose progress (-v):

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

(added, 18/4/2022)

Move files and directories

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

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.

End of Page