Brief Introduction to Linux

Brief Introduction to Linux

This guide is designed for new users of the Levich Institute Linux servers who have no prior experience.


Linux file system and basic commands

Inquire system resources

Show operating system:

jmao@Jupiter:~$ cat /etc/issue
Ubuntu 22.04.1 LTS

Show kernel version:

jmao@Jupiter:~$ uname -a
Linux Jupiter 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


Show disk usage of mounted file systems:

jmao@Jupiter:~$ df
Filesystem      1K-blocks       Used  Available Use% Mounted on
/dev/sdb        263174212   31208448  218527608  13% /
tmpfs             6524932          0    6524932   0% /mnt/wsl
tools           511747028  506051256    5695772  99% /init
none              6524932          4    6524928   1% /run
none              6524932          0    6524932   0% /run/lock
none              6524932          0    6524932   0% /run/shm
none              6524932          0    6524932   0% /run/user
tmpfs             6524932          0    6524932   0% /sys/fs/cgroup
drivers         511747028  506051256    5695772  99% /usr/lib/wsl/drivers
lib             511747028  506051256    5695772  99% /usr/lib/wsl/lib
C:\             511747028  506051256    5695772  99% /mnt/c
D:\            1953512444  863395912 1090116532  45% /mnt/d
F:\            5860487164 4247379760 1613107404  73% /mnt/f
G:\             104857600   99446620    5410980  95% /mnt/g

Show disk usage of mounted file systems in human readable unit:

jmao@Jupiter:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        251G   30G  209G  13% /
tmpfs           6.3G     0  6.3G   0% /mnt/wsl
tools           489G  483G  5.5G  99% /init
none            6.3G  4.0K  6.3G   1% /run
none            6.3G     0  6.3G   0% /run/lock
none            6.3G     0  6.3G   0% /run/shm
none            6.3G     0  6.3G   0% /run/user
tmpfs           6.3G     0  6.3G   0% /sys/fs/cgroup
drivers         489G  483G  5.5G  99% /usr/lib/wsl/drivers
lib             489G  483G  5.5G  99% /usr/lib/wsl/lib
C:\             489G  483G  5.5G  99% /mnt/c
D:\             1.9T  824G  1.1T  45% /mnt/d
F:\             5.5T  4.0T  1.6T  73% /mnt/f
G:\             100G   95G  5.2G  95% /mnt/g

Show disk usage of folders:

jmao@Jupiter:~$ du -hs *
1.2M    1akk
144K    1akk.pdb1
7.6M    1fue
6.0M    4j8p
116M    4lzt
1.1M    4lzt_new
25M     6nwa
3.2G    Downloads
148K    Figure_1.png
4.0K    LightsailDefaultKey-us-east-1.pem
90M     Miniconda3-latest-Linux-x86_64.sh
8.0K    Untitled.ipynb
4.0K    a
32K     batch-download-structures-1716948321485.zip
4.0K    bin
4.0K    chemsilo
48K     cve_list.json
440M    cves
940K    cyc

Show  CPU and memory usage:

top

Press "q" to quit.

or

htop

Press F10 key to quit.

 

Show memory usage:

jmao@Jupiter:~$ free -h
               total        used        free      shared  buff/cache   available
Mem:            12Gi        66Mi        11Gi       0.0Ki       969Mi        12Gi
Swap:          4.0Gi          0B       4.0Gi

 

Show current users on the system:

who

 

Show recently logged in users:

last

 

Show user information:

finger username

 

Send a message to another user:
Syntax: write username [terminal]

write jmao pts/0

Use CTRL-D to end the message.

 

Display a command manual:

man last

 

Inquire current user name:

whoami


File system and related commands

List files:

Basic command: ls

jmao@Jupiter:~$ ls
1akk                                         bin            java_error_in_pycharm_1372.log  pcet
1akk.pdb1                                    chemsilo       java_error_in_pycharm_1425.log  pcet_demo
1fue                                         cve_list.json  java_error_in_pycharm_1470.log  projects
4j8p                                         cves           java_error_in_pycharm_1561.log  pycharm
4lzt                                         cyc            java_error_in_pycharm_1941.log  scan.txt
4lzt_new                                     d              java_error_in_pycharm_2031.log  seaborn-data
6nwa                                         delphi         java_error_in_pycharm_307.log   test
Downloads                                    diprotic_demo  java_error_in_pycharm_3624.log  test.dat
Figure_1.png                                 drobo          java_error_in_pycharm_3687.log  test.txt
LightsailDefaultKey-us-east-1.pem            fad            java_error_in_pycharm_387.log   uiuc
Miniconda3-latest-Linux-x86_64.sh            fmn_Em_-520    java_error_in_pycharm_475.log   user_param
Untitled.ipynb                               granepura      java_error_in_pycharm_489.log
a                                            index.html     jose
batch-download-structures-1716948321485.zip  ipscan         miniconda3

 

Some useful options for command `ls`:

Command Function
ls -l List details
ls -lh list with human readable details units
ls -a list hidden files *
ls -lt list files in chronological order, newest first
ls -lrt list files in chronological order, reversed order

* Under Linux, hidden files are those files names tart with a "."

 

Navigate file system
Go to home directory:

cd or cd ~

Go to parent directory:

cd ..

In Unix, ". ." means parent directory, "." means current directory, and "~" means user's home directory.

Go to a directory: 

cd Documents

or

cd /home/jmao/Documents

The first command uses relative path and the second uses absolute path.

 

Show current directory's absolute path: 

pwd

Look for files
To locate a file by name:

jmao@ChemE:~$ locate Documents
/Users/jmao/Documents
/Users/jmao/Documents/MATLAB
/Users/jmao/Documents/New Document.ott
/Users/jmao/Documents/New Spreadsheet.ots

jmao@ChemE:~$ locate -b '\Documents'
/Users/jmao/Documents

The command "locate" searches folder name and file name that contains the specified string. If you want to search the exact file name, the option "-b" limits the scope to base name (excluding path name before the match, but still printed out), and the escape "\" removes the global name match. Be noted that `locate` command searches from a database refreshes daily, therefore new files may not show up in the search.

To search a file:

jmao@ChemE:~$ find ./ -name Documents
./Documents

The locate command searches a pre-updated database (usually refreshed everyday) and the find command searches the actual file system.

jmao@ChemE:~$ touch myfile
jmao@ChemE:~$ locate -b '\myfile'
jmao@ChemE:~$ find ~/ -name myfile
/home/jmao/myfile

Find an executable

"whereis" finds the command location and the same command name may appear in multiple places. "which" finds the location of command that will be called. You can always specify the full path to call the desired command.

jmao@ChemE:~$ whereis python python: /usr/bin/python3.6 /usr/bin/python2.7 
/usr/bin/python3.6m /usr/bin/python3.5m /usr/bin/python3.5 
/usr/bin/python /usr/lib/python3.6 /usr/lib/python2.7 /usr/lib/python3.5 
/usr/lib/python3.7 /etc/python3.6 /etc/python2.7 /etc/python3.5 /etc/python 
/usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/local/lib/python3.5 
/usr/include/python2.7 /usr/include/python3.6m /usr/share/python 
/Users/jmao/miniconda3/bin/python /Users/jmao/miniconda3/bin/python3.6m-config 
/Users/jmao/miniconda3/bin/python3.6m /Users/jmao/miniconda3/bin/python3.6-config 
/Users/jmao/miniconda3/bin/python3.6 /usr/share/man/man1/python.1.gz 

jmao@ChemE:~$ which python /home/jmao/miniconda3/bin/python

 

Find current directory:

pwd


Unix file permissions

If you list files in details, you see some strings like this:

jmao@ChemE:~$ ls -l
total 84
drwxr-xr-x  2 jmao jmao  4096 Feb  7 14:35 Desktop
-rwxrwxr-x  1 jmao jmao    45 Jan 25 07:21 test3.py
-rw-r--r--  1 jmao jmao    87 Jan 25 07:49 test.py
drwxr-xr-x  2 jmao jmao  4096 Mar 27  2018 Videos

d: directory
r: read permission
w: write permission
x:execute permission

The first character is reserved for directory. Other 9 characters are divided into three rwx fields. They are for owner, group, and others. The permission bits control how these files are read, wriitten, and determine if they can be run as a program.

To change permission, use

chmod

command.

Word mode of chmod command

One way to edit the file permission bits is through the word mode. Example:

chmod a+w file

In above example, "a" is reference, "+" is operator, and "w" is mode. It means, all three permission fields turn on write permission. Other references, operators and modes are:

Reference Meaning
u User (Owner)
g Group
o Others
a All, same as "ugo"

 

Operator Meaning
+ Turn on
- Turn off
= Set to be

 

Mode Meaning
r Readable
w Writable
x Executable, or allow to enter when being a directory

Example:

"chmod a+x file" turns a file to be executable by all users.

"chmod go-rwx file" makes a file not accessible by group and other users.

Binary mode of command chmod

Alternatively, you can set a file permission with a binary string.

rwx fields can be viewed as a binary number. When set, the value is 1, otherwise is 0. For example, rw_rw_rw is 110,110,110 in binary, and 666 in octal. So

chmod 666 file

means to set file to permission ```rw_rw_rw```.

This is a very straight forward way to set permission.

Handle files

Here are some commonly used commands to handle files:

Command Example Meaning
cp cp file file.bak copy file to file.bak
  cp -r run01 run02 recursicely copy directory run01 to directory run02
  cp -a file1 file2 copy file and preserve file properties (permission, time etc)
rm rm file delete file
  rm -rf folder recursively and silently delete directory named folder
mv mv file1 file2 rename (move)  file1 to file2
file file filename determine file type, text or binary or compressed, etc
cat cat text_file print text file contents
less less text_file navigate text file content, "q" to exit
more more text-file same as "less" command
mkdir mkdir dirname make a directory
rmdir rmdir dirname delete an empty directory, see "rm -rf"
touch touch file update file time stamp or create an empty file
wc wc -l text_file count number of lines


Special commands

There are some commands to handle odd situations in a terminal, for example, a terminal may hang, or print unrecognizable characters. These command may help.

CTRL-D

Send an EOF signal to the terminal.  Some commands like mail and talk let you type text in terminal as input, but needs a non-text to indicate the end of input. This key combination indicates the end of file (text).

If you press this key combination at the terminal prompt, it serves as log out command. 

CTRL-C

Interrupt a command, not resumable. Example:

jmao@ChemE:~$ sleep 60
^C

CTRL-Z

Interrupt a command, resumable with "fg" at foreground or "bg" at background.

jmao@jmao-desktop ~/projects/mcce-develop $ sleep 120 
^Z
[1]+  Stopped                 sleep 120
jmao@jmao-desktop ~/projects/mcce-develop $ fg
sleep 120

&

Send a job to background. Background job runs at background and you have the control of terminal before command finishes.

sleep 10
sleep 10 &


"jobs"

Check jobs issued from this terminal.

 

CTRL-L or "clear"


Clear the termical window.

"reset"

Reset and clear terminal. In case you ran some program and turned your terminal unusable, type in reset may give you back the terminal.

"history"

Show command history of this terminal window.

"echo $?"

$? is a special environment variable, holding the return value of the previous command. Each command has an exit code. Normally 0 means successful exit. 
 

Last Updated: 08/29/2024 15:48