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

Go to End

-> Top

Note

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.

-> Top

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.

-> Top

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

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>/."

-> Top

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 nano '{}' \;

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

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

Edit files

$ sudo nano <filename path>

or

$ gksudo gedit <filename path>

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

$ sudo gedit <filename path>
-> Top

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

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

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)

-> Top

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

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

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

Change password

Change own password:

$ passwd

Change other user 's password:

$ sudo passwd <other user's name>
-> Top

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

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

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

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)

-> Top

Move files and directories

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

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

-> Top

Back to top with progress scrollbar